如何在 JavaFX 中用两个参数创建气泡图?
气泡图接受一系列数据点 (x, y) 作为输入值,并针对给定序列中的数据点创建气泡。在 JavaFX 中,你可以通过实例化 javafx.scene.chart.BubbleChart 类来创建一个气泡图。
通常,在所有 X-Y 图表中,数据点表示两个值 (x, y)。气泡图中有一个第三个值,即半径。此图在三维中绘制数据点时非常方便。
无论如何,拥有第三个值并不是必要的,它只是可选的。与任何其他 XY 图像一样,你可以创建具有两个值的气泡图。
示例
以下是 JavaFX 示例,演示如何创建具有两个值的气泡图 −
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.chart.BubbleChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.layout.StackPane; public class BubbleChart_TwoParams extends Application { public void start(Stage stage) { //Creating the X and Y axes NumberAxis xAxis = new NumberAxis(5, 25, 5); NumberAxis yAxis = new NumberAxis(50, 90, 5); //Setting labels to the axes xAxis.setLabel("Temperature °C"); yAxis.setLabel("Ice Cream Sales in (USD)"); //Creating the Scatter chart BubbleChart bubbleChart = new BubbleChart(xAxis, yAxis); //Preparing data for the scatter chart XYChart.Series series = new XYChart.Series(); series.getData().add(new XYChart.Data(15.2, 72.79)); series.getData().add(new XYChart.Data(8.39, 83.97)); series.getData().add(new XYChart.Data(20.6, 67.14)); series.getData().add(new XYChart.Data(15.8, 80.32)); series.getData().add(new XYChart.Data(10.4, 87.27)); //Setting the data to scatter chart bubbleChart.getData().add(series); //Setting title to the scatter chart //scatterChart.setTitle("Ice Cream Sales vs Temperature"); //Setting name to the series series.setName("Temperatue vs Icecream Sales"); //Creating a stack pane to hold the chart StackPane pane = new StackPane(bubbleChart); pane.setPadding(new Insets(15, 15, 15, 15)); pane.setStyle("-fx-background-color: BEIGE"); //Setting the Scene Scene scene = new Scene(pane, 595, 300); stage.setTitle("Bubble Chart"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出
广告