4

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)

Austin
  • 1,018
  • 9
  • 20

3 Answers3

2

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!

srm
  • 128
  • 9
  • 2
    The `XYChartBuilder` (like most of the builders) is deprecated, and probably should not be used. You can easily create an array or `List` of `XYChart.Data` values and set them en masse anyway. – James_D Apr 30 '14 at 04:07
  • Thanks for the clarification. I wasn't aware of that (didn't thoroughly read the API). – srm May 02 '14 at 22:08
2

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);
Saeid Nourian
  • 1,606
  • 15
  • 32
1

This LineChart constructor takes the serieses of the chart as one of its arguments.

You can construct the series first, then construct the LineChart.

First convert your Float[] to a List

List<XYChart.Data<Int,Float>> seriesData = new ArrayList<>();
for(int i=0;i<data.length;++i)
    seriesData.add(new XYChart.Data(i,data[i]));

Then construct your LineChart

XYChart.Series<Int,Float> series = new XYChart.Series<>();
series.getData().addAll(seriesData);
LineChart<Int,Float> chart = new LineChart<>(...axes...,FXCollections.observableArrayList(series));

As brian suggests, you can also add more series, or add another series, after the chart has been created by using the XYChart.getData() method and adding your data there.

ggovan
  • 1,907
  • 18
  • 21
  • 1
    You could construct the series then add them to an existing line chart. It's useful if you want to add another series. – brian Apr 29 '14 at 22:06