JavaFx StackedBar Chart Issue

2019-02-27 00:29发布

I have a chart with data stacked on (-,+)Y-Axis.

  • With JfreeChart all this go rigth and data appear perfectly on each bar without loss. enter image description here

  • But when using javafx StackedBar Chart , when bar series have (positive & negative) data stacked bar corrupted and loss data. enter image description here

                if (plusValue > 0) {
                    series.getData().add(new XYChart.Data<String, Number>(xPoint, plusValue));
                }
                if (minusValue > 0) {
                    series.getData().add(new XYChart.Data<String, Number>(xPoint, -minusValue));
                }
    

Update :

-Oracle StackedBarChart Sample - "I change Series value"

import java.util.Arrays;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class StackedBarChartSample 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";
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final StackedBarChart<String, Number> sbc =
        new StackedBarChart<String, Number>(xAxis, yAxis);
final XYChart.Series<String, Number> series1 =
        new XYChart.Series<String, Number>();
final XYChart.Series<String, Number> series2 =
        new XYChart.Series<String, Number>();
final XYChart.Series<String, Number> series3 =
        new XYChart.Series<String, Number>();

@Override
public void start(Stage stage) {
    stage.setTitle("Bar Chart Sample");
    sbc.setTitle("Country Summary");
    xAxis.setLabel("Country");
    xAxis.setCategories(FXCollections.<String>observableArrayList(
            Arrays.asList(austria, brazil, france, italy, usa)));
    yAxis.setLabel("Value");
    series1.setName("2003");
    // plus value
    series1.getData().add(new XYChart.Data<String, Number>(austria, 200));
    series1.getData().add(new XYChart.Data<String, Number>(brazil, 300));
    series1.getData().add(new XYChart.Data<String, Number>(france, 400));
    series1.getData().add(new XYChart.Data<String, Number>(italy, 500));
    series1.getData().add(new XYChart.Data<String, Number>(usa, 600));
    // minus value
    series1.getData().add(new XYChart.Data<String, Number>(austria, -200));
    series1.getData().add(new XYChart.Data<String, Number>(brazil, -300));
    series1.getData().add(new XYChart.Data<String, Number>(france, -400));
    series1.getData().add(new XYChart.Data<String, Number>(italy, -500));
    series1.getData().add(new XYChart.Data<String, Number>(usa, -600));
    series2.setName("2004");
    series2.getData().add(new XYChart.Data<String, Number>(austria, 200));
    series2.getData().add(new XYChart.Data<String, Number>(brazil, 300));
    series2.getData().add(new XYChart.Data<String, Number>(france, 400));
    series2.getData().add(new XYChart.Data<String, Number>(italy, 500));
    series2.getData().add(new XYChart.Data<String, Number>(usa, 600));
    // minus value
    series2.getData().add(new XYChart.Data<String, Number>(austria, -200));
    series2.getData().add(new XYChart.Data<String, Number>(brazil, -300));
    series2.getData().add(new XYChart.Data<String, Number>(france, -400));
    series2.getData().add(new XYChart.Data<String, Number>(italy, -500));
    series2.getData().add(new XYChart.Data<String, Number>(usa, -600));
    series3.setName("2005");
    series3.getData().add(new XYChart.Data<String, Number>(austria, 200));
    series3.getData().add(new XYChart.Data<String, Number>(brazil, 300));
    series3.getData().add(new XYChart.Data<String, Number>(france, 400));
    series3.getData().add(new XYChart.Data<String, Number>(italy, 500));
    series3.getData().add(new XYChart.Data<String, Number>(usa, 600));
    // minus value
    series3.getData().add(new XYChart.Data<String, Number>(austria, -200));
    series3.getData().add(new XYChart.Data<String, Number>(brazil, -300));
    series3.getData().add(new XYChart.Data<String, Number>(france, -400));
    series3.getData().add(new XYChart.Data<String, Number>(italy, -500));
    series3.getData().add(new XYChart.Data<String, Number>(usa, -600));
    Scene scene = new Scene(sbc, 800, 600);
    sbc.getData().addAll(series1, series2, series3);
    stage.setScene(scene);
    stage.show();
}

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

}

  • Without minus value. Without minus value
  • With minus value data corrupted. With minus value data corrupted

标签: javafx-2
2条回答
我想做一个坏孩纸
2楼-- · 2019-02-27 00:48

I found the bug on the StackedBarChart code:

    private Map<Series, Map<String,Data<X, Y>>> seriesCategoryMap = new HashMap<Series, Map<String, Data<X, Y>>>();
  • That Line of code mean that each series has map<String categoryPoint ,data bar>,so that series has only one bar for that categoryPoint, and that bar will be the last value putted into map as map will clear previous bar data.

    categoryMap.put(categoryPoint,bar); 
    
  • But we need for each series in seriesCategoryMap to have categoryMap But that map must have for each categoryPoint to be List<Data>.

  • Conclusions, for each series we have categoryMap and for each categoryPoint in that categoryMap we have List<Data<X,Y>> bar List to have positive and negative bars.

  • So that line of code become.

    private Map<Series, Map<String, List<Data<X, Y>>>> seriesCategoryMap = new HashMap<Series, Map<String, List<Data<X, Y>>>>();
    
  • After that we have to update the code depend on that data Structure.

1-Update 'dataItemAdded & seriesAdded' methods.

        // list to hold more that one bar "positive and negative"
    List<Data<X, Y>> itemList = categoryMap.get(category) != null ? categoryMap.get(category) : new ArrayList<Data<X, Y>>();
    itemList.add(item);
    categoryMap.put(category, itemList); 

2-getDataItem method.

    private List<Data<X, Y>> getDataItem(Series<X, Y> series, int seriesIndex, int itemIndex, String category) {
    Map<String, List<Data<X, Y>>> catmap = seriesCategoryMap.get(series);
    return catmap != null ? catmap.get(category) != null ? catmap.get(category) : new ArrayList<Data<X, Y>>() : new ArrayList<Data<X, Y>>();
}

3-updateAxisRange & layoutPlotChildren update that two method to manage bar length over negative and positive values axis.

查看更多
登录 后发表回答