使用小值在JavaFX中堆叠图表

时间:2016-09-28 14:04:55

标签: java javafx

我想使用堆叠图表(条形图和折线图)来显示数据,我找到了本教程: Stacking charts in JavaFX

效果很好,但是当我使用小值列表时,结果显示不正确。

这是代码示例:

public class StackedChartSample extends Application {

    final static String austria = "Austria";
    final static String brazil = "Brazil";
    final static String france = "France";
    final static String italy = "Italy";
    final static String usa = "USA";

    @Override
    public void start(Stage stage) {

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

        // base chart
        final BarChart<String, Number> barChart = new BarChart<String, Number>(xAxis, yAxis);
        barChart.setLegendVisible(false);
        barChart.setAnimated(false);
        barChart.setTitle("Equipe1 Chart");
        barChart.setStyle("CHART_COLOR_1: blue;");

        xAxis.setLabel("Country");
        yAxis.setLabel("Value");

        // overlay chart
        LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis);
        lineChart.setLegendVisible(false);
        lineChart.setAnimated(false);
        lineChart.setCreateSymbols(true);
        lineChart.setAlternativeRowFillVisible(false);
        lineChart.setAlternativeColumnFillVisible(false);
        lineChart.setHorizontalGridLinesVisible(false);
        lineChart.setVerticalGridLinesVisible(false);
        lineChart.getXAxis().setVisible(false);
        lineChart.getYAxis().setVisible(false);
        lineChart.getStylesheets().addAll(getClass().getResource("chart.css").toExternalForm());

        barChart.getData().add( createChartSeries());
        lineChart.getData().add( createChartSeries2());

        StackPane root = new StackPane();
        root.getChildren().addAll( barChart, lineChart);

        Scene scene = new Scene(root, 800, 600);

        stage.setScene(scene);
        stage.show();
    }

    private XYChart.Series<String,Number> createChartSeries() {

        XYChart.Series<String,Number> series = new XYChart.Series<String,Number>();
        series.getData().add(new XYChart.Data<String,Number>("frf", 68));
        series.getData().add(new XYChart.Data<String,Number>("fef", 67.82));
        series.getData().add(new XYChart.Data<String,Number>("fdf", 50));
        series.getData().add(new XYChart.Data<String,Number>("fsf", 43.15));
        series.getData().add(new XYChart.Data<String,Number>("faf", 70));
        series.getData().add(new XYChart.Data<String,Number>("fxf", 68,34));
        series.getData().add(new XYChart.Data<String,Number>("dd", 60.82));
        series.getData().add(new XYChart.Data<String,Number>("ddd", 50));
        series.getData().add(new XYChart.Data<String,Number>("dyy", 80.15));
        series.getData().add(new XYChart.Data<String,Number>("ccc", 85));

        return series;
    }

    private XYChart.Series<String,Number> createChartSeries2() {

        XYChart.Series<String,Number> series = new XYChart.Series<String,Number>();
        series.getData().add(new XYChart.Data<String,Number>("frf", 67));
        series.getData().add(new XYChart.Data<String,Number>("fef", 67));
        series.getData().add(new XYChart.Data<String,Number>("fdf", 67));
        series.getData().add(new XYChart.Data<String,Number>("fsf", 67));
        series.getData().add(new XYChart.Data<String,Number>("faf", 67));
        series.getData().add(new XYChart.Data<String,Number>("fxf", 67));
        series.getData().add(new XYChart.Data<String,Number>("dd", 67));
        series.getData().add(new XYChart.Data<String,Number>("ddd", 67));
        series.getData().add(new XYChart.Data<String,Number>("dyy", 67));
        series.getData().add(new XYChart.Data<String,Number>("ccc", 67));
        return series;
    }

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

结果如下:

1 个答案:

答案 0 :(得分:0)

你必须设置:

  • lowerBound - 此轴的下限

  • upperBound - 此轴的上限,

  • tickUnit - 刻度单位,即刻度线之间的空格

    final NumberAxis yAxis = new NumberAxis(0,100,10);