package mint.javafx.dashboard.chart; /** * Copyright (c) 2008, 2012 Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. */ import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.chart.XYChart.Data; import javafx.scene.chart.XYChart.Series; import javafx.stage.Stage; import org.apache.commons.collections.Bag; import org.apache.commons.collections.bag.HashBag; /** * A chart in which lines connect a series of data points. Useful for viewing * data trends over time. * * @see javafx.scene.chart.LineChart * @see javafx.scene.chart.Chart * @see javafx.scene.chart.Axis * @see javafx.scene.chart.NumberAxis * @related charts/area/AreaChart * @related charts/scatter/ScatterChart */ public class LineChartSample extends Application { private void init(Stage primaryStage) { Group root = new Group(); primaryStage.setScene(new Scene(root)); CategoryAxis xAxis = new CategoryAxis(); NumberAxis yAxis = new NumberAxis(); Series series = createSeries(); disambiguateSeries(series); ObservableList> lineChartData = FXCollections.observableArrayList(series); LineChart chart = new LineChart(xAxis, yAxis, lineChartData); root.getChildren().add(chart); } private Series createSeries() { ObservableList> observableArrayList1 = FXCollections.observableArrayList( new XYChart.Data("Armani", 3.0), new XYChart.Data("Ayegbe", 0.0), new XYChart.Data("Batungbacal", 3.0), new XYChart.Data("Block", 0.0), new XYChart.Data("Bradford", 0.0), new XYChart.Data("Bris", 0.0), new XYChart.Data("Burnstein", 3.0), new XYChart.Data("Donovan", 0.0), new XYChart.Data("Gardner", 0.0), new XYChart.Data("Lamb", 0.0), new XYChart.Data("Li", 3.0), new XYChart.Data("Lichtenberg", 0.0), new XYChart.Data("Mack", 3.0), new XYChart.Data("Ollivier", 0.0), new XYChart.Data("Ouchi", 3.0), new XYChart.Data("Pierce", 0.0), new XYChart.Data("Rud", 3.0), new XYChart.Data("Russell", 0.0), new XYChart.Data("Russell", 3.0), new XYChart.Data("Salway", 0.0), new XYChart.Data("Saville", 3.0), new XYChart.Data("Seven", 0.0), new XYChart.Data("Shinozuka", 0.0), new XYChart.Data("Sugiri", 0.0), new XYChart.Data("Taplin", 3.0), new XYChart.Data("Trujillo", 0.0), new XYChart.Data("Vazquez", 0.0), new XYChart.Data("Wild", 0.0), new XYChart.Data("Alverez", 0.0), new XYChart.Data("Cloak", 3.0), new XYChart.Data("Green", 3.0), new XYChart.Data("Iler", 0.0), new XYChart.Data("Schettini", 0.0), new XYChart.Data("Sundquist", 3.0), new XYChart.Data("Baker", 0.0), new XYChart.Data("Bales", 0.0), new XYChart.Data("Beswick", 0.0), new XYChart.Data("Bullock", 0.0), new XYChart.Data("Cantrell", 0.0), new XYChart.Data("Crabbe", 0.0), new XYChart.Data("Duffy", 0.0), new XYChart.Data("Fenton", 0.0), new XYChart.Data("Fortis", 0.0), new XYChart.Data("Gvozdenko", 0.0), new XYChart.Data("Henderson", 0.0), new XYChart.Data("Jerez", 0.0), new XYChart.Data("Kesely", 0.0), new XYChart.Data("Lambert", 0.0), new XYChart.Data("Lane", 0.0), new XYChart.Data("Lee", 0.0), new XYChart.Data("Lockridge", 0.0), new XYChart.Data("Mitchell", 0.0), new XYChart.Data("PILE", 0.0), new XYChart.Data("Prates", 0.0), new XYChart.Data("Rawls", 0.0), new XYChart.Data("Raya", 0.0), new XYChart.Data("Shuler-Degrace", 0.0), new XYChart.Data("Smirnov", 0.0), new XYChart.Data("Steinberger", 0.0), new XYChart.Data("Torres", 0.0), new XYChart.Data("Warne-Reidsma", 0.0), new XYChart.Data("Wayman", 0.0), new XYChart.Data("Wilson", 0.0), new XYChart.Data("Wrynn", 0.0), new XYChart.Data("Christie", 0.0) ); ObservableList> observableArrayList = observableArrayList1; return new LineChart.Series("A330 / Theoretical /...", observableArrayList); } /** * Ensures that series x-values are unique. Charts do not automatically * aggregate duplicate x-values. Instead they seem to be skipped. To * prevent this, duplicate x-values are numbered with a unique suffix. */ private void disambiguateSeries(Series series) { // Collect all x-values. Bag bag = new HashBag(); for (Data data : series.getData()) bag.add(data.getXValue()); // Disambiguate the duplicates. for (Object s : bag.uniqueSet()) if (bag.getCount(s) > 1) { int i = 0; for (Data data : series.getData()) if (data.getXValue().equals(s)) data.setXValue(data.getXValue() + " (" + (++i) + ")"); } } @Override public void start(Stage primaryStage) throws Exception { init(primaryStage); primaryStage.show(); } public static void main(String[] args) { launch(args); } }