Lightweight, constantly updating Java charts
I want to have charts in my Swing application that show a wide variety of data--but the data for each chart will be constantly changing.
A good analogy to how my data will be behaving is how the the system's free and used memory will (likely) go up and down throughout the course of the application running. A good analogy of how I want the graph to look is the CPU usage graph in the Windows task manager (don't mean the specific colors and grid, but more the way the line graph look开发者_如何转开发s).
What is the best way to generate charts where I will constantly be "appending" new data points. Is the best way to append these points at regular time intervals?
Also, what charting/graphing API should I use? I would like to keep dependencies as small as possible, but I accept that they are probably necessary.
tl;dnr: I want to make a chart like the CPU Usage History in Task Manager, but in Java, for Swing.
JFreeChart is a common choice for generating charts in Java.
These days, computers are so powerful that you'd be hard-pressed to find any reasonable charting solution that's "too slow" for this application.
See also: Using JFreeChart to display recent changes in a time series (props to @trashgod for the link!).
I would think you could do this yourself without a custom charting application.
You should be able to store your graph points in an ArrayList. Then you can use the Graphics.drawLine(...) method to connect each point.
Then you can use a Swing Timer to schedule the update to your graph. Every time the Timer fires you can remove a number of points from the beginning and add a number of points to the end. Then you invoke repaint() on your custom component.
Take a look at JChart2D. From the web page:
JChart2D is an minimalistic realtime charting library published under the OSI approved GNU LESSER GENERAL PUBLIC LICENSE. It is designed for displaying multiple traces consisting of tracepoints. JChart2D is centered around a single configureable swing widget: the Chart2D.
JChart2D is intended for engineering tasks and not for presentations. It's specialty is run time - dynamic precise display of data with a minimal configuration overhead.
精彩评论