我有一个图表,数据堆放在( - ,+)Y轴上。
使用JfreeChart,所有这些都变得严谨,数据在每个栏上都能完美呈现而不会丢失。
但是当使用javafx StackedBar Chart时,条形系列有(正面和负面)数据堆叠条损坏和丢失数据。
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));
}
更新:
- Oracle StackedBarChart Sample - “我改变了系列值”
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);
}
}
答案 0 :(得分:1)
我在StackedBarChart代码中发现了错误:
private Map<Series, Map<String,Data<X, Y>>> seriesCategoryMap = new HashMap<Series, Map<String, Data<X, Y>>>();
该行代码表示每个系列都有map<String categoryPoint ,data bar>
,因此该系列只有one bar for that categoryPoint
,并且该条形图将是放入地图的最后一个值,因为地图将清除以前的条形数据
categoryMap.put(categoryPoint,bar);
但我们需要seriesCategoryMap中的每个系列都有categoryMap但该地图必须有for each categoryPoint to be List<Data>
。
结论,对于每个系列,我们都有categoryMap,对于该categoryMap中的每个categoryPoint,我们都有List<Data<X,Y>> bar List to have positive and negative bars
。
这样代码就行了。
private Map<Series, Map<String, List<Data<X, Y>>>> seriesCategoryMap = new HashMap<Series, Map<String, List<Data<X, Y>>>>();
之后我们必须根据该数据结构更新代码。
1-Update'dataItemAdded&amp; seriesAdded'方法。
// 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方法。
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&amp; layoutPlotChildren更新了两种方法来管理负值和正值轴上的条形长度。
Oracle StackedBarChart Sample with positive and negative bars
StackedBarChart显示栏完全没有任何数据损坏
答案 1 :(得分:-1)