JavaFx adding a text label on top of StackedBarChart

烈酒焚心 提交于 2020-01-05 08:40:57

问题


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.

  • 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.

  • Any help are welcome.


回答1:


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.



来源:https://stackoverflow.com/questions/17838490/javafx-adding-a-text-label-on-top-of-stackedbarchart

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