JavaFX LineChart - draw array


Question

I'm trying to display data to a user with a LineChart in JavaFX. I have an array of Float (not the primitive, the object, as in Float[]) that is ready to be added that could be anywhere from 512 to 4096 points long.

All the examples and help for LineChart show that data has to be added point by point using XYChart.Series.getData().add(new XYChart.Data(X, Y)) where X would be the index and Y would be the value at Float[index]. This is really, really slow since this approach requires looping through the array, but it works. I'd like the LineChart to update at 30FPS but it as less than 1FPS right now :/

Is there a faster way where I can just toss an array at the JavaFX LineChart class and have it draw without looping through and adding each point?

EDIT (Solution Found):

srm, that concept works!

On the first run, just fill the XYChart.Series with new XYChart.Data(X,Y). Then loop through and fetch and update using XYChart.Series.get(index).setYData(NewValue)

1
4
4/30/2014 1:27:58 AM

Accepted Answer

Ok, had to delete my previous post for not being able to read.

Have you tried using the XYChartBuilder? It looks like you can use an initial list of datapoints that might be changed during runtime data(ObservableList> x) I haven't worked with that that my first inutition is, that you create only one list of this and then change only the datapoints you need (while I may be completely wrong here). Try it and see, I'm keen to hear back on this!

2
4/29/2014 10:00:51 PM

Here is another way to load up series data very fast at startup:

ObservableList<XYChart.Data<Number, Number>> data = FXCollections.<XYChart.Data<Number, Number>>observableArrayList();
for (int i = 0; i < 10000; i++)
    data.add(new XYChart.Data<>(Math.random(), Math.random()));
XYChart.Series series = new XYChart.Series(data);
chart.getData().add(series);

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