Edv Beq Edv Beq - 4 months ago 24
Java Question

How to fill between two AreaCharts in JavaFX?

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.

enter image description here
enter image description here

How can I accomplish this? CSS is preferred.

Answer

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:
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:Screen/multiply

Comments