Chart.js 来自注释插件的行没有出现
Chart.js line from annotations plugin is not appearing
在 Angular 应用程序中使用 chart.js 的注释插件,所以如果这有点具体,我很抱歉。注释插件应该做的是在 x 轴上的 value=1 处创建一条垂直线。
然而此时什么也没有出现。没有抛出错误或类似的错误,但没有行。
我的图表上的 x 轴是浮点形式,所以我尝试了 value=1.0
,但是并没有成功。我应该指出,colorschemes 插件运行良好,所以插件也不会以某种方式被禁用。
import * as Chart from 'chart.js';
import 'chartjs-plugin-colorschemes';
import 'chartjs-plugin-annotation';
[...]
let massPopChart = new Chart(this.myChart, {
type: 'bubble',
data: {
labels:['Jobs']
},
options: {
plugins:{
colorschemes: {
scheme: 'brewer.YlOrBr9'
},
annotation: {
annotations: [{
type: 'line',
mode: 'vertical',
scaleID: 'y-axis-0',
value: 1,
borderColor: 'red',
borderWidth: 5,
label: {
enabled: false,
content: 'Test label'
}
}]
}
}, legend: {
display: false
}, title: {
display: true,
text: 'Location Quotient of Jobs in Region'
}, scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: "# of Jobs"
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: "LQ"
}
}]
}
}
});
this.jobTitles.forEach((value) => {
var r = parseInt(this.regionData.merged_titles[value])/100;
if(r < 5) r = 5;
else if (r>15) r = 15;
massPopChart.data.datasets.push({
label: value,
data: [{
x: parseFloat(this.regionData.location_quotient[value]),
y: parseInt(this.regionData.merged_titles[value]),
r: r
}]
});
massPopChart.update();
});
我的图表制作得很好。所有点都出现了,颜色由 colorscheme 插件定义,但是 x 轴上的 1 处没有红色实线。
如果以后有人遇到这个问题,我偶然发现的解决方案是转到 node_modules > @types > chart.js > index.d.ts
并将行 annotation?: Object;
放在第 217 行周围的 interface ChartOptions
部分. 然后我像这样重构了代码...
let massPopChart = new Chart(this.myChart, {
type: 'bubble',
data: {
labels:['Jobs']
},
options: {
plugins:{
colorschemes: {
scheme: 'brewer.YlOrBr9'
}
}, legend: {
display: false
}, title: {
display: true,
text: 'Location Quotient of Jobs in Region'
}, annotation: {
annotations: [{
type: 'line',
mode: 'vertical',
scaleID: 'y-axis-0',
value: 1,
borderColor: 'red',
borderWidth: 5,
label: {
enabled: false,
content: 'Test label'
}
}]
}, scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: "# of Jobs"
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: "LQ"
}
}]
}
}
});
随着 'annotation' 部分从 'plugins:' 部分移动到一般 'options' 部分。
在 Angular 应用程序中使用 chart.js 的注释插件,所以如果这有点具体,我很抱歉。注释插件应该做的是在 x 轴上的 value=1 处创建一条垂直线。
然而此时什么也没有出现。没有抛出错误或类似的错误,但没有行。
我的图表上的 x 轴是浮点形式,所以我尝试了 value=1.0
,但是并没有成功。我应该指出,colorschemes 插件运行良好,所以插件也不会以某种方式被禁用。
import * as Chart from 'chart.js';
import 'chartjs-plugin-colorschemes';
import 'chartjs-plugin-annotation';
[...]
let massPopChart = new Chart(this.myChart, {
type: 'bubble',
data: {
labels:['Jobs']
},
options: {
plugins:{
colorschemes: {
scheme: 'brewer.YlOrBr9'
},
annotation: {
annotations: [{
type: 'line',
mode: 'vertical',
scaleID: 'y-axis-0',
value: 1,
borderColor: 'red',
borderWidth: 5,
label: {
enabled: false,
content: 'Test label'
}
}]
}
}, legend: {
display: false
}, title: {
display: true,
text: 'Location Quotient of Jobs in Region'
}, scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: "# of Jobs"
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: "LQ"
}
}]
}
}
});
this.jobTitles.forEach((value) => {
var r = parseInt(this.regionData.merged_titles[value])/100;
if(r < 5) r = 5;
else if (r>15) r = 15;
massPopChart.data.datasets.push({
label: value,
data: [{
x: parseFloat(this.regionData.location_quotient[value]),
y: parseInt(this.regionData.merged_titles[value]),
r: r
}]
});
massPopChart.update();
});
我的图表制作得很好。所有点都出现了,颜色由 colorscheme 插件定义,但是 x 轴上的 1 处没有红色实线。
如果以后有人遇到这个问题,我偶然发现的解决方案是转到 node_modules > @types > chart.js > index.d.ts
并将行 annotation?: Object;
放在第 217 行周围的 interface ChartOptions
部分. 然后我像这样重构了代码...
let massPopChart = new Chart(this.myChart, {
type: 'bubble',
data: {
labels:['Jobs']
},
options: {
plugins:{
colorschemes: {
scheme: 'brewer.YlOrBr9'
}
}, legend: {
display: false
}, title: {
display: true,
text: 'Location Quotient of Jobs in Region'
}, annotation: {
annotations: [{
type: 'line',
mode: 'vertical',
scaleID: 'y-axis-0',
value: 1,
borderColor: 'red',
borderWidth: 5,
label: {
enabled: false,
content: 'Test label'
}
}]
}, scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: "# of Jobs"
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: "LQ"
}
}]
}
}
});
随着 'annotation' 部分从 'plugins:' 部分移动到一般 'options' 部分。