JavaFX LineCharthover值

我正在使用JavaFX创build折线图。 目前所有的都是好的,它成功地创build了我需要从数据库存储过程中获得的数据的图表。 无论如何,如果可能的话,LineChart上的每个数据点都有一个鼠标hover事件,它指定特定点后面的值,例如150,000英镑。 我已经在PieCharts上看到了这个例子,它显示了hover时的%值,但是我找不到LineCharts的任何地方的例子,甚至可以这样做吗?

任何人都可以指出我在正确的方向吗?

目前代码:

private static final String MINIMIZED = "MINIMIZED"; private static final String MAXIMIZED = "MAXIMIZED"; private static String chartState = MINIMIZED; // 12 Month Sales Chart XYChart.Series<String, Number> series = new XYChart.Series<>(); XYChart.Series<String, Number> series2 = new XYChart.Series<>(); public void getDeltaData() { try { Connection con = DriverManager.getConnection(connectionUrl); //Get all records from table String SQL = ""; Statement stmt = con.createStatement(); //Create the result set from query execution. ResultSet rs = stmt.executeQuery(SQL); while (rs.next()) { series.getData().add(new XYChart.Data<String, Number>(rs.getString(1), Double.parseDouble(rs.getString(7)))); series2.getData().add(new XYChart.Data<String, Number>(rs.getString(1), Double.parseDouble(rs.getString(8)))); } rs.close(); stmt.close(); } catch (Exception e) { } yearChart = createChart(); } protected LineChart<String, Number> createChart() { final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(); // setup chart series.setName("Target"); series2.setName("Actual"); xAxis.setLabel("Period"); yAxis.setLabel("£"); yearChart.getData().add(series); yearChart.getData().add(series2); yearChart.setCreateSymbols(false); return yearChart; } 

jewelsea提供的答案是解决这个问题的完美scheme。

谢谢你,jewelsea。

使用XYChart.Data.setNode(hoverPane)为每个数据点显示一个自定义节点。 使hoverNode成为一个像StackPane一样的容器。 添加鼠标事件侦听器,以便您知道鼠标何时进入和离开节点。 input时,为hoverPane中的值放置一个Label 。 退出时,从hoverPane中删除标签。

有一些示例代码来演示这种技术。

显示示例代码的输出,光标hover在22节点上。

hoverednode

使用工具提示:

 @Override public void start(Stage stage) { stage.setTitle("Line Chart Sample"); //defining the axes final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); xAxis.setLabel("Number of Month"); //creating the chart final LineChart<Number,Number> lineChart = new LineChart<>(xAxis,yAxis); lineChart.setTitle("Stock Monitoring, 2010"); //defining a series XYChart.Series series = new XYChart.Series(); series.setName("My portfolio"); //populating the series with data Random rand = new Random(); TreeMap<Integer, Integer> data = new TreeMap(); for(int i = 0; i < 3; i++) { data.put(rand.nextInt(51), rand.nextInt(51)); } Set set = data.entrySet(); Iterator i = set.iterator(); while(i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.println(me.getKey() + " - " + me.getValue()); series.getData().add(new XYChart.Data(me.getKey(), me.getValue())); } lineChart.getData().add(series); Set<Node> node = lineChart.lookupAll(".default-color0.chart-line-symbol.series0."); node.forEach((element) -> { element.setOnMouseEntered((MouseEvent event1) -> { double x = event1.getScreenX(); double y = event1.getScreenY(); List keys = new ArrayList(data.keySet()); System.out.println("over value!"); if (event1.getSource().toString().contains("data0")) { Tooltip t = new Tooltip(data.get(Integer.parseInt(keys.get(0).toString())).toString()); Tooltip.install(element, t); } else if (event1.getSource().toString().contains("data1")) { Tooltip t = new Tooltip(data.get(Integer.parseInt(keys.get(1).toString())).toString()); Tooltip.install(element, t); } else if (event1.getSource().toString().contains("data2")) { Tooltip t = new Tooltip(data.get(Integer.parseInt(keys.get(2).toString())).toString()); Tooltip.install(element, t); } }); }); Scene scene = new Scene(lineChart,800,600); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); }