条形图是具有可以是垂直或水平的矩形条的双轴图表

  1. import javafx.application.Application;
  2. import javafx.scene.Group;
  3. import javafx.scene.Scene;
  4. import javafx.scene.chart.BarChart;
  5. import javafx.scene.chart.CategoryAxis;
  6. import javafx.scene.chart.NumberAxis;
  7. import javafx.scene.layout.VBox;
  8. import javafx.stage.Stage;
  9. public class Main extends Application {
  10. @Override
  11. public void start(final Stage stage) {
  12. stage.setTitle("");
  13. stage.setWidth(500);
  14. stage.setHeight(500);
  15. Scene scene = new Scene(new Group());
  16. VBox root = new VBox();
  17. NumberAxis lineYAxis = new NumberAxis(0,100,10);
  18. CategoryAxis lineXAxis = new CategoryAxis();
  19. BarChart barChart = new BarChart(lineXAxis,lineYAxis);
  20. lineYAxis.setLabel("Sales");
  21. lineXAxis.setLabel("Products");
  22. root.getChildren().addAll(barChart);
  23. scene.setRoot(root);
  24. stage.setScene(scene);
  25. stage.show();
  26. }
  27. public static void main(String[] args) {
  28. launch(args);
  29. }
  30. }

image.png

水平条形图

  1. import javafx.animation.Animation;
  2. import javafx.animation.KeyFrame;
  3. import javafx.animation.Timeline;
  4. import javafx.application.Application;
  5. import javafx.event.ActionEvent;
  6. import javafx.event.EventHandler;
  7. import javafx.scene.Scene;
  8. import javafx.scene.chart.BarChart;
  9. import javafx.scene.chart.CategoryAxis;
  10. import javafx.scene.chart.NumberAxis;
  11. import javafx.scene.chart.XYChart;
  12. import javafx.stage.Stage;
  13. import javafx.util.Duration;
  14. public class Main extends Application {
  15. final static String itemA = "A";
  16. final static String itemB = "B";
  17. final static String itemC = "F";
  18. @Override
  19. public void start(Stage stage) {
  20. final NumberAxis xAxis = new NumberAxis();
  21. final CategoryAxis yAxis = new CategoryAxis();
  22. final BarChart<Number, String> bc = new BarChart<Number, String>(xAxis, yAxis);
  23. bc.setTitle("Summary");
  24. xAxis.setLabel("Value");
  25. xAxis.setTickLabelRotation(90);
  26. yAxis.setLabel("Item");
  27. XYChart.Series series1 = new XYChart.Series();
  28. series1.setName("2003");
  29. series1.getData().add(new XYChart.Data(2, itemA));
  30. series1.getData().add(new XYChart.Data(20, itemB));
  31. series1.getData().add(new XYChart.Data(10, itemC));
  32. XYChart.Series series2 = new XYChart.Series();
  33. series2.setName("2004");
  34. series2.getData().add(new XYChart.Data(50, itemA));
  35. series2.getData().add(new XYChart.Data(41, itemB));
  36. series2.getData().add(new XYChart.Data(45, itemC));
  37. XYChart.Series series3 = new XYChart.Series();
  38. series3.setName("2005");
  39. series3.getData().add(new XYChart.Data(45, itemA));
  40. series3.getData().add(new XYChart.Data(44, itemB));
  41. series3.getData().add(new XYChart.Data(18, itemC));
  42. Timeline tl = new Timeline();
  43. tl.getKeyFrames().add(new KeyFrame(Duration.millis(500),
  44. new EventHandler<ActionEvent>() {
  45. @Override public void handle(ActionEvent actionEvent) {
  46. for (XYChart.Series<Number, String> series : bc.getData()) {
  47. for (XYChart.Data<Number, String> data : series.getData()) {
  48. data.setXValue(Math.random() * 100);
  49. }
  50. }
  51. }
  52. }));
  53. tl.setCycleCount(Animation.INDEFINITE);
  54. tl.play();
  55. Scene scene = new Scene(bc, 800, 600);
  56. bc.getData().addAll(series1, series2, series3);
  57. stage.setScene(scene);
  58. stage.show();
  59. }
  60. public static void main(String[] args) {
  61. launch(args);
  62. }
  63. }

image.png

StackedBarChart演示

  1. import java.util.Arrays;
  2. import javafx.application.Application;
  3. import javafx.collections.FXCollections;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.scene.chart.CategoryAxis;
  7. import javafx.scene.chart.NumberAxis;
  8. import javafx.scene.chart.StackedBarChart;
  9. import javafx.scene.chart.XYChart;
  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. Group root = new Group();
  18. final CategoryAxis xAxis = new CategoryAxis();
  19. final NumberAxis yAxis = new NumberAxis();
  20. xAxis.setLabel("Month");
  21. xAxis.setCategories(FXCollections.<String> observableArrayList(Arrays.asList(
  22. "January",
  23. "February",
  24. "March")));
  25. yAxis.setLabel("Value");
  26. final StackedBarChart<String,Number> stackedBarChart = new StackedBarChart<String,Number>(xAxis,yAxis);
  27. stackedBarChart.setTitle("StackedBarChart");
  28. XYChart.Series<String,Number> series1 = new XYChart.Series();
  29. series1.setName("XYChart.Series 1");
  30. series1.getData().add(new XYChart.Data("January", 100));
  31. series1.getData().add(new XYChart.Data("February", 200));
  32. series1.getData().add(new XYChart.Data("March", 50));
  33. XYChart.Series<String,Number> series2 = new XYChart.Series();
  34. series2.setName("XYChart.Series 2");
  35. series2.getData().add(new XYChart.Data("January", 150));
  36. series2.getData().add(new XYChart.Data("February", 100));
  37. series2.getData().add(new XYChart.Data("March", 60));
  38. stackedBarChart.getData().addAll(series1, series2);
  39. root.getChildren().addAll(stackedBarChart);
  40. primaryStage.setScene(new Scene(root, 500, 400));
  41. primaryStage.show();
  42. }
  43. }

image.png