java - 如何在 StackedBarChart 上设置交替条形的样式

标签 java javafx

如何使 A1 B1 C1 条形变为红色,A2 B2 C2 条形变为蓝色?

enter image description here

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class StackedBarChartExample extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("StackedBarChart Experiments");

        CategoryAxis xAxis = new CategoryAxis();
        xAxis.getCategories().addAll("300", "302.5", "305");
        NumberAxis yAxis = new NumberAxis();

        StackedBarChart chart = new StackedBarChart(xAxis, yAxis);

        XYChart.Series series1 = new XYChart.Series();
        series1.setName("300");
        series1.getData().add(new XYChart.Data("A1", 25601.34));
        series1.getData().add(new XYChart.Data("A2", 20148.82));

        XYChart.Series series2 = new XYChart.Series();
        series2.setName("302.50");
        series2.getData().add(new XYChart.Data("B1", 57401.85));
        series2.getData().add(new XYChart.Data("B2", 41941.19));

        XYChart.Series series3 = new XYChart.Series();
        series3.setName("305");
        series3.getData().add(new XYChart.Data("C1", 45000.65));
        series3.getData().add(new XYChart.Data("C2", 44835.76));

        chart.getData().addAll(series1, series2, series3);

        VBox vbox = new VBox(chart);

        Scene scene = new Scene(vbox, 400, 600);
        primaryStage.setScene(scene);
        primaryStage.setHeight(400);
        primaryStage.setWidth(600);
        primaryStage.show();
    }


    public static void main(String[] args) {
        Application.launch(args);
    }
}

最佳答案

我认为你可以使用 this 中相同的想法回答。只需循环遍历该系列,如果系列数据 x 点等于您想要红色的数据点之一,则设置其节点样式。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class StackedBarChartExample extends Application
{

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        primaryStage.setTitle("StackedBarChart Experiments");

        CategoryAxis xAxis = new CategoryAxis();
        xAxis.getCategories().addAll("300", "302.5", "305");
        NumberAxis yAxis = new NumberAxis();

        StackedBarChart<String, Number> chart = new StackedBarChart(xAxis, yAxis);

        XYChart.Series<String, Number> series1 = new XYChart.Series();
        series1.setName("300");
        series1.getData().add(new XYChart.Data("A1", 25601.34));
        series1.getData().add(new XYChart.Data("A2", 20148.82));

        XYChart.Series<String, Number> series2 = new XYChart.Series();
        series2.setName("302.50");
        series2.getData().add(new XYChart.Data("B1", 57401.85));
        series2.getData().add(new XYChart.Data("B2", 41941.19));

        XYChart.Series<String, Number> series3 = new XYChart.Series();
        series3.setName("305");
        series3.getData().add(new XYChart.Data("C1", 45000.65));
        series3.getData().add(new XYChart.Data("C2", 44835.76));

        chart.getData().addAll(series1, series2, series3);

        VBox vbox = new VBox(chart);

        Scene scene = new Scene(vbox, 400, 600);
        primaryStage.setScene(scene);
        primaryStage.setHeight(400);
        primaryStage.setWidth(600);
        primaryStage.show();

        chart.getData().forEach((t) -> {
            t.getData().forEach((j) -> {
                if (j.getXValue().equals("A1") || j.getXValue().equals("B1") || j.getXValue().equals("C1")) {
                    j.getNode().setStyle("-fx-background-color: red");
                }
                else {
                    j.getNode().setStyle("-fx-background-color: blue");
                }
            });
        });


    }

    public static void main(String[] args)
    {
        Application.launch(args);
    }
}

如果颜色始终交替,您可以做的另一件事是使用计数器并根据 counter % 2 == 0 更改颜色。我不确定 ObservableList 是否总是强制执行该顺序。在使用第二种方法之前我会发现这一点。

AtomicInteger counter = new AtomicInteger();
chart.getData().forEach((t) -> {
    t.getData().forEach((j) -> {
        if (counter.getAndIncrement() % 2 == 0) {
            j.getNode().setStyle("-fx-background-color: red");
        }
        else {
            j.getNode().setStyle("-fx-background-color: blue");
        }
    });
});

enter image description here

关于java - 如何在 StackedBarChart 上设置交替条形的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60180455/

相关文章:

java - 如何循环Arraylist<String>并添加到子节点以及将子节点添加到父节点

java - Javafx中的滚动条不滚动

browser - JavaFX - 它真的可以部署在浏览器中吗?

JavaFX 文本在高 DPI 上无法正确缩放

java - 如何更改Json格式?

java - 无法在多文档事务中创建命名空间(MongoDB 4.0、Spring Data 2.1.0、Spring Boot)

java - 程序忽略 "If Else"并打印所有内容

java - 无法将数据从 C# 发送到 Java (Android) 程序

Java(FX) - 只允许 1 个类从单例类调用方法

java - 检查 JavaFX-FlowPane 中包装的元素