XChart

XChart

When creating graphical user interfaces with Java, it is often useful to add charts to your Java application for visualizing data. In this tutorial, we show how to easily add charts to a Java Swing application using our open source Java charting library XChart. The two examples shown here are basic demonstrations to illustrate the core concepts and code mechanics. For building your own application with charts, including real-time line, scatter, bar, pie, histogram, stick and bubble charts, these examples can be a useful starting point. At the end of the article, there are links to further examples including different chart types and integrating a chart panel into your existing Java Swing application. XChart is an open source Java charting library with an Apache 2.0 open source license, and the source code can be found on github. XChart can be downloaded as jar from here, or the XChart jar can be integrated into your Java application via Maven.

Simple Java Real-time Java Chart Example

Creating real-time charts is as simple as calling updateXYSeries for one or more series objects through the XYChart instance and triggering a redraw of the JPanel containing the chart. This works for all chart types including XYChart, CategoryChart, BubbleChart and PieChart, for which example source code can be found here. Examples demonstrate using the SwingWrapper with repaintChart() method as shown here, as well as XChartPanel with revalidate() and repaint(), which you would want to use if you already had your own Java Swing application which integrates an XChartPanel.

The following sample code used to generate the following real-time chart can be found here.

XChart Simple Realtime Java Chart

XChart Simple Realtime Java Chart

Swing Worker Java Real-time Java Chart Example

In the above example, the chart data generation runs on the EventDispatchThread, which is definitely not ideal for a responsive GUI. Additionally, the EventDispatchThread is forced to sleep for 100 ms between each update to the real-time chart. While far from the correct way to build a Java Swing application, it serves the purpose of demonstrating how the XChart components function.

In this example, lets use an additional thread to do the data generation, rather than the EventDispatchThread. This good-practice technique allows for your GUI to remain responsive to user interactions while the charting goes on in the background–except for when it comes time to actually repaint the chart. The SwingWorker thread is a special thread that handles the complicated details for you when updating the GUI running on the EventDispatchThread.

This example simulates a common scenario in making real-time charting applications: There is some background thread reading data from a sensor, and you want a live chart to update as new data becomes available. Sometimes however, you don’t know how often the data is being updated. One problem could be that the data is coming in way faster than you’d ever want to repaint a chart. For example, it doesn’t make any sense to update the chart on every single new datapoint if the data rate is faster than the screen refresh rate, for example 24 points per second. So in this example we simulate a data rate of 5 ms per data point and a chart update rate of 24 frames per second, dynamically calculated. The chart animation looks smooth, and no extra CPU cycles are being wasted updating the chart for no reason.

The following sample code used to generate the following real-time chart can be found here.

XChart SwingWorker Realtime Java Chart

XChart SwingWorker Realtime Java Chart

Further Reading

  1. XYChart, CategoryChart, BubbleChart and PieChart Real-time charts Example Code
  2. XChart on Knowm.org
  3. XChart on Github

Knowm Sundial Job Scheduling

Knowm Yank JDBC Persistance

Related Posts

Subscribe To Our Newsletter

Join our low volume mailing list to receive the latest news and updates from our team.

6 Comments

Leave a Comment

Knowm 32X32 Crossbar

Knowm Newsletter

Are you ready for memristor AI processors? With our newsletter, you will be.