[英]JavaFx create a chart from dataframe
我想在JavaFX中創建一個圖表。 我有這樣一個文件:
而且我不知道如何在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);
我創建了一個示例,演示了您的圖表。 由於您將數據附加為圖像,因此我不得不創建虛假數據。 在此示例中,我創建了一個DataFrame
類來保存每一行數據。 我將此數據添加到名為fakeData
的List
。 然后,我創建並命名要添加到Chart
每個Series
。 之后,我通過ID
過濾虛假數據。 如果過濾的ID
等於Series
名稱,則將數據添加到Series
。 此圖表顯示“ Total
Date
Total
。 如果您需要Date
to Val
,請替換seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
與seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getVal()));
。
主要
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class LineChartSample extends Application
{
String alphbets = "abcdefghij";
@Override
public void start(Stage stage)
{
List<DataFrame> fakeData = generateFakeDataFrames();
stage.setTitle("Line Chart Sample");
//defining the axes
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Date");
//creating the chart
final LineChart<String, Number> lineChart = new LineChart(xAxis, yAxis);
lineChart.setTitle("Fake Chart");
//defining a series
List<XYChart.Series<String, Number>> seriesList = new ArrayList();
for (int i = 0; i < alphbets.length(); i++) {
XYChart.Series<String, Number> series = new XYChart.Series();
series.setName(alphbets.charAt(i) + "");
seriesList.add(series);
}
for (int i = 0; i < seriesList.size(); i++) {
char tempChar = alphbets.charAt(i);
//Filter the fake data
List<DataFrame> subList = fakeData.stream().filter((t) -> {
return t.getId() == tempChar;
}).collect(Collectors.toList());
//Add the filtered data to the correct series
for (DataFrame dataFrame : subList) {
System.out.println(seriesList.get(i).getName() + " :" + dataFrame.getId() + " " + dataFrame.getDate() + " " + dataFrame.getTotal());//Check to see if data is correct.
seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
}
}
Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().addAll(seriesList);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
public List<LocalDate> createDates()
{
List<LocalDate> localDates = new ArrayList();
LocalDate localDate = LocalDate.of(1985, Month.FEBRUARY, 4);
for (int i = 0; i <= 4; i++) {
localDates.add(localDate.plusDays(i));
}
return localDates;
}
public List<DataFrame> generateFakeDataFrames()
{
Random random = new Random();
List<DataFrame> dataFrames = new ArrayList();
List<LocalDate> dates = createDates();
for (int i = 0; i < 50; i++) {
double total = (random.nextDouble() * 2 - 1) * 3;
double val = (random.nextDouble() * 2 - 1) * 2000;
dataFrames.add(new DataFrame(alphbets.charAt(i % 10), dates.get(i / 10).toString(), total, val));
}
return dataFrames;
}
}
DataFrame類別
/**
*
* @author blj0011
*/
public class DataFrame
{
private char id;
private String date;
private double total;
private double val;
public DataFrame(char id, String date, double total, double val)
{
this.id = id;
this.date = date;
this.total = total;
this.val = val;
}
public double getVal()
{
return val;
}
public void setVal(double val)
{
this.val = val;
}
public char getId()
{
return id;
}
public void setId(char id)
{
this.id = id;
}
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date = date;
}
public double getTotal()
{
return total;
}
public void setTotal(double total)
{
this.total = total;
}
@Override
public String toString()
{
return id + ", " + date + ", " + total + ", " + date + ", " + val;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.