簡體   English   中英

在JavaFX折線圖節點上設置工具提示

[英]Setting tooltips on JavaFX line chart node

我正在嘗試將工具提示添加到JavaFX折線圖上的不同節點。 我的圖表有兩個不同的系列。 每個系列都有多個數據點。 每個數據點都與一個對象相關。 我需要訪問數據點的關聯對象,以便將信息添加到其工具提示中。

據我了解,必須在將數據添加到圖表之后安裝工具提示。 我創建了一個HashMap,用於跟蹤針對相關對象添加的數據點,以便在將數據點添加到圖表之后檢索該對象。

以下是如何向系列添加數據的示例:

dataToAdd = new XYChart.Data(Integer.toString(count), timeSpent);
addedData.put(dataToAdd, object); // Store a reference in a HashMap<XYChart.Data, DataObject>
series1.getData().add(new XYChart.Data(Integer.toString(count), timeSpent));

運行lineChart.getData().addAll(series1, series2); 我遍歷圖表的系列,每個系列分別XYChart.Data 然后,我嘗試將此數據與我的HashMap的數據進行匹配,以便能夠提取出包含要提供給工具提示的數據的Question 以下是演示此代碼段:

for (XYChart.Series<String, Number> s : lineChart.getData()) {
    for (XYChart.Data<String, Number> d : s.getData()) {
        // This if statement never evaluates to true
        if (addedData.containsKey(d)) {
            Tooltip tooltip = new Tooltip();
            tooltip.setText("Difficulty: " + addedData.get(d).difficulty.toString());
            Tooltip.install(d.getNode(), tooltip);
        }
    }
}

我不知道為什么我無法將值與我的Hashmap相匹配。 我已經嘗試過各種方法,例如與XY.Data.getNode()方法進行比較,但這也不提供任何匹配。 我已經確認我的HashMap正在使用對XY.Data對象的引用進行填充。 我在這里做錯了什么?

編輯:正如Sedrick所指出的,我正在添加XYChart.Data對象的new實例,而不是提供對我添加到HashMap中的對象的引用,不知道我是怎么錯過的!

查看您的代碼,您的問題是

listeningSeries.getData().add(new XYChart.Data(Integer.toString(listeningQuestionCount), timeSpent));

專注於

new XYChart.Data(Integer.toString(listeningQuestionCount), timeSpent)

該代碼應為

listeningSeries.getData().add(dataToAdd);

在第一種情況下,您擁有此功能。 在其他情況下,您不需要。 我個人將擺脫HashMap並使用setExtraValue() ,如下面的示例所示。

我不確定我的問題是否正確,但是您可以做的一件事是使用setExtraValue()將關聯數據添加到節點。 這樣,您將不必依賴HashMap 下面的代碼演示了如何使用setExtraValue()添加額外的數據。

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

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

        //When you create the data points for the series, go ahead and add any assoicated data to the data point.
        int counter = 1;
        Iterator i = set.iterator();
        while (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            System.out.println(me.getKey() + " - " + me.getValue());
            XYChart.Data tempData = new XYChart.Data(me.getKey(), me.getValue());
            tempData.setExtraValue("I am data point " + counter++);
            series.getData().add(tempData);//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.getExtraValue().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);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM