Example usage for javafx.scene.chart XYChart.Data XYChart.Data

List of usage examples for javafx.scene.chart XYChart.Data XYChart.Data

Introduction

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

Prototype

public Data(X xValue, Y yValue) 

Source Link

Document

Creates an instance of XYChart.Data object and initializes the X,Y data values.

Usage

From source file:Main.java

private ObservableList<XYChart.Series<String, Double>> getChartData() {
    double aValue = 1.56;
    double bValue = 1.06;
    ObservableList<XYChart.Series<String, Double>> answer = FXCollections.observableArrayList();
    Series<String, Double> aSeries = new Series<String, Double>();
    Series<String, Double> bSeries = new Series<String, Double>();
    aSeries.setName("a");
    bSeries.setName("b");

    for (int i = 2011; i < 2021; i++) {
        aSeries.getData().add(new XYChart.Data(Integer.toString(i), aValue));
        aValue = aValue + Math.random() - .5;
        bSeries.getData().add(new XYChart.Data(Integer.toString(i), bValue));
        bValue = bValue + Math.random() - .5;
    }/*from  w w  w .j  av  a2  s.  co m*/
    answer.addAll(aSeries, bSeries);
    return answer;
}

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

/**
 * This method is invoked when the daily balance button has been toggled
 *
 * @param actionEvent/*  www. j  a v a  2  s  .  c om*/
 */
public void dailyBalanceToggled(ActionEvent actionEvent) {
    LocalDateAxis xAxis = new LocalDateAxis();
    NumberAxis yAxis = new NumberAxis();

    final LineChart<LocalDate, Number> lineChart = new LineChart<>(xAxis, yAxis);
    lineChart.setCreateSymbols(false);

    chartFrame.setCenter(lineChart);

    ToggleButton toggle = (ToggleButton) actionEvent.getTarget();
    if (toggle.isSelected()) {
        xAxis.setLabel("Day of year");
        yAxis.setLabel("Balance in Euro");
        lineChart.setTitle("Balance development day by day");

        ValueRange<LocalDate> period = getTransactionOpRange(accountCombo.getValue(), yearCombo.getValue());
        if (period.isEmpty()) {
            return;
        }
        xAxis.setLowerBound(period.from());
        xAxis.setUpperBound(period.to());

        Service<Void> service = new Service<Void>() {

            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {

                    @Override
                    protected Void call() throws Exception {
                        Map<Account, List<Transaction>> transactionMap = getTransactions(
                                accountCombo.getValue(), yearCombo.getValue());

                        transactionMap.entrySet().forEach(entry -> {
                            Account account = entry.getKey();
                            List<Transaction> transactionList = entry.getValue();

                            XYChart.Series<LocalDate, Number> series = new XYChart.Series<>();
                            series.setName(format("%s [%s]", account.toPresentableString(),
                                    account.getFormattedBalance()));

                            // sort transactions by operation value descending
                            transactionList.sort((t1, t2) -> t2.getDtOp().compareTo(t1.getDtOp()));
                            account.calculateStartingBalance(transactionList);
                            series.getData()
                                    .add(new XYChart.Data<>(account.getBalanceDate(), account.getBalance()));

                            // sort transactions by operation value ascending
                            transactionList.sort((t1, t2) -> t1.getDtOp().compareTo(t2.getDtOp()));
                            transactionList.forEach(trx -> {
                                account.calculateCurrentBalance(trx);
                                series.getData().add(
                                        new XYChart.Data<>(account.getBalanceDate(), account.getBalance()));
                            });

                            Platform.runLater(() -> lineChart.getData().add(series));
                        });

                        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  ava  2s.  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:UI.MainStageController.java

/**
 * Displays an abundance plot of the selected taxa
 *///from   w  w  w  . j a v a 2  s.  c  o  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: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);/*from  w w w  . ja  v  a  2s.  c  om*/

    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:de.ifsr.adam.ImageGenerator.java

/**
 * Creates the data series needed for creating a bar chart.
 *
 * @param result The result JSONObject you wish to transform
 * @param answerType The answer type JSONObject of the question
 * @return//from   w w w .j  ava2 s .  c om
 */
private XYChart.Series generateDataBarChart(JSONObject result, JSONObject answerType) {
    XYChart.Series series = new XYChart.Series();

    try {
        JSONObject answers;
        String[] fieldNames;

        try {
            answers = answerType.getJSONObject("answers");
            fieldNames = JSONObject.getNames(answers);
        } catch (NullPointerException e) {
            log.error("Missing JSONObject in result:" + result + " answerType: " + answerType);
            log.debug("", e);
            return series;
        }

        for (int i = 1; i < fieldNames.length; i++) { //i initialized with 1 to ignore the empty string result
            String answer = answers.getString(fieldNames[i]);
            Integer value;

            try {
                value = result.getInt(fieldNames[i]);
            } catch (JSONException e) {
                value = 0;
            }

            series.getData().add(new XYChart.Data(value, answer));
        }
    } catch (JSONException e) {
        log.error(e);
    }
    return series;
}

From source file:UI.MainStageController.java

/**
 * collects the data for the barChart/*www.jav  a 2 s . c  o m*/
 * data includes:
 * degree distribution
 * hubs
 */
public void displayGraphAnalysis() {
    //Generate Data for the BarChart
    GraphAnalysis analysis = AnalysisData.getAnalysis();
    HashMap<Integer, Double> degreeDistribution = analysis.getDegreeDistribution();
    XYChart.Series<String, Double> degreeSeries = new XYChart.Series<>();

    for (Map.Entry<Integer, Double> entry : degreeDistribution.entrySet()) {
        degreeSeries.getData().add(new XYChart.Data<>(entry.getKey().toString(), entry.getValue()));
    }
    degreeDistributionChart.getData().clear();
    degreeDistributionChart.getData().add(degreeSeries);

    //Generate Graph Statistics to display in the TextArea
    HashMap<TaxonNode, Integer> hubs = analysis.getHubsList();
    graphStatText.setText("List of Hubs:\n\n");

    //Sort hubs by descending values
    Map<TaxonNode, Integer> hubsSorted = hubs.entrySet().stream()
            .sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors
                    .toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

    for (Map.Entry<TaxonNode, Integer> entry : hubsSorted.entrySet()) {
        graphStatText
                .setText(graphStatText.getText() + entry.getKey().getName() + " (" + entry.getValue() + ")\n");
    }
}