📅  最后修改于: 2023-12-03 15:16:03.724000             🧑  作者: Mango
JavaFX散点图是一种用于展示数据集中的离散数据的图表类型。它通常用于发现两个变量之间的关系或趋势,或识别数据中的异常值。
要创建JavaFX散点图,您需要使用ScatterChart
类。以下是创建散点图的基本代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class ScatterChartSample extends Application {
@Override public void start(Stage stage) {
NumberAxis xAxis = new NumberAxis("X-Axis", 0, 10, 1);
NumberAxis yAxis = new NumberAxis("Y-Axis", 0, 10, 1);
ScatterChart<Number, Number> scatterChart = new ScatterChart<>(xAxis, yAxis);
scatterChart.setTitle("Scatter Chart");
XYChart.Series<Number, Number> series1 = new XYChart.Series<>();
series1.setName("Series 1");
series1.getData().add(new XYChart.Data<>(1, 2));
series1.getData().add(new XYChart.Data<>(2, 1));
series1.getData().add(new XYChart.Data<>(3, 5));
series1.getData().add(new XYChart.Data<>(4, 3));
series1.getData().add(new XYChart.Data<>(5, 8));
series1.getData().add(new XYChart.Data<>(6, 6));
series1.getData().add(new XYChart.Data<>(7, 9));
series1.getData().add(new XYChart.Data<>(8, 10));
series1.getData().add(new XYChart.Data<>(9, 4));
series1.getData().add(new XYChart.Data<>(10, 7));
scatterChart.getData().addAll(series1);
Scene scene = new Scene(scatterChart, 500, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
此代码将创建一个带有场景和标题的散点图,并将其添加到场景中。还将添加一个包含散点的系列,并使用addData()
方法添加数据。
要自定义JavaFX散点图,您可以使用以下选项之一:
您可以通过设置ScatterChart
,XYChart.Series
或XYChart.Data
的属性来实现这些更改。以下是一些示例代码:
// 更改散点大小和形状
XYChart.Series<Number, Number> series1 = new XYChart.Series<>();
series1.getData().add(new XYChart.Data<>(1, 2, 10)); // 第三个参数是大小
series1.getData().add(new XYChart.Data<>(2, 1, 20));
series1.getData().add(new XYChart.Data<>(3, 5, 30));
series1.getData().add(new XYChart.Data<>(4, 3, 40));
series1.getData().add(new XYChart.Data<>(5, 8, 50));
series1.getData().add(new XYChart.Data<>(6, 6, 60));
series1.getData().add(new XYChart.Data<>(7, 9, 70));
series1.getData().add(new XYChart.Data<>(8, 10, 80));
series1.getData().add(new XYChart.Data<>(9, 4, 90));
series1.getData().add(new XYChart.Data<>(10, 7, 100));
for (XYChart.Data<Number, Number> data : series1.getData()) {
Node node = data.getNode(); // 获取当前散点的 Node
node.setStyle("-fx-background-color: #ff0000;"); // 设定颜色
node.setScaleX(2); // 设定大小
node.setScaleY(2);
}
// 添加数据标签和工具提示
XYChart.Series<Number, Number> series1 = new XYChart.Series<>();
series1.getData().add(new XYChart.Data<>(1, 2, new Label("Point 1"))); // 第三个参数是 Label
series1.getData().add(new XYChart.Data<>(2, 1, new Label("Point 2")));
series1.getData().add(new XYChart.Data<>(3, 5, new Label("Point 3")));
series1.getData().add(new XYChart.Data<>(4, 3, new Label("Point 4")));
series1.getData().add(new XYChart.Data<>(5, 8, new Label("Point 5")));
series1.getData().add(new XYChart.Data<>(6, 6, new Label("Point 6")));
series1.getData().add(new XYChart.Data<>(7, 9, new Label("Point 7")));
series1.getData().add(new XYChart.Data<>(8, 10, new Label("Point 8")));
series1.getData().add(new XYChart.Data<>(9, 4, new Label("Point 9")));
series1.getData().add(new XYChart.Data<>(10, 7, new Label("Point 10")));
for (XYChart.Data<Number, Number> data : series1.getData()) {
Tooltip tooltip = new Tooltip(data.getYValue().toString());
Tooltip.install(data.getNode(), tooltip);
}
// 更改背景和网格线样式
scatterChart.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, CornerRadii.EMPTY, Insets.EMPTY)));
xAxis.setTickMarkVisible(false);
yAxis.setTickMarkVisible(false);
xAxis.setMinorTickVisible(false);
yAxis.setMinorTickVisible(false);
xAxis.setTickLabelFill(Color.DARKGRAY);
yAxis.setTickLabelFill(Color.DARKGRAY);
xAxis.setTickLabelFont(new Font(12));
yAxis.setTickLabelFont(new Font(12));
xAxis.setGridLineStrokeWidth(0.2);
yAxis.setGridLineStrokeWidth(0.2);
JavaFX散点图是一种强大的数据可视化工具,可帮助您展示离散数据的趋势和关系。通过使用ScatterChart
,XYChart.Series
和XYChart.Data
的属性,您可以自定义散点图以满足您的需求。