gpt4 book ai didi

javascript - AddRows 接收 PHP 数组

转载 作者:行者123 更新时间:2023-12-03 10:54:00 24 4
gpt4 key购买 nike

我正在努力将 PHP 中的数据转化为图表。我在 1 个 php 页面上有以下代码片段。首先是简单的部分:

$sql = "SELECT * FROM (SELECT timestamp, CurrentKelvin, TargetKelvin, WeatherTempKelvin FROM  `rawdata` ORDER BY  `rawdata`.`timestamp` DESC LIMIT 10) AS ttbl ORDER BY `timestamp` ASC;";
$results = mysqli_query($con,$sql);
$ChartData = array();
foreach($results as $result)
{
$ChartData[] = array( (int)$result['CurrentKelvin'],(int)$result['TargetKelvin']);
}
$ChartData = json_encode($ChartData);

然后是 javascript 部分:

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'CurrentKelvin');
data.addColumn('number', 'TargetKelvin');
alert( <?php echo json_encode($ChartData); ?>);

data.addRows( <?php echo json_encode($ChartData); ?> );
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data);
}
</script>

<!--Div that will hold the pie chart-->
<div id="chart_div"></div>

当我运行上述命令时,警报命令显示以下输出:

[[292,290],[292,290],[291,290],[291,290],[291,290],[291,290],[291,290],[291,290],[291,290],[291,290]]

但是 data.addRows 行会生成以下错误:错误:给 addRows 的参数必须是数字或数组

将 data.addRows 更改为简单的内容(并将 data.addColumn 更改为字符串,具有相同的数组结构,我确实得到了一个图表:

data.addRows([
['Ivan', 5],
['Igor', 7],
['Felix', 8],
['Bob', 4]
]);

我就是不明白出了什么问题。如有任何帮助,我们将不胜感激。

最佳答案

迪内什给出的答案有效:

      var json_arr = <?php echo json_encode($ChartData); ?>; 
data.addRows(JSON.parse(json_arr));

关于javascript - AddRows 接收 PHP 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28333537/

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