Skip to content

Instantly share code, notes, and snippets.

@KhaledLela
Last active December 15, 2015 02:49
Show Gist options
  • Save KhaledLela/5190378 to your computer and use it in GitHub Desktop.
Save KhaledLela/5190378 to your computer and use it in GitHub Desktop.
StackedBarChart positive and negative bar values.
import java.util.Arrays;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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;
/**
*
* @author KhaledLela
* @see http://docs.oracle.com/javafx/2/charts/bar-chart.htm
*/
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();
CustomizedStackedBarChart<String, Number> sbc;
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) {
System.out.println("javafx.runtime.version: " + System.getProperties().get("javafx.runtime.version"));
stage.setTitle("Bar Chart Sample");
xAxis.setLabel("Country");
// yAxis.setAutoRanging(false);
// yAxis.setLowerBound(-2000);
// yAxis.setUpperBound(2000);
// yAxis.setTickUnit(400);
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));
ObservableList<XYChart.Series<String, Number>> seriesList = FXCollections.observableArrayList(series1, series2, series3);
sbc = new CustomizedStackedBarChart<String, Number>(xAxis, yAxis);
sbc.setTitle("Country Summary");
sbc.getData().setAll(seriesList);
Scene scene = new Scene(sbc, 800, 600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment