Example usage for javafx.scene.chart BarChart BarChart

List of usage examples for javafx.scene.chart BarChart BarChart

Introduction

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

Prototype

public BarChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis) 

Source Link

Document

Construct a new BarChart with the given axis.

Usage

From source file:Main.java

@Override
public void start(final Stage stage) {
    stage.setTitle("");
    stage.setWidth(500);/* w  ww  . j a  v a2s .  c o m*/
    stage.setHeight(500);
    Scene scene = new Scene(new Group());

    VBox root = new VBox();
    NumberAxis lineYAxis = new NumberAxis(0, 100, 10);
    CategoryAxis lineXAxis = new CategoryAxis();

    BarChart barChart = new BarChart(lineXAxis, lineYAxis);

    lineYAxis.setLabel("Sales");
    lineXAxis.setLabel("Products");

    root.getChildren().addAll(barChart);
    scene.setRoot(root);

    stage.setScene(scene);
    stage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    final NumberAxis xAxis = new NumberAxis();
    final CategoryAxis yAxis = new CategoryAxis();
    final BarChart<Number, String> bc = new BarChart<Number, String>(xAxis, yAxis);
    bc.setTitle("Summary");
    xAxis.setLabel("Value");
    xAxis.setTickLabelRotation(90);/* ww w .ja v  a 2s .  c  o  m*/
    yAxis.setLabel("Item");

    XYChart.Series series1 = new XYChart.Series();
    series1.setName("2003");
    series1.getData().add(new XYChart.Data(2, itemA));
    series1.getData().add(new XYChart.Data(20, itemB));
    series1.getData().add(new XYChart.Data(10, itemC));

    XYChart.Series series2 = new XYChart.Series();
    series2.setName("2004");
    series2.getData().add(new XYChart.Data(50, itemA));
    series2.getData().add(new XYChart.Data(41, itemB));
    series2.getData().add(new XYChart.Data(45, itemC));

    XYChart.Series series3 = new XYChart.Series();
    series3.setName("2005");
    series3.getData().add(new XYChart.Data(45, itemA));
    series3.getData().add(new XYChart.Data(44, itemB));
    series3.getData().add(new XYChart.Data(18, itemC));

    Timeline tl = new Timeline();
    tl.getKeyFrames().add(new KeyFrame(Duration.millis(500), new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            for (XYChart.Series<Number, String> series : bc.getData()) {
                for (XYChart.Data<Number, String> data : series.getData()) {
                    data.setXValue(Math.random() * 100);
                }
            }
        }
    }));
    tl.setCycleCount(Animation.INDEFINITE);
    tl.play();

    Scene scene = new Scene(bc, 800, 600);
    bc.getData().addAll(series1, series2, series3);
    stage.setScene(scene);
    stage.show();
}

From source file:org.jacp.demo.components.ContactChartViewComponent.java

protected BarChart<String, Number> createChart() {

    this.xAxis = new CategoryAxis();
    this.yAxis = new NumberAxis();
    this.yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(this.yAxis, "$", null));
    this.bc = new BarChart<String, Number>(this.xAxis, this.yAxis);
    this.bc.setAnimated(true);
    this.bc.setTitle(" ");

    this.xAxis.getStyleClass().add("jacp-bar");
    this.yAxis.getStyleClass().add("jacp-bar");
    this.xAxis.setLabel("Year");
    this.yAxis.setLabel("Price");

    this.series1 = new XYChart.Series<String, Number>();
    this.series1.setName("electronic");
    this.series2 = new XYChart.Series<String, Number>();
    this.series2.setName("clothes");
    this.series3 = new XYChart.Series<String, Number>();
    this.series3.setName("hardware");
    this.series4 = new XYChart.Series<String, Number>();
    this.series4.setName("books");

    GridPane.setHalignment(this.bc, HPos.CENTER);
    GridPane.setVgrow(this.bc, Priority.ALWAYS);
    GridPane.setHgrow(this.bc, Priority.ALWAYS);
    GridPane.setMargin(this.bc, new Insets(0, 6, 0, 0));
    return this.bc;
}

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  w w.  j av a  2s  . c  om*/
    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:com.jscriptive.moneyfx.ui.chart.ChartFrame.java

/**
 * This method is invoked when the monthly in/out button has been toggled
 *
 * @param actionEvent/*from www  . ja  v a  2  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();
    }
}

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);/*w  w  w. ja  v  a 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:UI.MainStageController.java

/**
 * Displays an abundance plot of the selected taxa
 *///from w w w  .j a  v a  2s  .  co m
@FXML
private void displayAbundancePlot() {
    ObservableList selectedItems = LoadedData.getGraphView().getSelectionModel().getSelectedItems();
    List<TaxonNode> nodesList = new LinkedList<>();
    for (Object selectedItem : selectedItems) {
        nodesList.add(((MyVertex) selectedItem).getTaxonNode());
    }
    HashMap<Sample, HashMap<TaxonNode, Integer>> abundancesMap = SampleComparison.calcAbundances(nodesList);

    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Taxa");
    yAxis.setLabel("Abundance");
    BarChart<String, Number> abundancePlot = new BarChart<>(xAxis, yAxis);

    for (Map.Entry<Sample, HashMap<TaxonNode, Integer>> entry : abundancesMap.entrySet()) {
        XYChart.Series<String, Number> sampleSeries = new XYChart.Series<>();
        sampleSeries.setName(entry.getKey().getName());
        for (Map.Entry<TaxonNode, Integer> innerMapEntry : entry.getValue().entrySet()) {
            sampleSeries.getData()
                    .add(new XYChart.Data<>(innerMapEntry.getKey().getName(), innerMapEntry.getValue()));
        }
        abundancePlot.getData().add(sampleSeries);
    }

    //Display chart on a new pane
    Stage chartStage = new Stage();
    chartStage.setTitle("Abundance Plot");
    Scene chartScene = new Scene(abundancePlot);
    chartStage.setScene(chartScene);
    chartStage.show();
}

From source file:de.ifsr.adam.ImageGenerator.java

/**
 * Generates a bar chart/*from w w w .  j  a  va2  s. c  om*/
 *
 * @param question A question JSONObject of a report
 * @return
 */
private BarChart generateBarChart(JSONObject question) {

    XYChart.Series data;
    BarChart<Number, String> chart;

    JSONObject surveyQuestion;
    JSONObject answerType;
    JSONObject result;

    try {

        surveyQuestion = getSurveyQuestion(question.getString("question"));
        try {
            answerType = getAnswerType(surveyQuestion.getString("type"));
        } catch (NullPointerException e) {
            log.error("The answerType of " + question + " wasnt generated due to a Nullpointer");
            log.debug("", e);
            return null;
        }

        result = question.getJSONObject("result");
        data = generateDataBarChart(result, answerType);

        //Axis
        CategoryAxis yAxis = new CategoryAxis();
        yAxis.setAnimated(false); //Needs to be set, otherwise the labels are not printed to a file in the end.
        NumberAxis xAxis = new NumberAxis();
        xAxis.setLabel("Stimmen"); //TODO: Multi Language support.
        xAxis.setAnimated(false); //Needs to be set, otherwise the labels are not printed to a file in the end.

        chart = new BarChart<>(xAxis, yAxis);
        chart.getData().addAll(data);
        chart.setTitle(surveyQuestion.getString("text"));
        chart.setLegendVisible(false);
    } catch (JSONException e) {
        log.error(e);
        return null;
    }

    return chart;
}