user1519240
user1519240

Reputation: 2354

Echarts: how to get x and y values during mouse events

I have the following chart:

const quotesChart = echarts.init(quotesContainer);
quotesChart.setOption({
  dataset: [
    { source: quotes }
  ],
  xAxis: [
    {
      type: 'category',
    },
  ],
  yAxis: [
    {
      position: 'left',
    }
  ],
  series: [
    {
      name: 'Quotes',
      type: 'line',
      datasetIndex: 0,
    }
  ]
})

quotesChart.getZr().on("mousemove", function(params){
  const pointInPixel = [params.offsetX, params.offsetY];
 
  // how do I get x and y values for current mouse position?

  // I see some examples using convertFromPixel but I don't know how to use these coordinates to get x and y values
  const pointInGrid = quotesChart.convertFromPixel('grid', pointInPixel);
})

The quotes dataset source looks like: [{period: '20/01/2021', value: 15.59}, {period: '21/01/2021', value: 15.99}]

I need to get values from axis x and y (ex: 20/01/2021 and 15.59) but I don't know how, I only have the grid coordinates.

Thanks.

Upvotes: 3

Views: 2410

Answers (2)

Paralosva Rosos
Paralosva Rosos

Reputation: 11

let x = pointInGrid[0];

let y = pointInGrid[1];

Upvotes: 1

user1519240
user1519240

Reputation: 2354

It was simpler than I thought, it turns out pointInGrid[0] is the position of the series array, so I did: quotes[pointInGrid[0]] to get the series item I needed.

Upvotes: 2

Related Questions