单击条形后,在ng2图表条形图中获取条形图的值



我的页面上显示了一个 ng2 图表条形图,但我希望获得该条形的值,例如 5、7、9 .

我可以得到标签名称...但无法获取值。

有人这样做过吗?

  public chartClicked(e:any):void {
       //e.active[0]._model.label gives the label.
    console.log(e);
  }

您可以使用此解决方案:

lineChartData:你的数据源是从你的API填充的,所以你必须添加点击时会用到的必要数据。

public chartClicked(e: any): void {
 if (e.active.length > 0) {
  const chart = e.active[0]._chart;
  const activePoints = chart.getElementAtEvent(e.event);
    if ( activePoints.length > 0) {
      const clickedElementIndex = activePoints[0]._index;
      const label = chart.data.labels[clickedElementIndex];
      console.log("serie from your dataset = " + activePoints[0]._model.datasetLabel);
      console.log("dataset index = " + activePoints[0]._datasetIndex);
      console.log("serie id from your data source = " + this.lineChartData[activePoints[0]._datasetIndex].labelId);
      console.log("serie from your data source = " + this.lineChartData[activePoints[0]._datasetIndex].label);
      console.log("label from your dataset = " + label);
    }
   }}

你可以试试这个:

e.active[0]._chart.tooltip._model.dataPoints[0].yLabel

相关内容

  • 没有找到相关文章

最新更新