gpt4 book ai didi

javascript - Chartjs Array.join

转载 作者:行者123 更新时间:2023-11-27 23:40:41 25 4
gpt4 key购买 nike

您好,我正在为我的服务器监控创建一些图表。我使用 Php 获取数据并将其作为数组传递给 javascript:

<script>


var randomScalingFactor = function(){ return Math.round(Math.random()*100)};

var obj = JSON.parse('<?php echo json_encode($names) ?>');
var datenbarchart = obj.join(",");
var obj1 = JSON.parse('<?php echo json_encode($nameserver) ?>');

var barChartData = {
labels : [ obj1[0], obj1[1], obj1[2], obj1[3], obj1[4], obj1[5], obj1[6]],
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 : [datenbarchart]
}
]

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

</script>

问题部分是,我尝试使用 join 方法分隔第一个数组,以获得由 "," 分隔的字符串。 :

var datenbarchart = obj.join(",");

当我将其传递给 Chartjs 数据时,通常数据看起来像这样:data: [60,50,40,50]它在图表中不显示任何数据。难道不能用字符串来做到这一点吗,因为它需要每个小节的整个字符串?

最佳答案

根据http://www.chartjs.org/docs/数据字段是一个数组,因此您的语法应该是:

var datenbarchart = obj;

...

var barChartData = {
labels : [ obj1[0], obj1[1], obj1[2], obj1[3], obj1[4], obj1[5], obj1[6]],
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 : datenbarchart
}
]

}

关于javascript - Chartjs Array.join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689323/

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