0

I have a stock chart and I would like to get the value shown in the yAxis label that the crosshairs create when clicking in a plot. Essentially I want to know what the price is on the yAxis for the current location of my mouse when I click. So lets say the crosshairs yAxis label is showing 20, when I click, I want to put that 20 into a variable. So how can I "get" the crosshair's yAxis value? Thanks

const yAxis = plot.yAxis();
plot.listen('click', function (e) {

    console.log(e)
    //this will get the first label on the y Axis, this is 12
    //I dont want the yAxis of the chart labels, I want the crosshair yAxis label
    const value = yAxis.scale().ticks().get()[0];
    console.log(value)
    //prints  12

    var value = chart.crosshair().yLabel()
    //I dont know what to query to get the "value" of the label
    console.log(value)
   
});
bigt11
  • 78
  • 10

1 Answers1

0

Yes, it is possible to get yAxis label value.

You can do so by acessing the plot's crosshair object.

//Create variable for label value
  let yAxisLabelValue = 0;

  //Handling click event
  plot.listen("click", (e) => {
    let label = plot.crosshair().yLabel();
    let labelValue = label.text();
    
    //Asigning current value to a variable
    yAxisLabelValue = labelValue;
  });

Feel free to try out on the playground how to interact with a crosshair object.

AnyChart Support
  • 3,770
  • 1
  • 10
  • 16