Loop through observable list


Question

I have a javafx table (carTable) that contains a single column:

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

This column is part of an observable list (carOL). This OL also contains 2 Lists ("Dates" and "Prices"). For each brand prices can be downloaded from the internet for a specific time window.

eg:

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

I now want to create a line chart with all this data, that compares price development between the different brands.

What I have come up so far:

@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);

This of course does not quite return what I want. I somehow need to be able to create separate "series" for each brand.

Can anyone help?

1
2
6/12/2014 7:38:33 PM

If order of items in array of dates corresponds to order of items in array of prices, you need this:

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++; 
  } 
} 

Go through all dates and add them with appropriate prices.

1
6/13/2014 5:08:28 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon