【发布时间】:2021-01-29 23:35:57
【问题描述】:
我正在使用ng2-charts 库创建一个使用来自 REST API 的数据进行更新的图表。我使用annotation plugin。注解的数量发生了变化,因此更改现有注解是不够的 - 我需要能够在运行时添加/删除新注解。
当我在开始时向图形选项添加注释时,注释呈现得很好。但是在运行时,更新数据和更新options对象后,就看不到注解了。
稍后,我在图表的选项中添加注释:
this.barChartOptions.annotation.annotations.push(
{
drawTime: 'beforeDatasetsDraw',
type: 'box',
xScaleID: 'x-axis-0',
yScaleID: 'y-axis-0',
xMin: tmpFirstBorder,
xMax: tmpLastBorder,
yMin: 0.5,
yMax: 5,
backgroundColor: 'rgba(71, 158, 245, 0.5)',
borderColor: 'rgb(71,158,245)',
onClick: function (e) {
console.log('Box', e.type, this);
}
}
然后更新数据,以便重新绘制图表:
this.barChartOptions = this.barChartOptions;
this.barChartData = tempChartData;
图表重绘得很好,但没有注释。
我认为可能是图表选项没有更新的问题,所以我尝试添加一个显式更新:
@ViewChild(BaseChartDirective) chart: BaseChartDirective;
[...]
this.chart.chart.update();
但无济于事。
有谁知道如何正确绘制注释?谢谢。
【问题讨论】:
标签: typescript chart.js angular8 ng2-charts