amcharts / amcharts5

The newest, fastest, and most advanced amCharts charting library for JavaScript and TypeScript apps.
Other
346 stars 92 forks source link

Get date from X Axis with Line Chart #1556

Closed huy-jack closed 2 months ago

huy-jack commented 3 months ago

Question How to get the x position to get Date when clicking on the Line Chart

    this.chart.events.on('click', (event) => {
      let x = event.target.getPrivate('x'); // return undefined, there is no option "positionX" for getPrivate function, just "x"
      let dateX = this.xAxis.positionToDate(x);
    });

I also tried this but it still does not work

    cursor.events.on('click', (ev) => {
      let x = (ev.target as any).getPrivate('positionX'); // return undefined
      let dateX = this.xAxis.positionToDate(x);
    });

App info: Angular 16, Amchart 5

martynasma commented 3 months ago

The idea is to track cursor position using cursormoved event by logging it in some interval variable.

Then, when click event occurs, you can refer to that variable to know where the click occurred.

var dataItem;
cursor.events.on("cursormoved", (ev) => {
  var positionX = ev.target.getPrivate("positionX");
  var x = xAxis.toAxisPosition(positionX);
  var dataItem = xAxis.getSeriesItem(series, x);
});

chart.events.on('click', (event) => {
  console.log(dataItem);
});
github-actions[bot] commented 2 months ago

This issue is stale because it has been open 30 days with no activity. It will be closed in 5 days unless a new comment is added.