gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:00:48 25 4
gpt4 key购买 nike

如何使 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com