How to fill between two AreaCharts in JavaFX?

荒凉一梦 提交于 2020-01-05 04:09:09

问题


The Area Chart in JavaFX fills the area between the data line and the zero y-axis.

I would only like to know how to fill between two Area Charts.

More Details:

I am generating a bunch of random walks. Then, I create three different XYCharts for the Paths, the mean, and the mean +/- std. deviation. Finally, I use a StackPane to place each chart on top of each other with transparent background.

The order of the charts has to be the following: 1. Simulated paths chart on the very back (LineChart). 2. Std. Deviation chart (AreaChart). 3. The mean chart on top (LineChart).

Pictures - highlighting the problem.

How can I accomplish this? CSS is preferred.


回答1:


I managed to achieve the result using -fx-blend-mode property. Here is an example for two area charts:

public void start(Stage stage) throws Exception {

    stage.setTitle("Test");
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("X");
    final AreaChart<Number,Number> lineChart =
            new AreaChart<>(xAxis,yAxis);

    lineChart.setTitle("Test");

    XYChart.Series series1 = new XYChart.Series();
    series1.setName("S1");
    series1.getData().add(new XYChart.Data(0, 0));
    series1.getData().add(new XYChart.Data(20, 20));

    XYChart.Series series2 = new XYChart.Series();
    series2.setName("S2");
    series2.getData().add(new XYChart.Data(0, 0));
    series2.getData().add(new XYChart.Data(20, 10));

    Scene scene  = new Scene(lineChart,800,600);
    lineChart.getData().addAll(series1,series2);

    stage.setScene(scene);
    scene.getStylesheets().add("/test.css");
    stage.show();
}

And the css in test.css:

.default-color0.chart-series-area-fill {
    -fx-fill: #FF0000;
    -fx-blend-mode: difference;
}
.default-color1.chart-series-area-fill {
    -fx-fill: #FF0000;
    -fx-blend-mode: difference;
}

And the result:

Edit
CSS for screen/multiply:

.default-color0.chart-series-area-fill {
    -fx-fill: rgb(0,178,0);
    -fx-blend-mode: screen;
}

.default-color1.chart-series-area-fill {
    -fx-fill: rgb(255,246,255);
    -fx-blend-mode: multiply;
}

Result:



来源:https://stackoverflow.com/questions/38412470/how-to-fill-between-two-areacharts-in-javafx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!