Vue-chartjs: How to access or get value of specific graph on chart plot by click event?

Created on 6 Jul 2018  ยท  7Comments  ยท  Source: apertureless/vue-chartjs

I used vue-chartjs for drawing some chart like line, bar, ...

In my project, there are many cases using specific value or lable of data in chart.
Using tooltip option of vue-chartjs, I can check that value or label of data item when hovered.

I want to know how to access or get information of specific data matched with point or bar in graph when clicked(not hovered).
Here is my code about chart options.

chartOptions: {
responsive: false,
onClick: function(evt){
//Some logic to get value of label of specific data...(type, label, value, ...)
}

Environment

  • vue.js version: 2.5.2
  • vue-chart.js version: 3.3.2
  • npm version: 5.6.0
โ“ question

Most helpful comment

onClick: function (evt, item) {
var label = item[0]['_model'].label
}

All 7 comments

Well it depends on what you want to get.
I am not even sure that there is a global onClick callback in the options.

You have however onClick callbacks for the tooltips and legend items:

https://www.chartjs.org/docs/latest/configuration/legend.html#custom-on-click-actions

This might help you.

onClick: function (evt, item) {
var label = item[0]['_model'].label
}

onClick: function (evt, item) {
var label = item[0]['_model'].label
}

This is for the first dataset, as only the 0th index is considered. How to tackle this problem for chart having multiple datasets?

I also have the same question as @pranavkumar389. What if a particular use case was drilling down to data related to a specific dataset? Is there any way of identifying what dataset id was clicked on?

I have my onclick handler working fine but I can't seem to get props in this method.

For example:

const item = event[0];
const index = item._index;
const hotSpot = item._model.label;
console.log('chart', item, point, hotSpot, index, this.batches);

In the following snippet this.batches returns undefined.

Why is this closed? As said already the example given only references the first data set, How do you determine which dataset has been clicked?

I found a solution as detailed https://stackoverflow.com/questions/51219973/how-to-access-or-get-value-of-specific-graph-on-chart-plot-by-click-event#answer-66280509 using the undocumented (as far as I could see) $data._chart property of the vue component

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kurbar picture kurbar  ยท  4Comments

ihrankouski picture ihrankouski  ยท  3Comments

DavidSotoA picture DavidSotoA  ยท  3Comments

aido179 picture aido179  ยท  3Comments

syxolk picture syxolk  ยท  5Comments