gpt4 book ai didi

javascript - 使用 chart.js 绘制饼图

转载 作者:行者123 更新时间:2023-11-28 00:51:25 25 4
gpt4 key购买 nike

我正在使用来自 http://www.chartjs.org/ 的 chart.js .

我想画饼图,但是没有显示。我设法绘制了折线图,因此加载了 Javascript。

HTML

<!-- Chart -->
<script type="text/javascript" src="_javascripts/chart/Chart.min.js"></script>
<!-- //Chart-->


<canvas id="browsersChart" width="400" height="100"></canvas>
<script>
var ctx = document.getElementById("browsersChart");
var myChart = new Chart(ctx, {
type: 'pie',
data: {
datasets: [{
label: 'Browsers',
data: [14, 2, 2, 2, 1],
backgroundColor: [
window.chartColors.red,
window.chartColors.orange,
window.chartColors.yellow,
window.chartColors.green,
window.chartColors.blue,
],
}],
labels: [
"Chrome"
"Firefox"
""
"MSIE"
"Mobile Safari"
]
},
options: {
responsive: true
}
};
</script>

最佳答案

您的代码中有一些拼写错误。

在标签部分,每个标签后需要一个逗号:

labels: [
"Chrome", // These commas at the end need added.
"Firefox",
"",
"MSIE",
"Mobile Safari"
]

您还需要在图表声明的最后添加一个):

    options: {
responsive: true
}
}); // The rounded bracket here needs added

将来,您应该在浏览器中使用开发工具。控制台选项卡将告诉您此类错误。它准确地解释了哪一行有错误。

改正这些错别字后,我得到了一个饼图,正如预期的那样。

关于javascript - 使用 chart.js 绘制饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47127272/

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