Example usage for javafx.scene.chart CategoryAxis setCategories

List of usage examples for javafx.scene.chart CategoryAxis setCategories

Introduction

In this page you can find the example usage for javafx.scene.chart CategoryAxis setCategories.

Prototype

public final void setCategories(ObservableList<String> value) 

Source Link

Document

The ordered list of categories plotted on this axis.

Usage

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    Group root = new Group();

    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();

    xAxis.setLabel("Month");
    xAxis.setCategories(
            FXCollections.<String>observableArrayList(Arrays.asList("January", "February", "March")));
    yAxis.setLabel("Value");

    final StackedBarChart<String, Number> stackedBarChart = new StackedBarChart<String, Number>(xAxis, yAxis);
    stackedBarChart.setTitle("StackedBarChart");

    XYChart.Series<String, Number> series1 = new XYChart.Series();
    series1.setName("XYChart.Series 1");

    series1.getData().add(new XYChart.Data("January", 100));
    series1.getData().add(new XYChart.Data("February", 200));
    series1.getData().add(new XYChart.Data("March", 50));

    XYChart.Series<String, Number> series2 = new XYChart.Series();
    series2.setName("XYChart.Series 2");

    series2.getData().add(new XYChart.Data("January", 150));
    series2.getData().add(new XYChart.Data("February", 100));
    series2.getData().add(new XYChart.Data("March", 60));

    stackedBarChart.getData().addAll(series1, series2);

    root.getChildren().addAll(stackedBarChart);

    primaryStage.setScene(new Scene(root, 500, 400));
    primaryStage.show();//from   w  w w.  j  a v a  2 s. c o m
}

From source file:gui.accessories.BattleSimFx.java

private BarChart createBarChart() {
    CategoryAxis xAxis = new CategoryAxis();
    xAxis.setCategories(FXCollections.<String>observableArrayList(tableModel.getColumnNames()));
    xAxis.setLabel("Year");

    double tickUnit = tableModel.getTickUnit();

    NumberAxis yAxis = new NumberAxis();
    yAxis.setTickUnit(tickUnit);// w ww .  j  a va2 s .c o m
    yAxis.setLabel("Units Sold");

    final BarChart aChart = new BarChart(xAxis, yAxis, tableModel.getBarChartData());
    tableModel.addTableModelListener(new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent e) {
            if (e.getType() == TableModelEvent.UPDATE) {
                final int row = e.getFirstRow();
                final int column = e.getColumn();
                final Object value = ((SampleTableModel) e.getSource()).getValueAt(row, column);

                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        XYChart.Series<String, Number> s = (XYChart.Series<String, Number>) aChart.getData()
                                .get(row);
                        BarChart.Data data = s.getData().get(column);
                        data.setYValue(value);
                    }
                });
            }
        }
    });
    return aChart;
}

From source file:gui.accessories.GraphPopup.java

private BarChart createBarChartDynamic() {
    SampleTableModel tableModel = new SampleTableModel();
    CategoryAxis xAxis = new CategoryAxis();
    xAxis.setCategories(FXCollections.<String>observableArrayList(tableModel.getColumnNames()));
    xAxis.setLabel("Year");

    double tickUnit = tableModel.getTickUnit();

    NumberAxis yAxis = new NumberAxis();
    yAxis.setTickUnit(tickUnit);/*  w ww. j a  v a 2  s  .co  m*/
    yAxis.setLabel("Units Sold");

    final BarChart aChart = new BarChart(xAxis, yAxis, tableModel.getBarChartData());
    aChart.setAnimated(true);
    tableModel.addTableModelListener(new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent e) {
            if (e.getType() == TableModelEvent.UPDATE) {
                final int row = e.getFirstRow();
                final int column = e.getColumn();
                final Object value = ((SampleTableModel) e.getSource()).getValueAt(row, column);

                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        XYChart.Series<String, Number> s = (XYChart.Series<String, Number>) aChart.getData()
                                .get(row);
                        BarChart.Data data = s.getData().get(column);
                        data.setYValue(value);
                    }
                });
            }
        }
    });
    return aChart;
}

From source file:SampleTableModel.java

