首页 > 解决方案 > AmCharts Legend,如何在图表中的数据更改上重新单击未单击的图例

问题描述

我有一个来自 Am 图表的不同堆积条形图 ,我为其构建了自定义过滤器,过滤器通过用新的预先准备好的 JSON 数据替换整个 amChart.data 来更改数据。我正在使用带有打字稿且没有后端的 Angular 8,JSON 数据保存在资产中并由服务加载,然后传递到图表。

这些是相关的代码片段,不幸的是我不能分享整个代码。

 @Input() dataToDisplay: EventEmitter<any>;

 private amChart: any;

constructor(private wh: WarehouseService) {}

ngOnInit() {
 this.currentYearDisplayed.subscribe(async year => {
  await this.wh.loadData(year);
  this.loadDataIntoChart();
});
this.dataToDisplay.subscribe(data => {
  if (this.amChart) {
    this.amChart.data = data;
    this.amChart.invalidateData();
  }
});

}

  loadDataIntoChart() {
    if (this.amChart) {
      this.amChart.data = this.wh.filteredSet;
    }
  }

初始图表如下所示

我取消点击 Legend Aufwand,深蓝色线条消失。我单击一个更改数据并重新加载的过滤器(而 Aufwand Legend 仍未被点击)

然后图表看起来像这样

我的图例代码是

   // Legend
    this.amChart.legend = new am4charts.Legend();
    this.amChart.legend.position = "right";
    this.amChart.legend.width = 100;

我一直在尝试做的只是在加载数据之前重新单击所有关于数据更改的非活动图例,我在文档中或在 stackoverflow 中找不到正确的设置。

任何帮助表示赞赏,并提前感谢您。

标签: angulartypescriptamchartsamcharts4legend-properties

解决方案


您可以通过 API 调用show()对象上的方法来取消隐藏隐藏的系列。

以下将取消隐藏所有系列:

this.amChart.series.each(function(series) {
  series.show();
});

推荐阅读