I'm trying to add tooltips to the different nodes on a JavaFX line chart. My chart has two different series; each series has multiple data points. Each data point relates to an object. I need to access the datapoint's associated object in order to add information to its tooltip.
From what I understand, the tooltips must be installed after the data has been added to the chart. I have created a HashMap that tracks the added data points against the relevant object so I can retrieve the object after the data points are added to the chart.
The following is an example of how I add data to a series:
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));
After running lineChart.getData().addAll(series1, series2);
I loop through the chart's series and each series respective XYChart.Data
. I then try to match this data with the data in my HashMap
so I'm able to pull out the Question
which contains data I want to provide to the tooltip. The following is a code snippet demonstrating this:
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);
}
}
}
I have no idea why I'm unable to match the value against my Hashmap. I have tried all sorts of things, such as comparing against the XY.Data.getNode()
method but this does not provide any matches either. I have confirmed that my HashMap is being populated with references to XY.Data objects. What am I doing wrong here?
Edit: As Sedrick pointed out, I was adding a new
instance of an XYChart.Data object, rather than providing a reference to the object I was adding to the HashMap, don't know how I missed this!