Mariem
Mariem

Reputation: 133

How could I get the exact range value when I select a portion in amcharts?

I'm working on an angular application and I'm using the chart library amcharts. I want to make a selection and get the range of the selected portion. here is the code:

    this.chart.cursor.behavior = "selectX";
    this.chart.cursor.events.on("selectended", function(ev) {
      var range = ev.target.xRange;
      var axis = ev.target.chart.xAxes.getIndex(0);
      var from = (axis.getPositionLabel(axis.toAxisPosition(range.start)));
      var to = (axis.getPositionLabel(axis.toAxisPosition(range.end)));
      alert("Selected from " + from + " to " + to);
    });

the to and from variable are only getting the day not the full date. ps: xaxis is a datetime.

Upvotes: 0

Views: 283

Answers (1)

xorspark
xorspark

Reputation: 16012

You can use the axis' positionToDate method to get the exact Date object value from where the range is drawn:

this.chart.cursor.behavior = "selectX";
this.chart.cursor.events.on("selectended", function(ev) {
  var range = ev.target.xRange;
  var axis = ev.target.chart.xAxes.getIndex(0);
  var from = (axis.positionToDate(axis.toAxisPosition(range.start)));
  var to = (axis.positionToDate(axis.toAxisPosition(range.end)));
  alert("Selected from " + from + " to " + to);
});

Upvotes: 1

Related Questions