JavaFX,PieChart - 如何添加数据

时间:2015-12-03 12:16:13

标签: java javafx pie-chart scenebuilder

我正在尝试将数据添加到我的PieChart。我正在编写像tutorial这样的代码,但是当我尝试在它不工作之前添加变量时。怎么了?

import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Side;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;

public class HomeScreenController implements Initializable{


    //Counter dreams "straszny"
    public String Straszny = "Straszny";
    public double Straszny_Counter = 0;

    //Counter dreams "smieszny"
    public String Smieszny = "Śmieszny";
    public double Smieszny_Counter = 0;

    //Counter dreams "dziwny"
    public String Dziwny = "Dziwny";
    public double Dziwny_Counter = 0;

    //Counter dreams "SF"
    public String SF = "SF";
    public double SF_Counter = 0;

    //Counter dreams "straszny"
    public String Inny = "Inny";
    public double Inny_Counter = 0;

    @FXML
    private PieChart sleepTypes;

    ObservableList<PieChart.Data> pieChartData =
            FXCollections.observableArrayList(
            new PieChart.Data(Smieszny, Smieszny_Counter),
            new PieChart.Data(Straszny, Straszny_Counter),
            new PieChart.Data(Dziwny, Dziwny_Counter),
            new PieChart.Data(SF, SF_Counter),
            new PieChart.Data(Inny, Inny_Counter));

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        for(int i = 0;i<ShowDreamHistoryController.listDreams.size();i++)
        {
            //Check how much "strasznych" dreams is
            if(Straszny.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
            {
                Straszny_Counter++;
            }
            //Check how much "smiesznych" dreams is
            else if(Smieszny.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
            {
                Smieszny_Counter++;
            }
            //Check how much "dziwny" dreams is
            else if(Dziwny.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
            {
                Dziwny_Counter++;
            }
            //Check how much "SF" dreams is
            else if(SF.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
            {
                SF_Counter++;
            }
            //Check how much "inny" dreams is
            else if(Inny.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
            {
                Inny_Counter++;
            }
        }//for

        System.out.println("Straszych: "+Straszny_Counter);
        System.out.println("Smiesznych: "+Smieszny_Counter);
        System.out.println("Dziwny: "+Dziwny_Counter);
        System.out.println("SF: "+SF_Counter);
        System.out.println("Inny: "+Inny_Counter);

        //Add Data to the chart
        sleepTypes.setData(pieChartData);
    }//init
}//class

Here, what it looks like after compilation

代码就像这样:

    ObservableList<PieChart.Data> pieChartData =
            FXCollections.observableArrayList(
            new PieChart.Data(Smieszny, 1),
            new PieChart.Data(Straszny, 2),
            new PieChart.Data(Dziwny, 5),
            new PieChart.Data(SF, 1),
            new PieChart.Data(Inny, 1));

但我不想要那个。任何想法第一个代码有什么问题?

0 个答案:

没有答案