Thursday, June 17, 2010

Creating Bar Charts with JFreeChart

Create a new Plugin project "de.vogella.rcp.jfreechart.barchart" using "RCP application with a view" as a template. Add "de.vogella.rcp.jfreechart.libs" as a dependency to project "de.vogella.jfreechart.barchart" and change the coding of the view to the following:

   
package de.vogella.rcp.jfreechart.barchart;

import java.awt.Color;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public class View extends ViewPart {

private CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

// row keys...
String series1 = "First";
String series2 = "Second";

// column keys...
String category1 = "Label 1";
String category2 = "Label 2";
String category3 = "Label 3";

dataset.addValue(1.0, series1, category1);
dataset.addValue(4.0, series1, category2);
dataset.addValue(3.0, series1, category3);

dataset.addValue(5.0, series2, category1);
dataset.addValue(7.0, series2, category2);
dataset.addValue(6.0, series2, category3);

return dataset;
}

/**
* Creates a chart.
*
* @param dataset
* dataset.
*
* @return A chart.
*/
private JFreeChart createChart(CategoryDataset dataset) {

JFreeChart chart = ChartFactory.createBarChart("Bar Chart", // chart
// title
"Labels", // domain axis label
"Values", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);

CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);
return chart;

}

public void createPartControl(Composite parent) {
JFreeChart chart = createChart(createDataset());
final ChartComposite frame = new ChartComposite(parent, SWT.NONE,
chart, true);
}

public void setFocus() {
}
}

Run it and see that it is working.

2 comments:

  1. Hi can i know how to pass the values to the bar chart from a database. I am new to android.

    ReplyDelete
  2. Hey
    Good and simple example.
    Do u have some code to make it work with android.
    as AWT & swing re not included in android

    ReplyDelete

How to Block Spam Calls and Messages on Android

Spam calls and messages have gotten bad enough that most people now hesitate before answering any unknown number. The good news is Android h...