SWTChart in Eclipse RCP

SWTChart provides nice charts which can be easily used in Eclipse RCP applications.

The library is not yet as complete as JFreeChart but have the advantage that they are directly implemented in SWT.

To use swtchart in your Eclipse RCP application, download the zip file from http://www.swtchart.org. The zip file contains two OSGi bundles (org.swtchart and org.swtchart.ext). Import “org.swtchart” into your workspace.

Create a new Plug-in Project (RCP), e.g. de.vogella.swtchart, based on the Eclipse application with a view template. Add org.swtchart and org.swtchart.ext as a dependency to your plug-in.

Now change the code in View.java to the following:

package de.vogella.swtchart;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.swtchart.Chart;
import org.swtchart.IAxis;
import org.swtchart.IAxisSet;
import org.swtchart.IBarSeries;
import org.swtchart.ISeries.SeriesType;

public class View extends ViewPart {
public static final String ID = "de.vogella.swtchart.view";

public void createPartControl(Composite parent) {
Chart chart = new Chart(parent, SWT.NONE);
chart.getTitle().setText("SWT Chart");
chart.getAxisSet().getXAxis(0).getTitle().setText("Operating Systems");
chart.getAxisSet().getYAxis(0).getTitle().setText("Love");
IAxisSet axisSet = chart.getAxisSet();
IAxis xAxis = axisSet.getXAxis(0);
xAxis.setCategorySeries(new String[] { "Linux", "Windows" });
xAxis.enableCategory(true);

IBarSeries series = (IBarSeries) chart.getSeriesSet().createSeries(
SeriesType.BAR, "line series");
series.setBarColor(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
double[] values = { 0.7, 0.2 };
series.setYSeries(values);
}

/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
}
}

If you start your application you should get the following display:

swtchart

For details check out the excellent documentation under http://www.swtchart.org/doc/index.html.

 
Tags:
Filed under: Eclipse

Leave a Reply