JavaFx从数据框创建图表

时间:2018-11-26 17:17:51

标签: java file javafx charts axis

我想在JavaFX中创建一个图表。我有这样的文件:

enter image description here

我不知道如何在x或y轴上放置“日期”。我应该怎么做。 我有这样的东西,但不确定是否正确。

draw.setOnAction(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent event) {
        newWindow.setTitle("Chart");
        //defining the axes
        if(choice1.getValue().toString()=="val" && choice2.getValue().toString()=="total"){}

        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");

        //creating the chart
        final LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis);
        lineChart.setTitle("Chart");
        //defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        //populating the series with data
        for(int i=0; i<d.size(); i++){
            series.getData().add(new XYChart.Data(d.get(choice1.getValue().toString()).get(i), d.get(choice2.getValue().toString()).get(i)));
        }

        Scene scene  = new Scene(lineChart,800,600);
        lineChart.getData().add(series);
        newWindow.setScene(scene);
        newWindow.show();
     }
});

 LineChart linechart = new LineChart(xAxis, yAxis);
    ChoiceBox choice1 = new ChoiceBox(FXCollections.observableArrayList(
            "id", "date", "total", "val")
    );
    ChoiceBox choice2 = new ChoiceBox(FXCollections.observableArrayList(
            "id", "date", "total", "val")
    );
    Button draw = new Button("Draw!");
    HBox hb = new HBox(30);
    hb.getChildren().addAll(choice1, choice2);

1 个答案:

答案 0 :(得分:0)

我创建一个示例来演示您的图表。由于您将数据附加为图像,因此我不得不创建虚假数据。在此示例中,我创建了一个DataFrame类来容纳每一行数据。我将此数据添加到名为List的{​​{1}}中。然后,我创建并命名每个要添加到fakeData的{​​{1}}。之后,我用Series过滤了假数据。如果过滤后的Chart等于ID名称,则将数据添加到ID中。此图表显示了SeriesSeries。如果您需要DateTotal,则将Date替换为Val

  

主要

seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
  

DataFrame类

seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getVal()));

enter image description here