2

我有一张图表,其中的数据堆叠在 (-,+)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 示例 - “我更改系列值”

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);
}

}

  • 无负值。 无负值
  • 负值数据已损坏。 负值数据已损坏
4

2 回答 2

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-更新“dataItemAdded & 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 和 layoutPlotChildren 更新这两种方法来管理负值和正值轴上的条形长度。

于 2013-03-18T20:17:39.997 回答
-1

利用...

http://download.oracle.com/otndocs/products/javafx/2.2/samples/Ensemble/index.html

或者

JavaFX 场景生成器 1.1

在此处输入图像描述

于 2013-03-15T11:49:30.787 回答