0

I am using Line chart in which I would like to get Tooltip on Mouse click. Here is the code. Please let me know what I have written wrong so that I am not able to get the tooltip. I don't have any exception.

 public class ChartPlot  
 {
 static LineChart<Number,Number> linechart;
 static double[] xArray,yArray;
 static ArrayList<Double> xList,yList;
 public static  XYChart.Series series;
 public static XYChart.Data<Number, Number> data;

 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++) 
  {
      data = new XYChart.Data<>(xArray[i], yArray[i]);
        series.getData().add(data);           
  }

  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);

     linechart.setOnMouseEntered((MouseEvent event) -> {
         System.out.println("Clicked on Chart");
          Tooltip t = new Tooltip(data.getYValue().toString() + '\n' + data.getXValue().toString());
    Tooltip.install(data.getNode(), t);

    });

     return linechart;

    }
  }

I am using setMouseOnClick() function for the linechart is there any other way that I can do this?

Thanks in advance

  • Let me know if [this](https://stackoverflow.com/questions/25892695/tooltip-on-line-chart-showing-date/25906039) or [this](https://stackoverflow.com/questions/14615590/javafx-linechart-hover-values) helps. – SedJ601 Jun 26 '18 at 13:23
  • That is not how to install a tooltip. You are installing a new tooltip every time you hover the mouse over the chart, thus never actually able to display it. – Zephyr Jun 26 '18 at 14:28
  • Thank you for the link for examples. I have tried it. – DivyaShrungarJ Jun 27 '18 at 04:16
  • But I am getting error in this ....... d.getNode().setOnMouseEntered(event -> d.getNode().getStyleClass().add("onHover")); ------- I am getting null pointer exception. Please suggest to resolve this. – DivyaShrungarJ Jun 27 '18 at 04:17

0 Answers0