private BarChart createBarChart() {
    CategoryAxis xAxis = new CategoryAxis();
    xAxis.setCategories(FXCollections.<String>observableArrayList(tableModel.getColumnNames()));
    xAxis.setLabel("Year");

    double tickUnit = tableModel.getTickUnit();

    NumberAxis yAxis = new NumberAxis();
    yAxis.setTickUnit(tickUnit);/*from  w  w  w  .  j  ava2 s. co  m*/
    yAxis.setLabel("Units Sold");

    final BarChart chart = new BarChart(xAxis, yAxis, tableModel.getBarChartData());
    tableModel.addTableModelListener(new TableModelListener() {

        public void tableChanged(TableModelEvent e) {
            if (e.getType() == TableModelEvent.UPDATE) {
                final int row = e.getFirstRow();
                final int column = e.getColumn();
                final Object value = ((SampleTableModel) e.getSource()).getValueAt(row, column);

                Platform.runLater(new Runnable() {
                    public void run() {
                        XYChart.Series<String, Number> s = (XYChart.Series<String, Number>) chart.getData()
                                .get(row);
                        BarChart.Data data = s.getData().get(column);
                        data.setYValue(value);
                    }
                });
            }
        }
    });
    return chart;
}

From source file:gui.accessories.GraphPopup.java

private BarChart createBarChart() {
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();

    List<Nacao> nations = new ArrayList<Nacao>(WorldFacadeCounselor.getInstance().getNacoes().values());
    ComparatorFactory.getComparatorNationVictoryPointsSorter(nations);
    xAxis.setCategories(FXCollections.<String>observableList(getNames(nations)));

    //format axis
    yAxis.setTickUnit(200);//from  w  ww  .  j av  a2 s  .  co  m
    yAxis.setLabel(labels.getString("PONTOS.VITORIA"));
    //xAxis.setLabel(labels.getString("NACAO"));
    xAxis.setTickMarkVisible(false);

    //Series 1
    XYChart.Series<Number, String> series = new XYChart.Series();
    //series1.setName("XYChart.Series 1");

    for (Nacao nation : WorldFacadeCounselor.getInstance().getNacoes().values()) {
        final XYChart.Data data = new XYChart.Data(nation.getPontosVitoria(), nation.getNome());
        series.getData().add(data);
    }

    //
    final BarChart<Number, String> stackedBarChart = new BarChart<Number, String>(yAxis, xAxis);
    //        stackedBarChart.setTitle(labels.getString("PONTOS.VITORIA"));
    stackedBarChart.getData().addAll(series);
    stackedBarChart.setLegendVisible(false);
    //        stackedBarChart.setCategoryGap(0.2);

    return stackedBarChart;
}

From source file:gui.accessories.GraphPopup.java

private StackedBarChart createStackedBarChart() {
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();

    List<Nacao> nations = new ArrayList<Nacao>(WorldFacadeCounselor.getInstance().getNacoes().values());
    ComparatorFactory.getComparatorNationVictoryPointsSorter(nations);
    xAxis.setCategories(FXCollections.<String>observableList(getNames(nations)));

    //format axis
    yAxis.setTickUnit(200);//from  ww w .j a v a2  s  .  c  o  m
    yAxis.setLabel(labels.getString("PONTOS.VITORIA"));
    //xAxis.setLabel(labels.getString("NACAO"));
    xAxis.setTickMarkVisible(false);

    //Series 1
    XYChart.Series<Number, String> series = new XYChart.Series();
    //series1.setName("XYChart.Series 1");

    for (Nacao nation : WorldFacadeCounselor.getInstance().getNacoes().values()) {
        final XYChart.Data data = new XYChart.Data(nation.getPontosVitoria(), nation.getNome());
        series.getData().add(data);
    }

    //
    final StackedBarChart<Number, String> stackedBarChart = new StackedBarChart<Number, String>(yAxis, xAxis);
    //        stackedBarChart.setTitle(labels.getString("PONTOS.VITORIA"));
    stackedBarChart.getData().addAll(series);
    stackedBarChart.setLegendVisible(false);
    //        stackedBarChart.setCategoryGap(0.2);

    return stackedBarChart;
}

From source file:com.jscriptive.moneyfx.ui.chart.ChartFrame.java

