javascript - Chart js:我的饼图和甜甜圈上的标签越过了。无法获取图表本身
问题描述
我正在尝试用 vue js 实现图表。我得到的图表本身没有图表,但标签交叉。我不知道出了什么问题。
这应该是我的饼图。如您所见,我将标签交叉,这意味着删除所有标记的数据,因此给了我一个空图表。这是我的代码。
export const productChartData = {
type: 'pie',
data: {
labels: ['Purchased', 'Return', 'Cancelled', 'Shipped', 'Returned'],
datasets:[
{
data: ['22, 33, 12, 18, 19'],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)'
],
borderWidth: 1
}
]
},
options:{
maintainAspectRatio:false,
title:{
display:true,
fontSize:18,
text: "Products"
}
}
}
export default productChartData;
在我的 .vue 文件中,
methods: {
createChart(chartId, chartData){
const canvas = document.getElementById(chartId);
const ctx = canvas.getContext('2d');
const myChart = new Chart(ctx, {
type: chartData.type,
data: chartData.data,
options: chartData.options
});
}
}
解决方案
您的图表呈现代码中存在问题:
data: ['22, 33, 12, 18, 19']
应该作为data: [22, 33, 12, 18, 19]
. 我已经修改了笔并且它正在工作-> https://codepen.io/anon/pen/dwKjqM
推荐阅读
- node.js - 参数无效时axios IF语句不会返回错误
- javascript - 如何在点击时自动打开相机。角2+
- asp.net-core - 如何在 IIS 和 Azure 应用服务中处理客户端的大量下载(每个客户端大约 150 MB)
- python - 如何设置未知数量的 elif 语句
- python - 跨所有应用程序的 Kivy 参考变量
- node.js - 在所有 CPU 内核上运行 NodeJS 以实现最大的硬件潜力
- c++ - ImGui 读取然后返回 const char
- r - 循环和 If 语句以填充 R 中的主位置
- ruby-on-rails - 为什么我在设计上没有看到闪光警报消息
- karate - 使用 karateui 在 iframe 中上传文件