1

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 looks).

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alec Gorge
  • 17,110
  • 10
  • 59
  • 71

3 Answers3

5

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!).

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Is there anything else out there besides JFreeChart.. All java related charting q always have a single answer. – mP. Mar 02 '11 at 03:37
  • 1
    @mP: [there sure is](http://www.google.com/search?q=java+charts), but I've never used any of them. – Matt Ball Mar 02 '11 at 03:41
  • 2
    *"All java related charting q always have a single answer."* - perhaps because there is only one **good** answer ... unless you are willing to fork out money for the privilege of locking yourself into a vendor. – Stephen C Mar 02 '11 at 04:22
  • 1
    Here's a related [example](http://stackoverflow.com/questions/5048852) using JFreeChart. – trashgod Mar 02 '11 at 04:25
  • Thanks for this. I am going to play around with both and see which one I use. These are both great answers though. – Alec Gorge Mar 02 '11 at 12:38
3

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • this may be the way to go, let me fiddle with it. – Alec Gorge Mar 02 '11 at 12:38
  • This [kinetic model](http://sites.google.com/site/drjohnbmatthews/kineticmodel) has a [`Histogram`](http://sites.google.com/site/drjohnbmatthews/kineticmodel/code#Histogram) class that might be worth a look. – trashgod Mar 02 '11 at 19:31
  • This was actually much easier than I expected. Go for this! – Alec Gorge Mar 05 '11 at 02:43
2

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.
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71