gpt4 book ai didi

javascript - 如何用mysql数据填充Charts.js?

转载 作者:可可西里 更新时间:2023-11-01 08:28:31 25 4
gpt4 key购买 nike

下面是我的代码,我希望在 xaxis 和 yaxis 中显示来自数据库的数据。

 <script src="../Chart.js"></script>
<script>
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};

var barChartData = {
labels : <?=json_encode(array_values($count));?>,
datasets : [

{
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,0.8)",
highlightFill : "rgba(151,187,205,0.75)",
highlightStroke : "rgba(151,187,205,1)",
data :<?=json_encode(array_values($auditor));?>
}
]

}
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartData, {
responsive : true
});
}

</script>

我的 php 代码是这样的:

<?php

$link = mysql_connect('localhost', 'root', '')
or die('Could not connect: ' . mysql_error());

mysql_select_db('laravel') or die('Could not select database');

$auditor = array();
$sql = "SELECT DATE(created) AS date, COUNT(auditor_id) AS 'count' FROM auditor WHERE created BETWEEN '2015-09-01 00:00:00' AND '2015-09-31 23:59:59' GROUP BY date ORDER BY date";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$date = $row["date"];
$count = $row["count"];
//add to data array
$auditor[$date] = $count;
}
}

基本上我希望这些分组后的数据显示在X轴和Y轴上

最佳答案

Just change this line

<?=json_encode(array_values($count));?>

to

<?=json_encode(array_keys($auditor));?>

关于javascript - 如何用mysql数据填充Charts.js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32690648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com