JavaFX LineChart - draw an array

I am trying to display data for a user with LineChart in JavaFX. I have an array of Float (and not a primitive object, as in Float[] ), which is ready to be added, which can be anywhere from 512 to 4096 points.

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

Is there a faster way when I can just toss an array in the JavaFX LineChart class and draw it without scrolling and adding each point?

EDITOR (solution found) :

srm, this concept works!

In the first run, just populate XYChart.Series new XYChart.Data(X,Y) . Then loop and retrieve and update using XYChart.Series.get(index).setYData(NewValue)

+4
java javafx
source share
3 answers

Well, I had to delete the previous record due to the inability to read.

Have you tried using XYChartBuilder? It looks like you can use an initial list of data that can be changed at runtime data (ObservableList> x) I did not work on the fact that my first assumption is that you create only one list of this and then change only that data that you need (although I may be completely wrong here). Try and see, I really want to hear it!

+2
source share

This LineChart constructor takes a series of charts as one of its arguments.

First you can build a series, and then build a LineChart .

Convert Float[] to List First

 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 create 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 several series or add another series after creating the chart using the XYChart.getData() method and adding your data there.

+1
source share

Here is another way to quickly load series data 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); 
+1
source share

All Articles