JavaFX - LineChart
JavaFX 1.2 introduces new Chart APIs. Here we discuss about how to plot line chart using LineChart API.
For creating a chart, we need to create following instance
- Type of Chart - Area, Bar, Bubble, Line, Pie, 3D-Pie, Scatter
- Chart Series - Area, Bar, Bubble, Line, Scatter
- Chart Data - Area, Bar, Bubble, Line, Pie, Scatter
Note: Pie chart is an exception. In this case we don't need to create instance of Series. Series is required only for X-Y chart types.
Lets implement a Line-Chart which updates its data dynamically. In this case a CPU monitor is simulated by generating random data from java class. The data is generated in a java Thread. The data is sent to JavaFX using a listener interface so as to update the chart. The notification is done via Event Dispatch Thread.
Update: Sample using live CPU and Memory information is available here.
For Applet mode, click on above image
Create an instance of Series to plot chart for CPU usage of user, system and idle:
var cpuLineChartSeries : LineChart.Series[] = [
|
Create an instance of Chart and associate the Series with this chart:
var cpuLineChart = LineChart {
|
A simple UpdateListener interface is created. Which will be used to update data from java to javafx. Dummy CPU data is generated by CPUData class. The update is performed via Event Dispatch Thread using SwingUtilities.invokeLater( <Runnable> ).
/**
|
This update notification is handled in the JavaFX class and we can insert the data in chart as shown below.
public override function updateCPU(user : Integer, sys : Integer, idle : Integer) {
|
When the index reaches the upper-bound, we can adjust the lowerBound and upperBound attributes of X-Axis, so as to shift the chart towards left. Also we may remove the initial chart data which are not visible.
Update:
As noted in comments section below, there is some Memory-Leak when we try to continuously update the bounds. Updation of bounds triggers updation of ticks and Skin related code of Label. For now instead of NumberAxis I have used a custom NoTickNumberAxis which supress the tick updates.
class NoTickNumberAxis extends NumberAxis {
|
Try it out and let me know feedback!
Source:




cool!!!
Posted by Gesiel Mota on June 23, 2009 at 06:34 PM IST #
Great post!
What would happen if you did not use the Event Dispatching Thread but call updateListener.update() directly?
Posted by Nick on June 23, 2009 at 07:37 PM IST #
I wonder if JavaFX has serious memory leak. In less than 3 mins, the application run up to 100mb and then gradually increase in memory in Windows Vista.
I notice that at 102,532kb, every time one minimise and then restore it, there is a increase of 4 to 8 kb memory consumption.
What has gone wrong ?
Posted by GeekyCoder on June 23, 2009 at 08:41 PM IST #
@Gesiel Mota Thanks!
Posted by Rakesh Menon on June 23, 2009 at 09:47 PM IST #
@Nick Its to avoid thread synchronization issues. JavaFX code executes on EDT. The java code is executing on another thread, so we update the data from EDT so that it won't cause any synchronization issues.
Posted by Rakesh Menon on June 23, 2009 at 09:49 PM IST #
@GeekyCoder Thanks a lot.. I did some basic investigation.. its due to rendering of ticks which triggers a lot of code related to Skin when bounds of x-axis is updated. I suppressed tick-update code and it looks stable. I'll do some further investigation.
Posted by Rakesh Menon on June 23, 2009 at 09:53 PM IST #
Too cool :)
Posted by Vaibhav Choudhary on June 24, 2009 at 09:36 AM IST #
Nice work!
One little bug is in CPUMonitor.fx: it should be
cpuIndex += 1;
if(cpuIndex >= cpuLineChart.xAxis.upperBound) {
...
}
or you'll get those "jumps" after the first time, upperBound is reached.
Posted by M. Rohr on August 14, 2009 at 12:43 AM IST #
@M. Rohr Thanks! I have incorporated the change.
Posted by Rakesh Menon on August 18, 2009 at 10:28 AM IST #
ThankYou Menon!...
U done a greatJob!man......
please send me line3d,area3d,column2d,column3d charts urgently!....
Posted by kalyan on November 18, 2009 at 05:18 PM IST #
@kalyan Thanks! Are you looking for any specific example? How-To docs will be good start..
http://javafx.com/docs/howto/Charts-Tutorial.jsp
http://javafx.com/learn/howto.jsp
Let me know if you need any further help..
Posted by Rakesh Menon on November 18, 2009 at 05:28 PM IST #