GraphWidget for SWT

Sample Plot

PlotControl is a simple SWT widget for plotting a line graph. It includes functionality for filling and stroking the resultant curve, handling positive and negative x/y values, scaling to fit inside the widget space and standard or MRTG-style plots.

This is a development release - as such, the download includes source only, it is not compiled into a JAR or anything fancy like that.

Download

Download: GraphWidget-0.1.zip
License: GNU GPL v2
Release date: Jan 17, 2007

Example Usage

Create a Data Provider:
A data provider returns the values to plot - given any y-value, it will return the appropriate x-value. The interface is simple, and it can be backed by any data storage you'd like - however the data provider is queried by the UI thread, so this should not be a long-running computation. (Better to cache a lookup table.)

An example data provider (which draws a straight line at x=10):

public class SimplePlotData extends PlotData
{
	private Color fillColor;
	private Color strokeColor;

	SimplePlotData(Display display)
	{
		fillColor = display.getSystemColor(SWT.COLOR_GREEN);
		strokeColor = display.getSystemColor(SWT.COLOR_DARK_GREEN);
	}

	public Color getFillColor() { return fillColor; }
	public Color getStrokColor() { return strokeColor; }
	public int getStrokeWidth() { return 2; }
	public int getValue(int y) { return 10; }
	public int getMaximum() { return 10; }
}

Create a PlotControl:
Once you have created a data provider, you can instantiate a PlotControl which uses that provider. PlotControl objects are configured by PlotSetup objects, which setup their size (domain and range of the graph) and their data providers.

Example PlotControl instantiation:

// setup a plot from y=-50 to y=50, with a domain stepping of 5
// (ie, this will plot y={ -50, -45, -40, ... 40, 45, 50 }
// guidelines will be lightly drawn at y={-50, -40 .. 40, 50 }
// range will be drawn from x=0 to x=20
PlotSetup plotSetup = new PlotSetup();
plotSetup.setDomainStart(-50);
plotSetup.setDomainEnd(50);
plotSetup.setDomainStepping(5);
plotSetup.setDomainGuideStepping(10);
plotSetup.setGuideAlpha(25);
plotSetup.setRangeStart(0);
plotSetup.setRangeEnd(20);
plotSetup.addPlotData(new SimplePlotData(display));

PlotControl plotControl = new PlotControl(shell, SWT.NONE);
plotControl.setPlotSetup(plotSetup);

At this point, placing the PlotControl into your composite's layout should be all that's required, and it should draw a dark green line at x=10, filled down to the x-axis in lighter green.

Further Information

This is available under the GNU GPL (v2), so please contribute back bug fixes and new features. This software is available without warranty, so if your computer catches fire, you're on your own. Questions, bug reports, comments please contact me.

Edward Thomson is a Software Engineer at Teamprise, where he develops cross-platform client solutions for Microsoft Team Foundation Server, with an emphasis on Macintosh compatibility and IDE integration.