Search code examples
javacsseclipsejavafxstacked-area-chart

JavaFX - StackedAreaChart and method setLowerBound


I hava some problem with Class StackedAreaChart and setLowerBound. When I set autoRangingProperty to true, everything is OK

http://i62.tinypic.com/2q0n3ht.png

But when I turn off autoRanging and set some params I hava some bugs..

http://i62.tinypic.com/2hdcn6p.png

import java.util.Random;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedAreaChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TestStackedAreaChart extends Application {

    StackPane mainGraphStackPane = null;
    BorderPane pane;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        // /
        Random generator = new Random();

        XYChart.Series<Number, Number> series1 = new XYChart.Series<>(); // line
        XYChart.Series<Number, Number> series2 = new XYChart.Series<>(); // down
        XYChart.Series<Number, Number> series3 = new XYChart.Series<>(); // up
        for (int i = 1; i < 50; i++) {
            Double value = generator.nextDouble() + 1;
            series1.getData().add(new XYChart.Data<Number, Number>(i, value));
            Double Ymin = value - generator.nextDouble() * 0.2;
            series2.getData().add(new XYChart.Data<Number, Number>(i, Ymin));
            Double Ymax = (value - Ymin) + generator.nextDouble() * 0.2;
            series3.getData().add(new XYChart.Data<Number, Number>(i, Ymax));

        }
        // /

        final NumberAxis xAxisLC = new NumberAxis();
        final NumberAxis yAxisLC = new NumberAxis();
        /////////what's wrong
        yAxisLC.autoRangingProperty().set(false);
        yAxisLC.setMinorTickLength(yAxisLC.getTickLength());
        yAxisLC.setMinorTickCount(0);
        yAxisLC.setUpperBound(2.5);
        yAxisLC.setLowerBound(1.0); //when I set 0.0 is OK
        yAxisLC.setTickUnit(0.2);
        ///////
        pane = new BorderPane();
        mainGraphStackPane = new StackPane();
        mainGraphStackPane.getChildren().add(pane);

        StackedAreaChart<Number, Number> areaChart = new StackedAreaChart<Number, Number>(
                xAxisLC, yAxisLC);
        areaChart.setCreateSymbols(false);
        areaChart.setAlternativeRowFillVisible(false);
        areaChart.setAnimated(false);
        areaChart.setLegendVisible(false);
        areaChart.getData().add(series2);
        areaChart.getData().add(series3);

        Scene scene = new Scene(mainGraphStackPane);
        mainGraphStackPane.getChildren().add(areaChart);
        mainGraphStackPane.getStylesheets().add("chart.css");
        stage.setScene(scene);
        Group root = new Group();
        pane.getChildren().add(root);
        stage.show();
        stage.setWidth(stage.getWidth() + 1);
        stage.setHeight(stage.getHeight() + 1);

    }
}

and CSS file

@CHARSET "UTF-8";
.default-color0.chart-area-symbol { -fx-background-color: #e9967a, #ffa07a; }
.default-color1.chart-area-symbol { -fx-background-color: #e9967a, #ffa07a; }

.default-color0.chart-series-area-line { -fx-stroke: #ffaa00; }
.default-color1.chart-series-area-line { -fx-stroke: #ffaa00; }

.default-color0.chart-series-area-fill { -fx-fill: #ffff0055; }
.default-color1.chart-series-area-fill { -fx-fill: #ffaa005

Who has Idea to solve it.


Solution

  • I had a similar issue with the StackedAreaChart and I had to effectively offset all the values before adding them to the chart so that the chart actually started at zero, and then adjusted the y-axis label to be correct.

    1. setAutoRanging(false);
    2. setForceZeroInRange(false);
    3. work out the range of the values you'll be adding to the graph, storing the maximum and minimum.
    4. add all the values with the minimum subtracted.
    5. setLowerBound(0);
    6. setUpperBound(...); to the maximum minus the minimum.
    7. setTickLabelFormatter on the y-axis, and in the StringConverter<Number> toString method add your minimum to the value before formatting it.

    e.g. you have a load of data in the range 3 to 10. You add them to the graph as the range 0 to 7. And the y-axis label for 0 would be adjusted to 3, 1 adjusted to 4, etc.

    My graph's quite complicated, but if I have time to simplify it down I'll post some example code, but the above should be enough to go on.