如何使用带有chartjs
的NodeJ创建饼图?想使用chartjs
创建不同类型的图表,但是当我尝试运行代码时显示“无法设置宽度属性”
TypeError: Cannot set property 'width' of undefined
at ni.resize (node_modules\chart.js\dist\Chart.min.js:7:93312)
at ni.initialize (node_modules\chart.js\dist\Chart.min.js:7:92818)
at ni.construct (node_modules\chart.js\dist\Chart.min.js:7:92559)
at new ni (node_modules\chart.js\dist\Chart.min.js:7:91964)
at Object.<anonymous> (chartjs\chart.js:7:15)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
我试图运行此代码以生成饼图图像,但发现此错误。
let Canvas = require("canvas"),
canvas = Canvas.createCanvas(400, 400),
ctx = canvas.getContext("2d"),
Chart = require('chart.js'),
fs = require("fs");
var myChart = new Chart(ctx, {
type: "pie",
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "# of Votes",
data: [12, 19, 3, 5, 2, 3],
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)",
"rgba(255, 159, 64, 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)",
"rgba(255, 159, 64, 1)"
],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true
}
}
]
}
}
});
canvas.toBuffer(function(err, buf) {
if (err) throw err;
fs.writeFileSync("chart.png", buf);
});
应该是当前文件夹中的图像