[Chartjs]-ChartJS v2: Scale value at click coordinates (time scale)

4👍

This code actually works fine. The only problem was that I was looking at the _i value of the moment object to check it’s value (this is the value used as the initial input when creating the object, not necessarily the current value).

Changing the console.log line to the following yields the expected / correct result:

console.log(event.offsetX, valueX.format('YYYY-MM-DD HH:mm:ss'), null, event.offsetY, valueY);

0👍

If you want to get the nearest x axis value you could do it this way:

onClick: function (event) {

    const activeElements = this.getElementsAtXAxis(event);
    const xval = this.scales['x-axis-0']._timestamps.data[activeElements[0]._index];

    console.log(xval)

}

Leave a comment