1 Reply Latest reply: Mar 4, 2013 9:23 AM by shakir.gusaroff RSS

    Making a Stacked Bar chart Series Dynamic

    994645
      in the below example i have 3 series Region 1,2 & 3.. i want these series also to be dyanamic what ive done is used a mysql query using the DISTINCT CLAUSE got all the values of the attribute now depending on how many distinct values i have i need that many Series. n then the values for these Series again needs an invocation of the Mysql query.

      public class StackedBarChartSmple extends Application {

      private void init(Stage primaryStage) {
      Group root = new Group();
      primaryStage.setScene(new Scene(root));
      String[] years = {"2007", "2008", "2009"};
      CategoryAxis xAxis = CategoryAxisBuilder.create()
      .categories(FXCollections.<String>observableArrayList(years)).build();
      NumberAxis yAxis = NumberAxisBuilder.create()
      .label("Units Sold")
      .lowerBound(0.0d)
      .upperBound(10000.0d)
      .tickUnit(1000.0d).build();
      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),
      new StackedBarChart.Data(years[2], 2774)
      ))
      );

      StackedBarChart chart = new StackedBarChart(xAxis, yAxis, barChartData, 25.0d);
      root.getChildren().add(chart);
      }

      @Override public void start(Stage primaryStage) throws Exception {
      init(primaryStage);
      primaryStage.show();
      }
      public static void main(String[] args) { launch(args); }
      }
        • 1. Re: Making a Stacked Bar chart Series Dynamic
          shakir.gusaroff
          Hi. Does the following work for you? May be I am missing something.
             
                  ObservableList<StackedBarChart.Series> barChartData = FXCollections.observableArrayList();
                  Set<String> regions = new HashSet();  //from database 
                  for (String region : regions) {
                      barChartData.add(
                              new StackedBarChart.Series(region, FXCollections.observableArrayList(
                              new StackedBarChart.Data(years[0], 567d),     //values from db query
                              new StackedBarChart.Data(years[1], 1292d),
                              new StackedBarChart.Data(years[2], 1292d))));
          
          
                  }  
           
                 StackedBarChart chart = new StackedBarChart(xAxis, yAxis, barChartData, 25.0d);