-1

I have an ng2-chart bar graph displaying on my page but I'm looking to get the value of that bar e.g. 5, 7, 9 .

I can get the label name...but can't get the value.

Had anyone done this?

  public chartClicked(e:any):void {
       //e.active[0]._model.label gives the label.
    console.log(e);
  }
Andy
  • 61,948
  • 13
  • 68
  • 95
thegunner
  • 6,883
  • 30
  • 94
  • 143

2 Answers2

2

You can use this solution:

lineChartData: Is your data source filled from your API, So you must add the necessary data that you will use when you click.

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);
    }
   }}
Learning Always
  • 1,563
  • 4
  • 29
  • 49
0

You could try this:

e.active[0]._chart.tooltip._model.dataPoints[0].yLabel
wscourge
  • 10,657
  • 14
  • 59
  • 80
i.igor
  • 1
  • 2