19

我正在用 JavaFX 创建折线图。目前一切都很好,它成功地用我需要的数据库存储过程中的数据创建了图表。无论如何,如果可能的话,我需要的是 LineChart 上的每个数据点都有一个鼠标悬停事件,它说明特定点后面的值,例如 150,000 英镑。我已经在 PieCharts 上看到了这样的例子,它显示了悬停时的 % 值,但是我在任何地方都找不到 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提供的答案是解决这个问题的完美方案。

谢谢你,珠宝海。

4

2 回答 2

36

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

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

示例代码的输出显示为光标悬停在 22 节点上。

悬停节点

于 2013-01-31T10:21:53.457 回答
5

使用工具提示:

import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication250 extends Application
{

    @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<Number, Number> series = new XYChart.Series();
        series.setName("My portfolio");
        //populating the series with data
        Random rand = new Random();

        TreeMap<Integer, Integer> data = new TreeMap();
        //Create Chart data
        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()));//Add data to series
        }

        lineChart.getData().add(series);

        //loop through data and add tooltip
        //THIS MUST BE DONE AFTER ADDING THE DATA TO THE CHART!
        for (Data<Number, Number> entry : series.getData()) {                
            System.out.println("Entered!");
            Tooltip t = new Tooltip(entry.getYValue().toString());
            Tooltip.install(entry.getNode(), 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);
    }    
}
于 2016-11-04T21:21:22.710 回答