Starting from this question JavaFX LineChart Hover Values I modified the Gist in the first answer https://gist.github.com/jewelsea/4681797 into this
public class TestCharts extends Application
{
ObservableList<XYChart.Data<Integer, Integer>> _serie;
@SuppressWarnings("unchecked")
@Override
public void start(Stage stage) {
LineChart lineChart = new LineChart(new NumberAxis(), new NumberAxis());
XYChart.Series serie = new XYChart.Series(
"Test",
getTestValues()
);
lineChart.getData().add(serie);
lineChart.setCursor(Cursor.CROSSHAIR);
lineChart.setTitle("First Chart");
stage.setTitle("First Chart");
stage.setScene(new Scene(lineChart, 500, 400));
stage.show();
secondHandChart();
}
private void secondHandChart() {
LineChart lineChart = new LineChart(new NumberAxis(), new NumberAxis());
XYChart.Series serie = new XYChart.Series(
"Test",
getTestValues()
);
lineChart.getData().add(serie);
lineChart.setCursor(Cursor.CROSSHAIR);
lineChart.setTitle("Second Chart");
Stage stage = new Stage();
stage.setTitle("Second Chart");
stage.setScene(new Scene(lineChart, 500, 400));
stage.show();
}
private ObservableList<XYChart.Data<Integer, Integer>> getTestValues() {
return plot(23, 14, 15, 24, 34, 36, 22, 45, 43, 17, 29, 25);
}
public ObservableList<XYChart.Data<Integer, Integer>> plot(int... y) {
if (_serie == null) {
_serie = FXCollections.observableArrayList();
int i = 0;
while (i < y.length) {
XYChart.Data<Integer, Integer> data = new XYChart.Data<>(i + 1, y[i]);
data.setNode(
new HoveredThresholdNode(
(i == 0) ? 0 : y[i - 1],
y[i]
)
);
_serie.add(data);
i++;
}
return _serie;
}
else {
return FXCollections.observableArrayList(_serie);
}
}
/**
* a node which displays a value on hover, but is otherwise empty
*/
class HoveredThresholdNode extends StackPane
{
HoveredThresholdNode(int priorValue, int value) {
setPrefSize(8, 8);
final Label label = createDataThresholdLabel(priorValue, value);
setOnMouseEntered(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent mouseEvent) {
getChildren().setAll(label);
setCursor(Cursor.NONE);
toFront();
}
});
setOnMouseExited(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent mouseEvent) {
getChildren().clear();
setCursor(Cursor.CROSSHAIR);
}
});
}
private Label createDataThresholdLabel(int priorValue, int value) {
final Label label = new Label(value + "");
label.setStyle("-fx-font-size: 20; -fx-font-weight: bold;");
label.setMinSize(Label.USE_PREF_SIZE, Label.USE_PREF_SIZE);
return label;
}
}
public static void main(String[] args) { launch(args); }
}
This class has the only pourpose of showing my problem. I want to get two charts of the same serie, getTestValues(). The problem is: the second chart is perfect (2° Img) but the first one (1° Img) is missing the HoveredThresholdNode and when I resize the first chart, the HoveredThresholdNodes in the second chart are changing position (3° Image). How can I get a copy of the serie, containing also a copy of those nodes? I know that printing the same serie doesn't make sense, but in the reality I have a two lists of serie, and I have a chart for each and a chart with the two lists together for compairing pourposes. Anyway the problem can be semplified to just one serie in two charts.