正确创建JavaFX Chart的新实例

时间:2015-04-06 21:48:55

标签: javafx javafx-8

我无法找到问题的解决方案如何创建新的图表实例。我测试了以下代码:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
import javafx.util.Duration;

public class MainApp extends Application
{
    private StackedBarChart<String, Number> schart;
    private CategoryAxis xAxis = new CategoryAxis();
    private NumberAxis yAxis = new NumberAxis();

    private StackedBarChart<String, Number> inits()
    {
        schart = new StackedBarChart<>(xAxis, yAxis);
        schart.setAnimated(false);

        schart.setTitle("Some_value");

        return schart;
    }

    GenDatService data = new GenDatService();

    @Override
    public void start(Stage stage) throws Exception
    {

        StackedBarChart<String, Number> inits = inits();



        data.setPeriod(new Duration(1000));
        data.setOnSucceeded(new EventHandler<WorkerStateEvent>()
        {
            @Override
            public void handle(final WorkerStateEvent workerStateEvent)
            {

                HashMap<String, Integer> value = data.getValue();

                visuData(value);

            }
        });
        data.setOnFailed(new EventHandler<WorkerStateEvent>()
        {
            @Override
            public void handle(WorkerStateEvent t)
            {
            }
        });
        data.start();

        Scene scene = new Scene(schart);

        stage.setTitle("JavaFX and Maven");
        stage.setScene(scene);
        stage.show();
    }

    public void visuData(HashMap<String, Integer> map)
    {
        Set<String> keySet = map.keySet();

        ObservableList<XYChart.Series<String, Number>> observableArrayList = FXCollections.observableArrayList();

        ObservableList<String> observabtd = FXCollections.observableArrayList();

        Iterator<String> iterator = keySet.iterator();

        while (iterator.hasNext())
        {
            String column = iterator.next();
            Integer value = map.get(column);

            XYChart.Series<String, Number> series1 = new XYChart.Series<>();
            XYChart.Data<String, Number> dataS1 = new XYChart.Data<>();

            observabtd.add(column);

            series1.setName(column);
            dataS1.setXValue(column);
            dataS1.setYValue(value);
            series1.getData().add(dataS1);

            observableArrayList.add(series1);
        }

        xAxis.setCategories(observabtd);
        yAxis.setLabel("Some Value");

        Scene scene = schart.getScene();
        schart = new StackedBarChart<>(xAxis, yAxis);
        scene.setRoot(schart);
        schart.setAnimated(false);

        schart.setData(observableArrayList);
    }

    public static void main(String[] args)
    {
        launch(args);
    }

    class GenDatService extends ScheduledService<HashMap<String, Integer>>
    {
        @Override
        protected Task<HashMap<String, Integer>> createTask()
        {
            return new Task<HashMap<String, Integer>>()
            {
                @Override
                protected HashMap<String, Integer> call() throws Exception
                {
                    HashMap<String, Integer> usageData = new HashMap<>();
                    usageData.put("val_1", (int) (Math.random() * 100));

                    usageData.put("val_2", (int) (Math.random() * 100));

                    usageData.put("val_3", (int) (Math.random() * 100));

                    usageData.put("val_4", (int) (Math.random() * 100));

                    return usageData;
                }
            };
        }
    }
}

此代码工作正常,但唯一的问题是我无法想出正确设置StackedBarChart的新实例的方法。这是我需要解决的代码。

  Scene scene = schart.getScene();
    schart = new StackedBarChart<>(xAxis, yAxis);
    scene.setRoot(schart);

你能帮我解决这个问题吗?

0 个答案:

没有答案