user1673554
user1673554

Reputation: 461

Javafx PieChart created with scene builder does not dispaly data

Im having trouble using some of the Javafx examples from Oracle as the 'scenes' are not created with the Scene Builder so the code differs from what I want to use it for.

Im trying to create a layout with scenebuilder that holds a Pie Chart. I have set the fxid of the chart to 'myPieChart' with the corresponding @FXML private Chart myPieChart; at the start of my code.

I have also added the following code that gets executed on initialisation that I thought would have crated the chart:-

    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));


    PieChart myPieChart = new PieChart(pieChartData);

All of the values are being put into an observable list and then the Pie Chart is instantiated with the ObsevableList value.

Unfortunately my pie chart is not showing....what am I missing guys?

Cheers for any help.

Upvotes: 4

Views: 6102

Answers (2)

Nidhi Agrawal
Nidhi Agrawal

Reputation: 21

You can do like this by just setting the id in fxml for your chart

public class GraphScreenController implements Initializable {

    @FXML
    PieChart chart;

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
 ObservableList<PieChart.Data> pieChartData =
            FXCollections.observableArrayList(
            new PieChart.Data("Executed", 60),
            new PieChart.Data("Passed", 25),
            new PieChart.Data("Fails", 15));

 chart.setData(pieChartData);

}
}

Upvotes: 2

jewelsea
jewelsea

Reputation: 159290

Don't create a new PieChart.

The FXML loading process will create a chart instance for you and insert it as a child of the FXML defined layout Pane. The @FXML annotation will inject a reference to the chart into your Controller.

All you need to do in your Controller's initializer is populate the existing chart with your data. You can do this by calling setData on myPieChart.

Upvotes: 3

Related Questions