Example usage for javafx.scene.chart PieChart PieChart

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

Introduction

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

Prototype

public PieChart(ObservableList<PieChart.Data> data) 

Source Link

Document

Construct a new PieChart with the given data

Usage

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Imported Fruits");
    stage.setWidth(500);// w  w w  . jav a  2  s.co  m
    stage.setHeight(500);

    ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
            new PieChart.Data("Grapefruit", 13), new PieChart.Data("Oranges", 25),
            new PieChart.Data("Plums", 10), new PieChart.Data("Pears", 22), new PieChart.Data("Apples", 30));

    final PieChart chart = new PieChart(pieChartData);
    chart.setTitle("Imported Fruits");
    final Label caption = new Label("");
    caption.setTextFill(Color.DARKORANGE);
    caption.setStyle("-fx-font: 24 arial;");

    for (final PieChart.Data data : chart.getData()) {
        data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent e) {
                caption.setTranslateX(e.getSceneX());
                caption.setTranslateY(e.getSceneY());
                caption.setText(String.valueOf(data.getPieValue()) + "%");
            }
        });
    }

    ((Group) scene.getRoot()).getChildren().addAll(chart, caption);
    stage.setScene(scene);
    //scene.getStylesheets().add("piechartsample/Chart.css");
    stage.show();
}

From source file:dpfmanager.shell.interfaces.gui.component.report.ReportsView.java

public void addChartScore() {
    colScore.setCellFactory(new Callback<TableColumn<ReportRow, String>, TableCell<ReportRow, String>>() {
        @Override/*w w  w .j a  v a2s .  co  m*/
        public TableCell<ReportRow, String> call(TableColumn<ReportRow, String> param) {
            TableCell<ReportRow, String> cell = new TableCell<ReportRow, String>() {
                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!empty && item != null) {

                        Double score = item.indexOf("%") < 0 || item.indexOf("?") >= 0 ? 0
                                : Double.parseDouble(item.substring(0, item.indexOf('%')));

                        ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
                                new PieChart.Data("Correct", score), new PieChart.Data("Error", 100 - score));

                        PieChart chart = new PieChart(pieChartData);
                        chart.setId("pie_chart");
                        chart.setMinSize(22, 22);
                        chart.setMaxSize(22, 22);

                        HBox box = new HBox();
                        box.setSpacing(8);
                        box.setAlignment(Pos.CENTER_LEFT);

                        Label score_label = new Label(item);
                        score_label.setTextFill(Color.LIGHTGRAY);

                        box.getChildren().add(chart);
                        box.getChildren().add(score_label);

                        setGraphic(box);
                    } else {
                        setGraphic(null);
                    }
                }
            };
            return cell;
        }
    });
}

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

/**
 * Generates a pie chart.// ww w.ja va 2s .c  o  m
 *
 * @param question A question JSONObject of a report
 * @return
 */
private PieChart generatePieChart(JSONObject question) {
    ObservableList<PieChart.Data> data;
    PieChart 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 = generateDataPieChart(result, answerType);

        chart = new PieChart(data);
        chart.setTitle(surveyQuestion.getString("text"));
        chart.setLegendVisible(false);
    } catch (JSONException e) {
        log.error(e);
        return null;
    }

    return chart;
}