气泡图是一个双轴图表,为一系列数据点绘制气泡。

创建气泡图

要创建气泡图,我们创建BubbleChart类,定义水平和垂直轴,并使用添加一个或多个数据系列XYChart.Data类。

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.chart.BubbleChart;
  4. import javafx.scene.chart.NumberAxis;
  5. import javafx.scene.chart.XYChart;
  6. import javafx.scene.paint.Color;
  7. import javafx.stage.Stage;
  8. public class Main extends Application {
  9. @Override public void start(Stage stage) {
  10. stage.setTitle("Bubble Chart Sample");
  11. final NumberAxis xAxis = new NumberAxis(1, 53, 4);
  12. final NumberAxis yAxis = new NumberAxis(0, 80, 10);
  13. final BubbleChart<Number,Number> blc =
  14. new BubbleChart<Number,Number>(xAxis,yAxis);
  15. xAxis.setLabel("Week");
  16. xAxis.setTickLabelFill(Color.CHOCOLATE);
  17. xAxis.setMinorTickCount(4);
  18. yAxis.setLabel("Product Budget");
  19. yAxis.setTickLabelFill(Color.CHOCOLATE);
  20. yAxis.setTickLabelGap(10);
  21. yAxis.setTickLabelFormatter(
  22. new NumberAxis.DefaultFormatter(yAxis,"$ ",null)
  23. );
  24. blc.setTitle("Budget Monitoring");
  25. XYChart.Series series1 = new XYChart.Series();
  26. series1.setName("Product 1");
  27. series1.getData().add(new XYChart.Data(3, 35, 2));
  28. series1.getData().add(new XYChart.Data(12, 60, 1.8));
  29. series1.getData().add(new XYChart.Data(15, 15, 7));
  30. series1.getData().add(new XYChart.Data(22, 30, 2.5));
  31. series1.getData().add(new XYChart.Data(28, 20, 1));
  32. series1.getData().add(new XYChart.Data(35, 41, 5.5));
  33. series1.getData().add(new XYChart.Data(42, 17, 9));
  34. series1.getData().add(new XYChart.Data(49, 30, 1.8));
  35. XYChart.Series series2 = new XYChart.Series();
  36. series2.setName("Product 2");
  37. series2.getData().add(new XYChart.Data(8, 15, 2));
  38. series2.getData().add(new XYChart.Data(13, 23, 1));
  39. series2.getData().add(new XYChart.Data(15, 45, 3));
  40. series2.getData().add(new XYChart.Data(24, 30, 4.5));
  41. series2.getData().add(new XYChart.Data(38, 78, 1));
  42. series2.getData().add(new XYChart.Data(40, 41, 7.5));
  43. series2.getData().add(new XYChart.Data(45, 57, 2));
  44. series2.getData().add(new XYChart.Data(47, 23, 3.8));
  45. Scene scene = new Scene(blc);
  46. blc.getData().addAll(series1, series2);
  47. //scene.getStylesheets().add("bubblechartsample/Chart.css");
  48. stage.setScene(scene);
  49. stage.show();
  50. }
  51. public static void main(String[] args) {
  52. launch(args);
  53. }
  54. }

image.png
NumberAxis.DefaultFormatter类将向指定轴的标记添加前缀和后缀。

  1. yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,"$ ",null));

以下代码行指定了刻度标签的CHOCOLATE颜色。

  1. xAxis.setTickLabelFill(Color.CHOCOLATE);
  2. yAxis.setTickLabelFill(Color.CHOCOLATE);

例子

  1. import javafx.application.Application;
  2. import javafx.collections.FXCollections;
  3. import javafx.collections.ObservableList;
  4. import javafx.scene.Scene;
  5. import javafx.scene.chart.BubbleChart;
  6. import javafx.scene.chart.NumberAxis;
  7. import javafx.scene.chart.XYChart;
  8. import javafx.scene.chart.XYChart.Series;
  9. import javafx.scene.layout.StackPane;
  10. import javafx.stage.Stage;
  11. public class Main extends Application {
  12. public static void main(String[] args) {
  13. launch(args);
  14. }
  15. @Override
  16. public void start(Stage primaryStage) {
  17. NumberAxis xAxis = new NumberAxis();
  18. NumberAxis yAxis = new NumberAxis();
  19. xAxis.setAutoRanging(false);
  20. xAxis.setLowerBound(2011);
  21. xAxis.setUpperBound(2016);
  22. BubbleChart bubbleChart = new BubbleChart(xAxis, yAxis);
  23. bubbleChart.setData(getChartData());
  24. bubbleChart.setTitle("Title");
  25. primaryStage.setTitle("Chart example");
  26. StackPane root = new StackPane();
  27. root.getChildren().add(bubbleChart);
  28. primaryStage.setScene(new Scene(root, 400, 250));
  29. primaryStage.show();
  30. }
  31. private ObservableList<XYChart.Series<Integer, Double>> getChartData() {
  32. double aValue = 1.56;
  33. double bValue = 1.06;
  34. ObservableList<XYChart.Series<Integer, Double>> answer = FXCollections.observableArrayList();
  35. Series<Integer, Double> aSeries = new Series<Integer, Double>();
  36. Series<Integer, Double> bSeries = new Series<Integer, Double>();
  37. aSeries.setName("A");
  38. bSeries.setName("B");
  39. for (int i = 2011; i < 2016; i++) {
  40. double diff = Math.random();
  41. aSeries.getData().add(new XYChart.Data(i, aValue, diff));
  42. aValue = aValue + 10*diff - 5;
  43. diff = Math.random();
  44. bSeries.getData().add(new XYChart.Data(i, bValue, diff));
  45. bValue = bValue + 10*diff - 5;
  46. diff = Math.random();
  47. }
  48. answer.addAll(aSeries, bSeries);
  49. return answer;
  50. }
  51. }

image.png