Created
June 9, 2024 22:25
-
-
Save trashgod/8165d815767f4294035be9021f52a91f to your computer and use it in GitHub Desktop.
JavaFX Charts
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.beans.property.SimpleStringProperty; | |
import javafx.scene.Scene; | |
import javafx.scene.chart.BarChart; | |
import javafx.scene.chart.CategoryAxis; | |
import javafx.scene.chart.NumberAxis; | |
import javafx.scene.chart.XYChart; | |
import javafx.scene.chart.XYChart.Data; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
/** | |
* https://stackoverflow.com/a/78568588/230513 | |
*/ | |
public class BarChartApp extends Application { | |
final static String austria = "Austria"; | |
final static String brazil = "Brazil"; | |
final static String france = "France"; | |
final static String italy = "Italy"; | |
final static String usa = "USA"; | |
@Override | |
public void start(Stage stage) { | |
final CategoryAxis xAxis = new CategoryAxis(); | |
final NumberAxis yAxis = new NumberAxis(); | |
final BarChart<String, Number> chart = new BarChart<>(xAxis, yAxis); | |
chart.setTitle("Country Summary"); | |
xAxis.setLabel("Country"); | |
yAxis.setLabel("Value"); | |
XYChart.Series<String, Number> series1 = new XYChart.Series<>(); | |
series1.setName("2003"); | |
series1.getData().add(new XYChart.Data<>(austria, 25601.34)); | |
series1.getData().add(new XYChart.Data<>(brazil, 20148.82)); | |
series1.getData().add(new XYChart.Data<>(france, 10000)); | |
series1.getData().add(new XYChart.Data<>(italy, 35407.15)); | |
series1.getData().add(new XYChart.Data<>(usa, 12000)); | |
XYChart.Series<String, Number> series2 = new XYChart.Series<>(); | |
series2.setName("2004"); | |
series2.getData().add(new XYChart.Data<>(austria, 57401.85)); | |
series2.getData().add(new XYChart.Data<>(brazil, 41941.19)); | |
series2.getData().add(new XYChart.Data<>(france, 45263.37)); | |
series2.getData().add(new XYChart.Data<>(italy, 117320.16)); | |
series2.getData().add(new XYChart.Data<>(usa, 14845.27)); | |
XYChart.Series<String, Number> series3 = new XYChart.Series<>(); | |
series3.setName("2005"); | |
series3.getData().add(new XYChart.Data<>(austria, 45000.65)); | |
series3.getData().add(new XYChart.Data<>(brazil, 44835.76)); | |
series3.getData().add(new XYChart.Data<>(france, 18722.18)); | |
series3.getData().add(new XYChart.Data<>(italy, 17557.31)); | |
series3.getData().add(new XYChart.Data<>(usa, 92633.68)); | |
chart.getData().add(series1); | |
chart.getData().add(series2); | |
chart.getData().add(series3); | |
var clicked = new SimpleStringProperty(); | |
clicked.set("Click a Bar."); | |
for (XYChart.Series<String, Number> series : chart.getData()) { | |
for (Data<String, Number> data : series.getData()) { | |
data.getNode().setOnMouseClicked((var t) -> { | |
clicked.set("Series: " + series.getName() + ", " | |
+ data.getXValue() + " = " + data.getYValue()); | |
}); | |
} | |
} | |
var root = new BorderPane(chart); | |
var label = new Label(); | |
label.textProperty().bind(clicked); | |
label.setStyle("-fx-font-size: 18; -fx-padding: 5"); | |
root.setBottom(new StackPane(label)); | |
stage.setTitle("Bar Chart Sample"); | |
stage.setScene(new Scene(root, 800, 600)); | |
stage.show(); | |
} | |
public static void main(String[] args) { | |
launch(BarChartApp.class); | |
} | |
} |
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.beans.property.SimpleStringProperty; | |
import javafx.beans.property.StringProperty; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
import org.jfree.chart.ChartFactory; | |
import org.jfree.chart.JFreeChart; | |
import org.jfree.chart.entity.CategoryItemEntity; | |
import org.jfree.chart.entity.ChartEntity; | |
import org.jfree.chart.fx.ChartViewer; | |
import org.jfree.chart.fx.interaction.ChartMouseEventFX; | |
import org.jfree.chart.fx.interaction.ChartMouseListenerFX; | |
import org.jfree.chart.plot.PlotOrientation; | |
import org.jfree.data.category.CategoryDataset; | |
import org.jfree.data.category.DefaultCategoryDataset; | |
/** | |
* https://stackoverflow.com/q/78567687/230513 | |
*/ | |
public class BarChartJFCFX extends Application { | |
@Override | |
public void start(Stage stage) { | |
var chartViewer = new ChartViewer(); | |
var handler = new Handler(chartViewer); | |
var label = new Label(); | |
label.setStyle("-fx-font-size: 18; -fx-padding: 5"); | |
label.textProperty().bind(handler.clickedProperty()); | |
var root = new StackPane(chartViewer); | |
root.getChildren().add(label); | |
StackPane.setAlignment(label, Pos.TOP_RIGHT); | |
Scene scene = new Scene(root, 640, 480); | |
stage.setTitle("BarChart JFreeChart FX"); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
private static final class Handler { | |
private static final String PROMPT = "Click on a bar."; | |
private final StringProperty clicked; | |
public StringProperty clickedProperty() { | |
return clicked; | |
} | |
public Handler(ChartViewer view) { | |
this.clicked = new SimpleStringProperty(PROMPT); | |
view.setChart(createChart(createDataset())); | |
view.addChartMouseListener(new ChartMouseListenerFX() { | |
@Override | |
public void chartMouseClicked(ChartMouseEventFX e) { | |
ChartEntity ce = e.getEntity(); | |
if (ce instanceof CategoryItemEntity cie) { | |
var data = cie.getDataset(); | |
var rowKey = cie.getRowKey(); | |
var colKey = cie.getColumnKey(); | |
var value = data.getValue(rowKey, colKey); | |
clicked.set(rowKey + ", " + colKey + " = " + value); | |
} else { | |
clicked.set(PROMPT); | |
} | |
} | |
@Override | |
public void chartMouseMoved(ChartMouseEventFX event) { | |
} | |
}); | |
} | |
private static JFreeChart createChart(CategoryDataset dataset) { | |
var chart = ChartFactory.createBarChart( | |
"Bar Chart", "Category", "Value", dataset, | |
PlotOrientation.VERTICAL, true, true, false | |
); | |
return chart; | |
} | |
private static CategoryDataset createDataset() { | |
// row keys... | |
var series1 = "Series 1"; | |
var series2 = "Series 2"; | |
// column keys... | |
var category1 = "Category 1"; | |
var category2 = "Category 2"; | |
var category3 = "Category 3"; | |
var dataset = new DefaultCategoryDataset(); | |
dataset.addValue(1.0, series1, category1); | |
dataset.addValue(4.0, series1, category2); | |
dataset.addValue(3.0, series1, category3); | |
dataset.addValue(5.0, series2, category1); | |
dataset.addValue(7.0, series2, category2); | |
dataset.addValue(6.0, series2, category3); | |
return dataset; | |
} | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment