使用canvasjs在图表中显示y轴

xkrw2x1b  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(370)

我做了一个图表,显示机器和它们被固定的时间(机器x,时间y)数据通过mysql动态加载,如下所示

<?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>

但结果与预期不符
结果:https://i.stack.imgur.com/zyizm.png
你能帮我找出解决这个问题的方法吗:)

yhxst69z

yhxst69z1#

看起来您是在用y值传递日期和时间,而canvasjs在axisy中只支持数字。有关更多信息,请参阅文档。但是,您可以通过传递-值作为时间戳来解决这个问题,并在轴标签和工具提示中设置值的格式,如下面的示例所示,这里是相同的jsfiddle。

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>

相关问题