javascript - 将我的 canvasjs 图表转换为 chart.js 图表
问题描述
我为我的项目制作了一个 canvasjs 图表,但问题是我的试用版在几天后就结束了,而且我对 javascript 还是很陌生。我看到了一个类似的图表制作工具,叫做 chart.js,它是免费的。有人可以帮我将我的 canvasjs 图表转换为 chart.js 图表吗?谢谢你。
<head>
<script type="text/javascript" src="../javascript/canvasjs.min.js"></script>
<script type="text/javascript" src="../javascript/jquery.js"></script>
<script type="text/javascript">
window.onload = function() {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
title: {
text: "Mesures"
},
axisY: {
title: "Temperature (°C) Humidité (%)",
suffix: ""
},
legend: {
cursor: "pointer",
fontSize: 12,
itemclick: toggleDataSeries
},
toolTip: {
shared: true
},
data: [{
type: "spline",
dataPoints: [],
},
{
type: "spline",
dataPoints: [],
}
]
});
$.getJSON("Connexion.php", function(data) {
$.each((data), function(key, value) {
chart.options.data[0].dataPoints.push({
label: value[0],
y: parseInt(value[1])
});
chart.options.data[1].dataPoints.push({
label: value[0],
y: parseInt(value[2])
});
});
chart.render();
updateChart();
});
function updateChart() {
$.getJSON("Connexion.php", function(data) {
chart.options.data[0].dataPoints = [];
chart.options.data[1].dataPoints = [];
$.each((data), function(key, value) {
chart.options.data[0].dataPoints.push({
label: value[0],
y: parseInt(value[1])
});
chart.options.data[1].dataPoints.push({
label: value[0],
y: parseInt(value[2])
});
});
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();
};
setInterval(function() {
updateChart()
}, 1000);
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
</body>
</html>
这是我的 Connexion.php :
<?php
$host = 'localhost';
$bd = 'Fablab';
$login = 'root';
$password = '';
$conn = new mysqli($host, $login, $password, $bd);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Date, Temperature, Humidite FROM Mesures";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$obj = array();
while($row = $result->fetch_assoc()) {
$element = array($row["Date"],$row["Temperature"],$row["Humidite"]);
array_push($obj,$element);
}
echo json_encode($obj);
} else {
echo "0 results";
}
$conn->close();
?>
解决方案
推荐阅读
- android - 在 android 中在哪里准确添加 Okhttp 和改造 proguard 规则?
- matlab - 如何清空一个单元格
- wordpress - 手机版首页出现logo怎么解决?
- spring-boot - 如何在 Spring Boot 中使用存储过程 MySQL?
- unity3d - 如何在Unity中转换坐标?
- python - 使用看门狗库进行文件监控
- prometheus - 如何使用 Prometheus 监控 grpc-java 服务端和客户端?
- r - 自动重复运行一段代码 25 次并整理输出
- c# - 当我在 c# Winforms 中使用 itextsharp 将 html 转换为 PDF 时,引导 css 文件使我的文档变小
- javascript - 基于条件的对象键映射不起作用