I am using Chart.JS to plot some data from external JSON. But I am unable to format the X-AXIS in a proper way.
My html & javascripts codes are here:
$(document).ready(function(){
$.ajax({
url : "https://api.npoint.io/1f7ea8eeaa8996ad0d71",
type : "POST",
dataType: "json",
success : function(data){
console.log(data);
var reg_date = [];
var datetime = [];
var temperature = [];
var humidity = [];
var effluent_level = [];
for(var i in data) {
reg_date.push(new Date(data[i].reg_date));
temperature.push(data[i].temperature);
humidity.push(data[i].humidity);
effluent_level.push(data[i].effluent_level);
}
var chartdata = {
labels: reg_date,
type: 'time',
distribution: 'linear',
datasets: [
{
label: "Temperature",
fill: false,
//lineTension: 0.1,
backgroundColor: "rgba(59, 89, 152, 0.75)",
borderColor: "rgba(59, 89, 152, 1)",
pointHoverBackgroundColor: "rgba(59, 89, 152, 1)",
pointHoverBorderColor: "rgba(59, 89, 152, 1)",
data: temperature
},
{
label: "Humidity",
fill: false,
//lineTension: 0.1,
backgroundColor: "rgba(29, 202, 255, 0.75)",
borderColor: "rgba(29, 202, 255, 1)",
pointHoverBackgroundColor: "rgba(29, 202, 255, 1)",
pointHoverBorderColor: "rgba(29, 202, 255, 1)",
data: humidity
},
{
label: "Effluent Level",
fill: false,
//lineTension: 0.1,
backgroundColor: "rgba(211, 72, 54, 0.75)",
borderColor: "rgba(211, 72, 54, 1)",
pointHoverBackgroundColor: "rgba(211, 72, 54, 1)",
pointHoverBorderColor: "rgba(211, 72, 54, 1)",
data: effluent_level
}
]
};
var ctx = $("#mycanvas");
var LineGraph = new Chart(ctx, {
type: 'line',
data: chartdata
});
},
error : function(data) {
}
});
});
<!DOCTYPE html>
<html>
<head>
<title>ChartJS - LineGraph</title>
<style>
.chart-container {
width: 640px;
height: auto;
}
</style>
</head>
<body>
<div class="chart-container">
<canvas id="mycanvas"></canvas>
</div>
<!-- javascript -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js"></script>
<script type="text/javascript" src="linegraph.js"></script>
</body>
</html>
Output Image:
But I want to show only time values in X-AXIS (not losing date data). How to plot the graph in an effective way to represent the data?