public void yearlyInOutToggled(ActionEvent actionEvent) {
    final NumberAxis xAxis = new NumberAxis();
    final CategoryAxis yAxis = new CategoryAxis();
    yAxis.setLabel("In/Out in Euro");
    xAxis.setLabel("Year");

    final BarChart<Number, String> barChart = new BarChart<>(xAxis, yAxis);
    barChart.setTitle("Yearly in/out");

    chartFrame.setCenter(barChart);/*www .java  2s .  c o  m*/

    ToggleButton toggle = (ToggleButton) actionEvent.getTarget();
    if (toggle.isSelected()) {
        Account account = accountCombo.getValue();
        String accountLabel = getAccountLabel(account);

        XYChart.Series<Number, String> inSeries = new XYChart.Series<>();
        inSeries.setName("In" + accountLabel);
        barChart.getData().add(inSeries);

        XYChart.Series<Number, String> outSeries = new XYChart.Series<>();
        outSeries.setName("Out" + accountLabel);
        barChart.getData().add(outSeries);

        ValueRange<LocalDate> period = getTransactionOpRange(account, yearCombo.getValue());
        if (period.isEmpty()) {
            return;
        }
        ObservableList<String> categories = FXCollections.observableArrayList();
        for (int y = period.from().getYear(); y < period.to().getYear() + 6; y++) {
            categories.add(String.valueOf(y));
        }
        yAxis.setCategories(categories);
        Service<Void> service = new Service<Void>() {
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override
                    protected Void call() throws Exception {

                        List<TransactionVolume> incomingVolumes = (account == ALL_ACCOUNTS)
                                ? transactionRepository.getYearlyIncomingVolumes(false)
                                : transactionRepository.getYearlyIncomingVolumesOfAccount(account, false);
                        if (INTEGER_ZERO.compareTo(yearCombo.getValue()) < 0) {
                            incomingVolumes = incomingVolumes.stream()
                                    .filter(v -> v.getYear().equals(yearCombo.getValue()))
                                    .sorted((v1, v2) -> v1.getDate().compareTo(v2.getDate())).collect(toList());
                        }
                        for (TransactionVolume volume : incomingVolumes) {
                            XYChart.Data<Number, String> inData = new XYChart.Data<>(volume.getVolume(),
                                    String.valueOf(volume.getYear()));
                            Platform.runLater(() -> {
                                inSeries.getData().add(inData);
                                StackPane node = (StackPane) inData.getNode();
                                node.getChildren().add(
                                        new Label(CurrencyFormat.getInstance().format(volume.getVolume())));
                                node.addEventHandler(MOUSE_CLICKED,
                                        event -> handleYearlyInOutChartMouseClickEvent(
                                                (account == ALL_ACCOUNTS) ? null : account,
                                                ofYearDay(volume.getYear(), 1), event));
                            });
                        }

                        List<TransactionVolume> outgoingVolumes = (account == ALL_ACCOUNTS)
                                ? transactionRepository.getYearlyOutgoingVolumes(false)
                                : transactionRepository.getYearlyOutgoingVolumesOfAccount(account, false);
                        if (INTEGER_ZERO.compareTo(yearCombo.getValue()) < 0) {
                            outgoingVolumes = outgoingVolumes.stream()
                                    .filter(v -> v.getYear().equals(yearCombo.getValue()))
                                    .sorted((v1, v2) -> v1.getDate().compareTo(v2.getDate())).collect(toList());
                        }
                        for (TransactionVolume volume : outgoingVolumes) {
                            XYChart.Data<Number, String> outData = new XYChart.Data<>(volume.getVolume().abs(),
                                    String.valueOf(volume.getYear()));
                            Platform.runLater(() -> {
                                outSeries.getData().add(outData);
                                StackPane node = (StackPane) outData.getNode();
                                node.getChildren().add(
                                        new Label(CurrencyFormat.getInstance().format(volume.getVolume())));
                                node.addEventHandler(MOUSE_CLICKED,
                                        event -> handleYearlyInOutChartMouseClickEvent(
                                                (account == ALL_ACCOUNTS) ? null : account,
                                                ofYearDay(volume.getYear(), 1), event));
                            });
                        }

                        return null;
                    }
                };
            }
        };
        service.start();

    }
}

From source file:com.jscriptive.moneyfx.ui.chart.ChartFrame.java

/**
 * This method is invoked when the monthly in/out button has been toggled
 *
 * @param actionEvent/*from w ww. j  a  v a2  s  .  c o  m*/
 */
