0

I am using JavaFX LineChart. I am trying to set the tooltip on the linechart. I am getting error runtime error in event handle. What is wrong in the code?

public class ChartPlot 
{
    static LineChart <Number,Number> linechart;
    static double[] xArray, yArray;
    static ArrayList <Double> xList, yList;
    public static XYChart.Series series;
    public static LineChart linePlot(double[] x, double[] y) {
        xArray = new double[x.length];
        yArray = new double[y.length];

        xArray = x;
        yArray = y;

        //Defining the x axis             
        final NumberAxis xAxis = new NumberAxis();
        xAxis.setLabel("Wavelength");

        //Defining the y axis   
        final NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("Intensity");

        //Creating the line chart 
        linechart = new LineChart <>(xAxis, yAxis);

        linechart.getData().clear();
        //Prepare XYChart.Series objects by setting data 
        series = new XYChart.Series();

        //Setting the data to Line chart  
        for (int i = 0; i < xArray.length; i++) {
            series.getData().add(new XYChart.Data(xArray[i], yArray[i]));
        }

        linechart.setCreateSymbols(false);

        linechart.getData().add(series);

        xAxis.setAutoRanging(true);
        xAxis.setForceZeroInRange(false);
        yAxis.setAutoRanging(true);
        yAxis.setForceZeroInRange(false);
        linechart.autosize();
        linechart.applyCss();
        String css = FXMLDocumentController.class.getResource("LSG.css").toExternalForm();
        linechart.getStylesheets().add(css);
        linechart.setLegendVisible(false);

        for (XYChart.Series <Number, Number> s: linechart.getData()) {
            for (XYChart.Data <Number, Number> d: s.getData()) {
                Tooltip.install(d.getNode(), new Tooltip(
                d.getXValue().toString() + "," + d.getYValue()));

                //Adding Class on Hover or on click
                d.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler < MouseEvent > () //----- This is the line I am getting error
                {@Override
                    public void handle(MouseEvent event) {
                        System.out.println("X :" + d.getXValue() + " Y :" + d.getYValue());
                    }
                });
            }
        }

        return linechart;

    }
}

I am using Open button to read a file and plot. While plotting I am trying to insert listener also to show the value on which I click.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    And what is the error you're getting? Post your exception stack trace. – Zephyr Jun 23 '18 at 06:02
  • Get some ideas from [here](https://stackoverflow.com/questions/14615590/javafx-linechart-hover-values/40431880#40431880) – SedJ601 Jun 23 '18 at 06:25

0 Answers0