javascript - 将数据从mysql动态加载到Javascript表
问题描述
我用 JavaScript(Canvas Js) 制作了这张图表
代码如下
<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: "Preventive",
dataPoints: [
{ label: "ABC", y: 2 },
{ label: "DEF", y: 2 },
{ label: "AAA", y: 2 }
]
},
{
type: "spline",
showInLegend: true,
yValueFormatString: "##.00H",
name: "Curative",
dataPoints: [
{ label: "ABC", y: 10 },
{ label: "DEF", y: 2.27 },
{ label: "AAA", y: 1.25 }
]
}]
});
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: 920px; margin: 0px auto;"></div>
<script src="../../canvasjs.min.js"></script>
</body>
</html>
正如您所说,数据是静态的。
但是,当我想用 mysql 查询动态加载数据时
dataPoints: [
<?php
$sq = "select machine,timediff 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'];
?>
{ label: "<?php echo $machine ?>", y: <?php echo $diff ?> }
<?php } ?>
]
页面一片空白,你能帮我找出我哪里出错了吗?
解决方案
推荐阅读
- c++ - 在 Windows 上导致问题的 active-win npm 包,一般如何解决依赖问题,npm?
- python - 在 Python 中绘制频谱图(频率与时间)
- python - 将 Pandas DataFrame 转换为单行 DataFrame,将一列的值保存为标题
- c# - 我将如何制作一个返回数据的函数
- javascript - 无法获取特定频道 - Discord.js v11.6.4
- smtp - 在 Apache/Debian/Linode 上为多个客户网站设置多个联系表格的最佳方式
- python - 努力理解如何在 qtdesigner 设计的选项卡布局中嵌入 matplotlib 小部件图形
- node.js - 无法将应用程序/八位字节流解析为节点 js 中的图像
- .net - 如何验证 devextreme 文本框中的输入格式?
- javascript - 在 `useEffect()` 中手动调用 api