javafx 2.2中散点图中数据点的工具提示 [英] Tooltips for datapoints in a scatter chart in javafx 2.2

查看:186
本文介绍了javafx 2.2中散点图中数据点的工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javafx中创建了一个散点图。它包含五个数据系列。我从Arraylist中添加了数据。我需要在每个数据点上显示一个工具提示。要添加工具提示,它说我们需要创建一个节点每个数据点。我们怎么做?

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

推荐答案

来自 XYChart.Data.nodeProperty的API文档 :(由我粗体标记)

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


public final ObjectProperty nodeProperty

public final ObjectProperty nodeProperty

要为
显示此数据项的节点。在将项目添加到图表之前,您可以创建自己的节点并
数据项上设置它。否则,图表
将为您创建一个节点,该节点具有
图表类型的默认表示。只要将数据添加到
图表,就会设置此节点。 然后你可以让它添加鼠标监听器等。图表将最好地定位和调整节点大小,例如
a线或散点图这个节点将是

数据值位置为中心。对于条形图,这个定位并调整大小
作为此数据项的条形。

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.

它应该是最好的让图表设置其默认的节点 s并稍后注册工具提示。像这样:

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

看起来像这样:

关于散点图的好文章:

  • http://docs.oracle.com/javafx/2/charts/scatter-chart.htm

这篇关于javafx 2.2中散点图中数据点的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