crashwap
crashwap

Reputation: 3062

Get position in array element

In JavaScript, I am unable to figure out how to use the data object in the function below to get the position of the clicked-on data point (e.g. third data point in the series).

Using chartsNew.js, a popular fork of charts.js, this code shows the value of the datapoint at the mouse click:

function fnMouseDownLeft(event,ctx,config,data,other){
    if(other != null){
        window.alert("["+  data.labels[other.v12]+",  "+data.datasets[other.v11].data[other.v12]  +"]");
    }else{
        window.alert("You click Left but not on a data");
    }
}

How do I display the clicked element's position in the data series?

jsFiddle Example

This seems the most promising but I don't understand the relationship between data.datasets, data.datasets[other] and data.datasets[other].data[other]

window.alert("Position: " + data.datasets[other.v11].data[other.v3] );

Here is documentation:

https://github.com/FVANCOP/ChartNew.js/wiki/100_095_Mouse_Actions

https://github.com/FVANCOP/ChartNew.js/wiki/120_Template_variables#inGraphDataTmpl-annotateLabel

My confusion: v12 (for a line chart) should display the position of data in the series (which is what I want) but instead it displays the x-axis value for that datapoint.

Upvotes: 1

Views: 82

Answers (1)

Serhiy
Serhiy

Reputation: 2555

other.v12 seems to do the trick

alert(other.v12);

http://jsfiddle.net/wesn0xm5/1/

Not sure why it's not giving you the series, it does for me.

Upvotes: 1

Related Questions