java - 在 javafx 中更改堆栈条形图中的颜色

标签 java javafx javafx-2 javafx-8

我创建了包含两个堆栈的堆栈条形图,我想让后一个堆栈不可见....

StackBar.getData().addAll(series1,series2);

我想改变series1的颜色,

有没有什么方法或教程可以说明如何做到这一点?

最佳答案

我想你的意思是这样的:

因此,您所要做的就是更改此 CSS 类:.default-color0.chart-bar

然后你可以将它设置为白色(与背景或visibility:hidden;相同) 您可以在下面看到一个如何实现这一目标的示例。

enter image description here

import java.util.Arrays;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;


public class Charts extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("stackoverflow.com");
        Group root = new Group();

        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();

        xAxis.setLabel("Month");
        xAxis.setCategories(FXCollections.<String> observableArrayList(Arrays.asList(
                "January", 
                "February",
                "March",
                "April",
                "May",
                "June",
                "July",
                "August",
                "September",
                "October",
                "November",
                "December")));
        yAxis.setLabel("Value");

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

        stackedBarChart.setTitle("StackedBarChart");

        //Series 1
        XYChart.Series<String,Number> series1 = new XYChart.Series();
        series1.setName("XYChart.Series 1");

        series1.getData().add(new XYChart.Data("January", 100));
        series1.getData().add(new XYChart.Data("February", 200));
        series1.getData().add(new XYChart.Data("March", 50));
        series1.getData().add(new XYChart.Data("April", 75));
        series1.getData().add(new XYChart.Data("May", 110));
        series1.getData().add(new XYChart.Data("June", 300));
        series1.getData().add(new XYChart.Data("July", 111));
        series1.getData().add(new XYChart.Data("August", 30));
        series1.getData().add(new XYChart.Data("September", 75));
        series1.getData().add(new XYChart.Data("October", 55));
        series1.getData().add(new XYChart.Data("November", 225));
        series1.getData().add(new XYChart.Data("December", 99));

        //Series 2
        XYChart.Series<String,Number> series2 = new XYChart.Series();
        series2.setName("XYChart.Series 2");


        series2.getData().add(new XYChart.Data("January", 150));
        series2.getData().add(new XYChart.Data("February", 100));
        series2.getData().add(new XYChart.Data("March", 60));
        series2.getData().add(new XYChart.Data("April", 40));
        series2.getData().add(new XYChart.Data("May", 30));
        series2.getData().add(new XYChart.Data("June", 100));
        series2.getData().add(new XYChart.Data("July", 100));
        series2.getData().add(new XYChart.Data("August", 10));
        series2.getData().add(new XYChart.Data("September", 175));
        series2.getData().add(new XYChart.Data("October", 155));
        series2.getData().add(new XYChart.Data("November", 125));
        series2.getData().add(new XYChart.Data("December", 150));

        //
        stackedBarChart.getData().addAll(series1, series2);      
        root.getChildren().addAll(stackedBarChart);
        primaryStage.setScene(new Scene(root, 500, 400));
        primaryStage.getScene().getStylesheets().add("style.css");
        primaryStage.show();
    }
}

然后是 styles.css 的 CSS:

.default-color0.chart-bar {
    -fx-bar-fill: #FFFFFF; 
    visibility:hidden;
    }
.default-color1.chart-bar { 
    -fx-bar-fill: #00FFCC; 
    }

关于java - 在 javafx 中更改堆栈条形图中的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24018210/

相关文章:

JAVA double 转字符串格式

java - 为什么编码应该是 "to the interface"特别是对于原始数据类型?

javafx - 无法构建 JemmyFX

java - 将焦点放在组合框的特定项目上而不选择它

java - Swing 中的统一工具栏无法正常工作

java - 使用 sed 更新 java 属性文件中的属性

java - 无法在 JavaFx 中加载图像

java - 桌面应用程序上的一次性 Java 配置文件

zooming - Javafx 2图表缩放

animation - AnimationTimer 是如何工作的?