public void monthlyInOutToggled(ActionEvent actionEvent) {
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Month of Year");
    yAxis.setLabel("In/Out in Euro");

    final BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis);
    barChart.setTitle("Monthly in/out");

    chartFrame.setCenter(barChart);

    ToggleButton toggle = (ToggleButton) actionEvent.getTarget();
    if (toggle.isSelected()) {
        Account account = accountCombo.getValue();
        String accountLabel = getAccountLabel(account);

        XYChart.Series<String, Number> inSeries = new XYChart.Series<>();
        inSeries.setName("In" + accountLabel);
        barChart.getData().add(inSeries);

        XYChart.Series<String, Number> outSeries = new XYChart.Series<>();
        outSeries.setName("Out" + accountLabel);
        barChart.getData().add(outSeries);

        ValueRange<LocalDate> period = getTransactionOpRange(account, yearCombo.getValue());
        if (period.isEmpty()) {
            return;
        }
        ObservableList<String> monthLabels = FXCollections.observableArrayList();
        for (LocalDate date = period.from().withDayOfMonth(1); !date.isAfter(period.to()); date = date
                .plusMonths(1)) {
            monthLabels.add(getMonthLabel(date.getYear(), date.getMonthValue()));
        }
        xAxis.setCategories(monthLabels);
        Service<Void> service = new Service<Void>() {
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override
                    protected Void call() throws Exception {

                        List<TransactionVolume> incomingVolumes = (account == ALL_ACCOUNTS)
                                ? transactionRepository.getMonthlyIncomingVolumes(false)
                                : transactionRepository.getMonthlyIncomingVolumesOfAccount(account, false);
                        if (INTEGER_ZERO.compareTo(yearCombo.getValue()) < 0) {
                            incomingVolumes = incomingVolumes.stream()
                                    .filter(v -> v.getYear().equals(yearCombo.getValue()))
                                    .sorted((v1, v2) -> v1.getDate().compareTo(v2.getDate())).collect(toList());
                        }
                        for (TransactionVolume volume : incomingVolumes) {
                            String monthLabel = getMonthLabel(volume.getYear(), volume.getMonth());
                            XYChart.Data<String, Number> data = new XYChart.Data<>(monthLabel,
                                    volume.getVolume());
                            Platform.runLater(() -> {
                                inSeries.getData().add(data);
                                StackPane barNode = (StackPane) data.getNode();
                                // TODO make that look nicer
                                Label labelNode = new Label(
                                        CurrencyFormat.getInstance().format(volume.getVolume()));
                                labelNode.setPrefWidth(100);
                                labelNode.setAlignment(CENTER_RIGHT);
                                labelNode.setRotate(270);
                                barNode.getChildren().add(labelNode);
                                barNode.addEventHandler(MOUSE_CLICKED,
                                        event -> handleMonthlyInOutChartMouseClickEvent(
                                                (account == ALL_ACCOUNTS) ? null : account,
                                                of(volume.getYear(), volume.getMonth(), 1), event));
                            });
                        }

                        List<TransactionVolume> outgoingVolumes = (account == ALL_ACCOUNTS)
                                ? transactionRepository.getMonthlyOutgoingVolumes(false)
                                : transactionRepository.getMonthlyOutgoingVolumesOfAccount(account, false);
                        if (INTEGER_ZERO.compareTo(yearCombo.getValue()) < 0) {
                            outgoingVolumes = outgoingVolumes.stream()
                                    .filter(v -> v.getYear().equals(yearCombo.getValue()))
                                    .sorted((v1, v2) -> v1.getDate().compareTo(v2.getDate())).collect(toList());
                        }
                        for (TransactionVolume volume : outgoingVolumes) {
                            String monthLabel = getMonthLabel(volume.getYear(), volume.getMonth());
                            XYChart.Data<String, Number> data = new XYChart.Data<>(monthLabel,
                                    volume.getVolume().abs());
                            Platform.runLater(() -> {
                                outSeries.getData().add(data);
                                StackPane node = (StackPane) data.getNode();
                                // TODO make that look nicer
                                Label labelNode = new Label(
                                        CurrencyFormat.getInstance().format(volume.getVolume()));
                                labelNode.setPrefWidth(100);
                                labelNode.setAlignment(CENTER_RIGHT);
                                labelNode.setRotate(270);
                                node.getChildren().add(labelNode);
                                node.addEventHandler(MOUSE_CLICKED,
                                        event -> handleMonthlyInOutChartMouseClickEvent(
                                                (account == ALL_ACCOUNTS ? null : account), volume.getDate(),
                                                event));
                            });
                        }

                        return null;
                    }
                };
            }
        };
        service.start();
    }
}