Created
January 7, 2013 02:37
-
-
Save jingglang/4471869 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.default-color8.chart-bar { -fx-bar-fill: #babdb6; } | |
.default-color9.chart-bar { -fx-bar-fill: #4e9a06; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2008, 2012 Oracle and/or its affiliates. | |
* All rights reserved. Use is subject to license terms. | |
*/ | |
import com.javafx.experiments.scenicview.ScenicView; | |
import javafx.application.Application; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.scene.Node; | |
import javafx.scene.chart.BarChart; | |
import javafx.scene.chart.CategoryAxis; | |
import javafx.scene.chart.NumberAxis; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.BorderPane; | |
/** | |
* A chart that displays rectangular bars with heights indicating data values | |
* for categories. Used for displaying information when at least one axis has | |
* discontinuous or discrete data. | |
* | |
* @see javafx.scene.chart.BarChart | |
* @see javafx.scene.chart.Chart | |
* @see javafx.scene.chart.Axis | |
* @see javafx.scene.chart.CategoryAxis | |
* @see javafx.scene.chart.NumberAxis | |
* | |
*/ | |
public class BarChartSample extends Application { | |
private static final int AVAILABLE_COLORS = 10; | |
private static final int CASPIAN_COLOR_COUNTS = 8; | |
private void init(Stage primaryStage) { | |
BorderPane root = new BorderPane(); | |
primaryStage.setScene(new Scene(root)); | |
String[] years = {"2010","2011", "2012"}; | |
CategoryAxis xAxis = new CategoryAxis(); | |
xAxis.setCategories(FXCollections.<String>observableArrayList(years)); | |
NumberAxis yAxis = new NumberAxis("Units Sold", 0.0d, 3000.0d, 1000.0d); | |
ObservableList<BarChart.Series> barChartData = FXCollections.observableArrayList( | |
new BarChart.Series("Watermelons", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 1292d), | |
new BarChart.Data(years[1], 567d), | |
new BarChart.Data(years[2], 1000) | |
)), | |
new BarChart.Series("Pineapples", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 298), | |
new BarChart.Data(years[1], 100), | |
new BarChart.Data(years[2], 1270) | |
)), | |
new BarChart.Series("Coconuts", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 2156), | |
new BarChart.Data(years[1], 2000), | |
new BarChart.Data(years[2], 600) | |
)), | |
new BarChart.Series("Kiwis", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 1908), | |
new BarChart.Data(years[1], 1750), | |
new BarChart.Data(years[2], 2800) | |
)), | |
new BarChart.Series("Bananas", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 1356), | |
new BarChart.Data(years[1], 800), | |
new BarChart.Data(years[2], 2507) | |
)), | |
new BarChart.Series("Blueberries", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 987), | |
new BarChart.Data(years[1], 300), | |
new BarChart.Data(years[2], 1987) | |
)), | |
new BarChart.Series("Kurmas", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 1500), | |
new BarChart.Data(years[1], 567d), | |
new BarChart.Data(years[2], 437) | |
)), | |
new BarChart.Series("Mangos", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 1292d), | |
new BarChart.Data(years[1], 500), | |
new BarChart.Data(years[2], 609) | |
)), | |
new BarChart.Series("Apples", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 2587), | |
new BarChart.Data(years[1], 567d), | |
new BarChart.Data(years[2], 1398) | |
)), | |
new BarChart.Series("Lemons", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 2559), | |
new BarChart.Data(years[1], 956), | |
new BarChart.Data(years[2], 1984) | |
)), | |
new BarChart.Series("Oranges", FXCollections.observableArrayList( | |
new BarChart.Data(years[0], 2774), | |
new BarChart.Data(years[1], 1154), | |
new BarChart.Data(years[2], 1927) | |
)) | |
); | |
BarChart chart = new BarChart(xAxis, yAxis, barChartData, 25.0d); | |
/** | |
* Set Series color | |
*/ | |
for (int i = 0; i < barChartData.size(); i++) { | |
for (Node node : chart.lookupAll(".series" + i)) { | |
node.getStyleClass().remove("default-color" + (i % CASPIAN_COLOR_COUNTS)); | |
node.getStyleClass().add("default-color" + (i % AVAILABLE_COLORS)); | |
} | |
} | |
/** | |
* Set Legend items color | |
*/ | |
int i = 0; | |
for (Node node : chart.lookupAll(".chart-legend-item")) { | |
if (node instanceof Label && ((Label) node).getGraphic() != null) { | |
((Label) node).getGraphic().getStyleClass().remove("default-color" + (i % CASPIAN_COLOR_COUNTS)); | |
((Label) node).getGraphic().getStyleClass().add("default-color" + (i % AVAILABLE_COLORS)); | |
} | |
i++; | |
} | |
root.setCenter(chart); | |
} | |
@Override public void start(Stage primaryStage) throws Exception { | |
init(primaryStage); | |
primaryStage.setTitle("10 Bar Colors"); | |
primaryStage.getScene().getStylesheets().add("barchart.css"); | |
primaryStage.show(); | |
ScenicView.show(primaryStage.getScene()); | |
} | |
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