简体   繁体   中英

showing Y axis in a chart using CanvasJS

I made a chart that shows machines and the times that was taken for them to be fixed (machines X, time Y) the data is loaded dynamically via mysql like the following

<?php
include_once(“connexion.php”);
ini_set(‘max_execution_time’,300);
//ini_set(‘error_reporting’, E_ALL);

$sq = “select machine,date_tech,time(time_fin – time_deb) as diff from action_archiv_tech WHERE (date_tech BETWEEN ‘2018-09-10’ AND ‘2018-10-06’)”;
$r = mysql_query($sq) or (die(mysql_error()));
while($tab = mysql_fetch_assoc($r)) {
$machine = $tab[‘machine’];
$diff = $tab[‘diff’];
// $data[] = [“label”=>$machine, “y”=>$diff];
$data[] = array(
‘label’ => $tab[‘machine’],
‘y’ => $tab[‘diff’]
);
}

?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset=”UTF-8”>
<script>

window.onload = function () {

var chart = new CanvasJS.Chart(“chartContainer”, {
theme:”light2″,
animationEnabled: true,
title:{
text: “PM”
},
axisY :{
includeZero: false,
title: “Heures”,
suffix: “H”
},
toolTip: {
shared: “true”
},
legend:{
cursor:”pointer”,
itemclick : toggleDataSeries
},
data: [
{
type: “spline”,
showInLegend: true,
yValueFormatString: “##.00H”,
name: “Curative”,
dataPoints: <?php echo json_encode($data); ?>
}

]
});
chart.render();

function toggleDataSeries(e) {
if (typeof(e.dataSeries.visible) === “undefined” || e.dataSeries.visible ){
e.dataSeries.visible = false;
} else {
e.dataSeries.visible = true;
}
chart.render();
}

}
</script>
</head>
<body>
<div id=”chartContainer” style=”height: 370px; max-width: 1920px; margin: 0px auto;”></div>
<script src=”../../canvasjs.min.js”></script>
</body>
</html>

However the results are not like the expected

results : https://i.stack.imgur.com/zyIZM.png

Could you please help me figure out how to solve this problem :)

It seems like you are passing date-time in y-values, whereas CanvasJS supports only numeric in axisY. Please refer documentation for more info. However you can workaround this by passing -values as timestamp and format the value in axis-labels and toolTip as shown in below example, here is the JSFiddle for the same.

 var chart = new CanvasJS.Chart("chartContainer", { title: { text: "Machine Operating Time" }, axisY: { minimum: (new Date(2016, 0, 25, 17, 30)).getTime(), interval: (12 * 60 * 60 * 1000), labelFormatter: function(e){ return CanvasJS.formatDate(e.value, "DD-MMM h:mm TT"); } }, toolTip:{ contentFormatter: function ( e ) { return "<strong>Machine " + e.entries[0].dataPoint.label + "</strong></br> Start Time: " + CanvasJS.formatDate(e.entries[0].dataPoint.y[0], "DD-MMM h:mm TT") + "</br> Stop Time: " + CanvasJS.formatDate(e.entries[0].dataPoint.y[1], "DD-MMM h:mm TT"); }}, data: [{ type: "rangeColumn", dataPoints: [ { label: "A", y: [(new Date(2016, 0, 25, 18, 30)).getTime(), (new Date(2016, 0, 26, 11, 00)).getTime()] }, //.getTime() returns timestamp => y-value is numeric { label: "B", y: [(new Date(2016, 0, 26, 12, 00)).getTime(), (new Date(2016, 0, 27, 14, 00)).getTime()] }, { label: "C", y: [(new Date(2016, 0, 27, 3, 30)).getTime(), (new Date(2016, 0, 27, 14, 00)).getTime()] }, { label: "D", y: [(new Date(2016, 0, 27, 12, 00)).getTime(), (new Date(2016, 0, 28, 14, 00)).getTime()] } ] }] }); chart.render(); 
 <script src="http://canvasjs.com/assets/script/canvasjs.min.js"></script> <div id="chartContainer" style="height: 260px; width: 100%;"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM