Tooltips for datapoints in a scatter chart in java

2019-08-25 08:40发布

I have created a scatter chart in javafx.It consists of five series of data.I added data from an Arraylist.I need to show a tooltip on every datapoint.To add tooltip it says that we need to create a node for every datapoint.How can we do that?

I tried creating node while adding data but i didnot succeed. I also tried accessing data from scatter chart and assigning it to a new series.even that didnot work.Can someone please help me retrieve details or datapoints from a scatter chart and create a node to each datapoint. Thanks in Advance

2条回答
淡お忘
2楼-- · 2019-08-25 08:52

From the API doc of XYChart.Data.nodeProperty: (bold marking by me)

public final ObjectProperty nodeProperty

The node to display for this data item. You can either create your own node and set it on the data item before you add the item to the chart. Otherwise the chart will create a node for you that has the default representation for the chart type. This node will be set as soon as the data is added to the chart. You can then get it to add mouse listeners etc. Charts will do their best to position and size the node appropriately, for example on a Line or Scatter chart this node will be positioned centered on the data values position. For a bar chart this is positioned and resized as the bar for this data item.

It should probably be best to let the chart set its default Nodes and register Tooltips later. Like this:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;

public class SO extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        NumberAxis xa = new NumberAxis(0, 10, 1);
        NumberAxis ya = new NumberAxis(0, 100, 10);
        xa.setLabel("x");                
        ya.setLabel("x*x");
        final ScatterChart<Number,Number> chart = new ScatterChart<Number,Number>(xa,ya);
        chart.setTitle("Square");

        XYChart.Series<Number,Number> series1 = new XYChart.Series<>();
        series1.setName("square");
        for (double x=0; x<10; x++) {
            XYChart.Data<Number, Number> d = new XYChart.Data<Number, Number>(x, x*x);
            series1.getData().add(d);
        }

        chart.getData().add(series1);
        Scene scene  = new Scene(chart, 400, 300);
        stage.setScene(scene);
        stage.show();

        for (XYChart.Series<Number, Number> s : chart.getData()) {
            for (XYChart.Data<Number, Number> d : s.getData()) {
                Tooltip.install(d.getNode(), new Tooltip(
                        String.format("%2.1f ^ 2 = %2.1f", 
                                d.getXValue().doubleValue(), 
                                d.getYValue().doubleValue())));
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Looks like this:

enter image description here

Nice article about scatter charts:

查看更多
Ridiculous、
3楼-- · 2019-08-25 08:53

After adding data to bar chart

chart.getData().add(series);

add the following snippet to the code

for (final Series<String, Number> series : chart.getData()) {
        for (final Data<String, Number> data : series.getData()) {
            Tooltip tooltip = new Tooltip();
            tooltip.setText(data.getYValue().toString());
            Tooltip.install(data.getNode(), tooltip);
        }
    }
查看更多
登录 后发表回答