JavaFx adding a text label on top of StackedBarCha

2019-07-24 07:34发布

After Search

  • I found this sample how to display bar value on top of bar javafx, But that when bar separated on the category axis so each category data has a single bar and Listener can be added to that data (Category).

  • But with StackedBarChart each category consists of StackedBar.

  • I try that code simple based on the @jewelsea code.

  • That solution work prefect if all series are symmetric and contain all the categories. symmetric Series

  • But with non-symmetric series

        ObservableList<StackedBarChart.Series> barChartData = FXCollections.observableArrayList(
            new StackedBarChart.Series("Region 1", FXCollections.observableArrayList(
            new StackedBarChart.Data(years[0], 567d),
            new StackedBarChart.Data(years[1], 1292d),
            new StackedBarChart.Data(years[2], 1292d))),
            new StackedBarChart.Series("Region 2", FXCollections.observableArrayList(
            new StackedBarChart.Data(years[0], 956),
            new StackedBarChart.Data(years[1], 1665),
            new StackedBarChart.Data(years[2], 2559))),
            new StackedBarChart.Series("Region 3", FXCollections.observableArrayList(
            new StackedBarChart.Data(years[0], 1154),
            //new StackedBarChart.Data(years[1], 1927),// series names Region 3(which is the last series on the chart data) doesn't exist category years[1] "2008".
            new StackedBarChart.Data(years[2], 2774))));
    
  • We will miss Text on top of category 2008. non-symmetric series miss text on the missing categories

  • Any help are welcome.

标签: javafx-2
1条回答
狗以群分
2楼-- · 2019-07-24 08:00

It's missing because you comment it out:

    //new StackedBarChart.Data(years[1], 1927),// series names Region 3(which is the last series on the chart data) doesn't exist category years[1] "2008".

That second parameter is the "1927" title. This is technically the Y-Axis parameter. You need to keep that line in order to show that "1927". Check out the API:

http://docs.oracle.com/javafx/2/api/javafx/scene/chart/StackedBarChart.html

You may need to put your "titles" up another way. Use a text class and add it on top of the StackedBarChart.Series objects.

查看更多
登录 后发表回答