Last active
November 11, 2024 20:45
-
-
Save jewelsea/4681797 to your computer and use it in GitHub Desktop.
JavaFX sample to display a LineChart which shows the value of a plotted Node when you hover over the Node.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.collections.*; | |
import javafx.event.EventHandler; | |
import javafx.scene.*; | |
import javafx.scene.chart.*; | |
import javafx.scene.control.Label; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.paint.Color; | |
import javafx.stage.Stage; | |
/** Displays a LineChart which displays the value of a plotted Node when you hover over the Node. */ | |
public class LineChartWithHover extends Application { | |
@SuppressWarnings("unchecked") | |
@Override public void start(Stage stage) { | |
final LineChart lineChart = new LineChart( | |
new NumberAxis(), new NumberAxis(), | |
FXCollections.observableArrayList( | |
new XYChart.Series( | |
"My portfolio", | |
FXCollections.observableArrayList( | |
plot(23, 14, 15, 24, 34, 36, 22, 45, 43, 17, 29, 25) | |
) | |
) | |
) | |
); | |
lineChart.setCursor(Cursor.CROSSHAIR); | |
lineChart.setTitle("Stock Monitoring, 2013"); | |
stage.setScene(new Scene(lineChart, 500, 400)); | |
stage.show(); | |
} | |
/** @return plotted y values for monotonically increasing integer x values, starting from x=1 */ | |
public ObservableList<XYChart.Data<Integer, Integer>> plot(int... y) { | |
final ObservableList<XYChart.Data<Integer, Integer>> dataset = FXCollections.observableArrayList(); | |
int i = 0; | |
while (i < y.length) { | |
final XYChart.Data<Integer, Integer> data = new XYChart.Data<>(i + 1, y[i]); | |
data.setNode( | |
new HoveredThresholdNode( | |
(i == 0) ? 0 : y[i-1], | |
y[i] | |
) | |
); | |
dataset.add(data); | |
i++; | |
} | |
return dataset; | |
} | |
/** a node which displays a value on hover, but is otherwise empty */ | |
class HoveredThresholdNode extends StackPane { | |
HoveredThresholdNode(int priorValue, int value) { | |
setPrefSize(15, 15); | |
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.getStyleClass().addAll("default-color0", "chart-line-symbol", "chart-series-line"); | |
label.setStyle("-fx-font-size: 20; -fx-font-weight: bold;"); | |
if (priorValue == 0) { | |
label.setTextFill(Color.DARKGRAY); | |
} else if (value > priorValue) { | |
label.setTextFill(Color.FORESTGREEN); | |
} else { | |
label.setTextFill(Color.FIREBRICK); | |
} | |
label.setMinSize(Label.USE_PREF_SIZE, Label.USE_PREF_SIZE); | |
return label; | |
} | |
} | |
public static void main(String[] args) { launch(args); } | |
} |
Thanks~ Checking whether this solution is still instructive. By the way, checking for https://github.com/fair-acc/chart-fx instead of https://github.com/openjdk/jfx actually.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answer to the StackOverflow question: LineChart Hover Values