我正在使用Chart.js制作一个折线图,该折线图最初所有的数据点都为null
。我尝试一次更新一个点,具体取决于renovation_val
。这是导致错误的行:
line_chart.data.datasets.data[0] = 2500*1.2*mults;
该错误表明data
数组未定义,但我不确定如何解决。
JavaScript代码:
line_chart = new Chart(profit_chart, {
type: 'line',
data:{
labels:['$2,500','$5,000','$7,500','$10,000','$12,500','$15,000','$17,500','$20,000','$22,500','$25,000','$27,500','$30,000','$32,500','$35,000','$37,500','$40,000','$42,500','$45,000','$47,500','$50,000'],
datasets:[{
label:'Profit ($)',
data: [null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],
backgroundColor: 'rgba(255, 255, 255, 0.2)',
borderColor: '#333',
}],
},
options:{
tooltips:{
enabled:false
},
title:{
fontFamily: 'Nunito',
},
legend: {
display: false,
labels: {
fontColor: "#000",
fontSize: 18
},
},
scales:{
xAxes: [{
gridLines: {
display: false,
lineWidth: 2,
},
ticks: {
fontFamily: 'Nunito',
fontColor: "#000", // this here
autoSkip:false,
},
scaleLabel: {
display: true,
labelString: "Budget",
fontFamily: 'Nunito',
fontColor: "#000", // this here
},
}],
yAxes: [{
gridLines: {
display: false,
lineWidth: 2,
},
ticks: {
fontFamily: 'Nunito',
beginAtZero: true,
fontColor: "#000", // this here
},
scaleLabel: {
display: true,
labelString: "Profit",
fontFamily: 'Nunito',
fontColor: "#000", // this here
},
}],
},
}
});
switch(renovation_val) {
case 2500:
line_chart.data.datasets.data[0] = 2500*1.2*mults;
line_chart.update();
break;
};