如何从数组中添加特定的标签和值到图表- ChartJS



希望你过得愉快,也希望你能帮助我。

我正试图用ChartJS编码一个甜甜圈图。我有一个数据库,我有一个表称为"Quality"信号。在这个表中,我将有五种情况:无/A,优秀,良好,平均,坏信号。首先,我为每个索引创建一个索引,并计算有多少个,然后将它们存储在一个数组中,每个索引都有一个标签。下面是我的数组:

$dataPoints = array(
array("label" => "Excellent ", "value" => $lqi_excellent),
array("label" => "Good ",      "value" => $lqi_good),
array("label" => "Average ",   "value" => $lqi_average),
array("label" => "Limit ",     "value" => $lqi_limit),
array("label" => "N/A ",       "value" => $lqi_na)
);
下面是我的脚本代码:
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
new Chart(document.getElementById("MyChart"), {
type: 'doughnut',
data: {
labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
datasets: [
{
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
data: [1,2,3,4,5]
}
]
},
options: {
title: {
display: true,
text: 'Predicted world population (millions) in 2050'
}
}
});
</script>

我的目标是能够用数组的值替换标签和数据。请问我该怎么做?

使用foreach创建两个数组

$labelArray = array();
foreach ($dataPointsas as $key => $val) {
$labelArray[$key] = $val['label'];
}

$valueArray = array();
foreach ($dataPointsas as $key => $val) {
$valueArray[$key] = $val['value'];
}

然后使用

labels: $labelArray

data: $valueArray 

相关内容

  • 没有找到相关文章

最新更新