Feb 18

There is a great tutorial on vogella.de describing how to add a pie chart with JFreeChart to an Eclipse RCP application or plug-in. The ChartFactory that is used to create the pie chart does not include a method to create a speedometer (or dial) as shown in the sample section of JFreeChart. The following code fragment creates a view that displays a very basic speedometer using the org.jfree.chart.plot.MeterPlot class:

package com.martinklinke.eclipse.jfreechart.demo.views;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.MeterPlot;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.experimental.chart.swt.ChartComposite;
 
/**
* @author martin
*
*/
public class MeterChartView extends ViewPart {
   public static final String ID = "com.martinklinke.eclipse.jfreechart.demo.views.MeterChartView";
 
   public void createPartControl(Composite parent) {
      JFreeChart chart = createChart();
      final ChartComposite frame = new ChartComposite(parent, SWT.NONE, chart, true);
   }
 
   public void setFocus() {
   }
 
   /**
    * Creates the Chart based on a dataset
    */
   private JFreeChart createChart() {
      DefaultValueDataset data = new DefaultValueDataset(20.0);
      MeterPlot plot = new MeterPlot(data);
      JFreeChart chart = new JFreeChart("Meter Chart",
      JFreeChart.DEFAULT_TITLE_FONT, plot, false);
      plot.setNoDataMessage("No data available");
      return chart;
   }
}

The result should look like the following screenshot:

JFreeChart Speedometer in Eclipse View

JFreeChart Speedometer in Eclipse View

Of course, the MeterPlot can be further customized by calling the plot.setXXX(…) methods. However, this exercise is left to the willing reader ;)

blog