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.
But when using javafx StackedBar Chart , when bar series have (positive & negative) data stacked bar corrupted and loss data.
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.
- With minus value data corrupted.
I found the bug on the StackedBarChart code:
That Line of code mean that each series has
map<String categoryPoint ,data bar>
,so that series has onlyone bar for that categoryPoint
, and that bar will be the last value putted into map as map will clear previous bar data.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.
After that we have to update the code depend on that data Structure.
1-Update 'dataItemAdded & seriesAdded' methods.
2-getDataItem method.
3-updateAxisRange & layoutPlotChildren update that two method to manage bar length over negative and positive values axis.
Updated code for StackedBarChart
Oracle StackedBarChart Sample with positive and negative bars
StackedBarChart show bar perfectly without any data corruption
Use...
http://download.oracle.com/otndocs/products/javafx/2.2/samples/Ensemble/index.html
or
JavaFX Scene Builder 1.1