【问题标题】:JavaFX Coloring XYChart.SeriesJavaFX 着色 XYChart.Series
【发布时间】:2014-12-11 10:07:52
【问题描述】:

我正在尝试设置我的 JavaFX 面积图的样式,但找不到任何方法将颜色设置为特定系列。我知道,我可以通过 CSS 设置样式,但我也想在代码中实现。

我该怎么做? 1.) 如何使用内联样式为面积图设置颜色?

感谢您的帮助。

@何塞:

我以前是这样弄的,但是对我不起作用!

@Override
    public void start(Stage primaryStage)
    {
        final AreaChart<String, Number> areaChart = new AreaChart<>(new CategoryAxis(), new NumberAxis());

        ObservableList<XYChart.Data<String, Integer>> xyList = FXCollections.observableArrayList(
            new XYChart.Data<>("P1", 30),
            new XYChart.Data<>("P2", 40),
            new XYChart.Data<>("P3", 30));
        XYChart.Series series = new XYChart.Series(xyList);
        areaChart.getData().addAll(series);

        Button button = new Button("Change style");
        button.setOnAction(new EventHandler<ActionEvent>()
        {

            @Override
            public void handle(ActionEvent arg0)
            {
                int redColor = 0, greenColor = 127, blueColor = 195;
                double opacity = 0.3;
                areaChart.setStyle("CHART_COLOR_1: rgb(" + redColor + "," + greenColor + "," + blueColor + "); "
                    + "CHART_COLOR_1_TRANS_20: rgba(" + redColor + "," + greenColor + "," + blueColor + "," + opacity + ");");

            }
        });

        VBox root = new VBox();
        root.getChildren().addAll(button, areaChart);
        Scene scene = new Scene(root, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

【问题讨论】:

  • 你已经有一个answer
  • @JoséPereda 你可以为我再次制作那个示例文件,而不需要任何 Java8 函数等等吗?我无法使用它,因为我使用的是 Java7!它以前对我也不起作用。如果您可以制作一个完整的类来处理它,那就太好了(仅 Java7 函数-> 没有 Lambda 表达式等等=
  • @JoséPereda 我也只想更改意甲的颜色,而不是完整图表。

标签: java css charts javafx styles


【解决方案1】:

answer 在 Java 7 中不起作用,因为默认的 CSS 样式表是 Caspian,而不是 Modena。

在 Caspian 中,主调色板的这些常量未定义:CHART_COLOR_1CHART_COLOR_1_TRANS_20、...

因此,如果您想应用内联样式,一种方法是通过查找查找系列并根据其 CSS 选择器将您的样式应用于符号、线条和/或区域。例如:

@Override
public void start(Stage primaryStage) {

    final AreaChart<String, Number> areaChart = new AreaChart<>(new CategoryAxis(), new NumberAxis());

    ObservableList<XYChart.Data<String, Integer>> xyList = FXCollections.observableArrayList(
        new XYChart.Data<>("P1", 30),
        new XYChart.Data<>("P2", 40),
        new XYChart.Data<>("P3", 30));
    XYChart.Series series = new XYChart.Series(xyList);
    areaChart.getData().addAll(series);

    Scene scene = new Scene(areaChart, 400, 300);
    primaryStage.setScene(scene);
    primaryStage.show();

    int redColor = 0, greenColor = 127, blueColor = 195;
    double opacity = 0.3;

    for(Node symbol : areaChart.lookupAll(".default-color0.chart-area-symbol")){
        symbol.setStyle("-fx-background-color: rgb(" + redColor + "," + greenColor + "," + blueColor + "), white; ");
    }
    Node line = areaChart.lookup(".default-color0.chart-series-area-line");
    line.setStyle("-fx-stroke: rgb(" + redColor + "," + greenColor + "," + blueColor + "); ");
    Node area = areaChart.lookup(".default-color0.chart-series-area-fill");
    area.setStyle("-fx-fill: rgba(" + redColor + "," + greenColor + "," + blueColor + "," + opacity + "); ");
}

编辑

上述解决方案最多适用于 8 个系列。

对于任何数量的系列,这种其他方法也可以:

    int numSeries=10;
    final int[] redColor = new int[numSeries];
    Arrays.fill(redColor, 0);
    final int[] greenColor =new int[numSeries]; 
    Arrays.fill(greenColor, 127);
    final int[] blueColor = new int[numSeries];
    Arrays.fill(blueColor, 195);
    double opacity = 0.3;


    for(int i=0; i<numSeries; i++){
        for(Node n : areaChart.lookupAll(".series"+i)){
            n.setStyle("-fx-background-color: rgb(" + redColor[i] + "," + greenColor[i] + "," + blueColor[i] + "), white; "
                    + "-fx-stroke: rgb(" + redColor[i] + "," + greenColor[i] + "," + blueColor[i] + "); "
                    + "-fx-fill: rgba(" + redColor[i] + "," + greenColor[i] + "," + blueColor[i] + "," + opacity + "); ");
        }
    }

【讨论】:

  • 我认为相同的解决方案:)
  • @Jose 是否只能使用样式 8“默认颜色”?因为当我尝试使用 Value 10 时,我得到了一个空指针
  • 用任意数量系列的解决方案编辑了我的答案。
【解决方案2】:

你可以用这个:

chart-series-area-line series<i> default-color<j>

在哪里是系列的索引,是系列的颜色索引

正如 css 参考 this 中指定的那样,您可以将其与 areaChart 的 setStyle 函数一起使用

你可以用这个:

Node line = areaChart.lookup(".default-color0.chart-series-area-line");
line.setStyle("-fx-stroke: rgb(" + redColor + "," + greenColor + "," + blueColor + "); ");

【讨论】:

  • 你能再描述一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 2016-05-05
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多