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

Javascript 单击条形图后,获取ng2图表条形图中条形图的值,javascript,ng2-charts,Javascript,Ng2 Charts,我有一个ng2图表条形图显示在我的页面上,但我希望得到该条形图的值,例如5、7、9 我可以获取标签名称…但无法获取值 有人这样做过吗 public chartClicked(e:any):void { //e.active[0]._model.label gives the label. console.log(e); } 你可以试试这个: e.active[0]._chart.tooltip._model.dataPoints[0].yLabel 您可以使用此

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

我可以获取标签名称…但无法获取值

有人这样做过吗

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

你可以试试这个:

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

您可以使用此解决方案:

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);
    }
   }}