Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created May 6, 2021 08:34
Show Gist options
  • Save Da9el00/9c1e63a5ba8c84e0219bb7e59d7a5bad to your computer and use it in GitHub Desktop.
Save Da9el00/9c1e63a5ba8c84e0219bb7e59d7a5bad to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder PieChart controller
package sample;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.PieChart;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private PieChart pieChart;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Apples", 2),
new PieChart.Data("Oranges", 25),
new PieChart.Data("Grapes", 50),
new PieChart.Data("Melons", 3));
pieChartData.forEach(data ->
data.nameProperty().bind(
Bindings.concat(
data.getName(), " amount: ", data.pieValueProperty()
)
)
);
pieChart.getData().addAll(pieChartData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment