2

我有一个包含单列的 javafx 表(carTable):

+-------------+
+ Brand        +
+-------------+

此列是可观察列表 (carOL) 的一部分。此 OL 还包含 2 个列表(“日期”和“价格”)。可以从互联网上下载每个品牌的特定时间窗口的价格。

例如:

Audi
   2014-01-01
   2014-01-02
   55000
   90000
BMW
   2014-01-01
   2014-01-02
   85000
   70000

我现在想用所有这些数据创建一个折线图,比较不同品牌之间的价格变化。

到目前为止我想出了什么:

@FXML private CategoryAxis xAxis = new CategoryAxis();
@FXML private NumberAxis yAxis = new NumberAxis();
@FXML private LineChart<String, Number> lc = new LineChart<>(xAxis,yAxis);

Series<String, Number> series = new Series<>();
List<String> d = new ArrayList<String>();
List<Double> p = new ArrayList<Double>();
int id = 0;

for (Car c : carOL) {
    d = c.getDate(); // returns array list with dates from OL
    p = c.getPrice(); // returns array list with prices from OL
    series.getData().add(new XYChart.Data<>(d.get(id), p.get(id)));
    id++;
    }

lc.getData().add(series);

这当然不能完全返回我想要的。我不知何故需要能够为每个品牌创建单独的“系列”。

任何人都可以帮忙吗?

4

1 回答 1

1

如果日期数组中的项目顺序对应于价格数组中的项目顺序,则需要:

for (Car c : carOL) { 
  d = c.getDate(); // returns array list with dates from OL 
  p = c.getPrice(); // returns array list with prices from OL 
  for (String dates : d.getDate()) { 
    series.getData().add(new XYChart.Data<>(dates, p.get(id))); 
    id++; 
  } 
} 

浏览所有日期并以适当的价格添加它们。

于 2014-06-12T19:52:13.303 回答