How can I make a dynamic line chart with JavaFX using a socket input

前端 未结 1 648
遥遥无期
遥遥无期 2021-01-24 05:12

I have written some code that reads a socket input and outputs the data that I want in an ArrayList of Double values. I would like to keep updating this ArrayList with current v

相关标签:
1条回答
  • 2021-01-24 06:00

    The data in a JavaFX chart is a ObservableList of ObservableLists - one ObservableList for each chart line series. The chart listens to changes on the observable lists of data and, when you modify any of the data in any of these ObservableList (due to the observable nature of them), the chart will automatically update itself. So you don't really need to do much.

    Before changing the chart data, set the animated property of your chart to an appropriate value; i.e., turn off animation if you want the new values in the chart to display directly, or turn on animation if you want the data points in the chart to slowly move to the new values.

    You state that you are receiving the data over a socket. Usually with traditional socket IO, you have a thread monitoring the socket for new data - this should not be the JavaFX application thread otherwise your application UI will hang while the awaiting new data. When the socket monitoring thread receives new data, it should not modify the the ObservableList for the chart data directly. Instead, it should wrap any modifications to the chart's observable list in a Platform.runLater() call so that the modifications to the chart data occur on the JavaFX application thread.

    0 讨论(0)
提交回复
热议问题