How to create a custom overlay

Article ID: 1034
Last updated: 02 Feb, 2008
Article ID: 1034
Last updated: 02 Feb, 2008
Revision: 1
Views: 5002
Posted: 19 Jan, 1999
by Dean J.
Updated: 02 Feb, 2008
by Dean J.
Problem


The prebuilt overlays that ship with ChartJ don't do everything that I need to do. I need to create my own chart type or customize an existing one.


Cause





Action


You either have the choice to create your own overlay chart or extend an existing overlay. For example, the Area Chart does not currently exist as a prebuilt overlay chart. However, it is not difficult to create your own.

To create an Area Chart overlay you can extend the existing LineChartOverlay class. The LineChartOverlay uses two points and connects them in its drawable. To make an area chart, you will use these same two points, but you will draw a polygon and fill it in from the line to the base of your chart. In this example we will use the LineChartOverlay.java file from (com.roguewave.chart.awt.overlay.overlays.v2_2)and make some changes and save it as the AreaChartOverlayKN.java.

First, change the class declaration from:

public class LineChartOverlay
implements ChartOverlay, DataModelConstants, ScaleConstants
{
To:
public class AreaChartOverlayKN extends LineChartOverlay
implements ChartOverlay, DataModelConstants, ScaleConstants
{

Change all the constructors to use the new class name.

Finally, you need to set the add2DDrawable to use your new drawable that we will write. Change the line:

chartGraphics.add2DDrawable(new DeviceLines(x1, y1, x2, y2, color_));
to:
chartGraphics.add2DDrawable(new DeviceAreaChartKN(x1, y1, x2, y2, color_, base));

I used the same DeviceLines class, made a few changes and renamed it DeviceAreaChartKN. (The original file can be found in com.roguewave.chart.awt.drawables.device.v2_2). I have added a parameter (int base) so that I can pass it the coordinate of the bottom of the chart.

In my new device class, I created the new class

  public DeviceAreaChartKN(int[] x1, int[] y1, int[] x2, int[] y2, Color color,
int baseCoord)
I created a new int to keep track of bottom coordinate of the chart

baseCoord_ = baseCoord; //KN new bottom coord

The most important step is that I created a new drawOn method to create my overlay.

 

  public void drawOn(Canvas3D canvas, Graphics g)
{
g.setColor(color_);
for(int i = low_; i < end_; i++)
{
//g.drawLine(x1_[i], y1_[i], x2_[i], y2_[i]);//The old method from DeviceLine
Polygon knPolygon = new Polygon();//KN new drawOn method to draw polygon
knPolygon.addPoint(x1_[i],y1_[i]);
knPolygon.addPoint(x2_[i],y2_[i]);
knPolygon.addPoint(x2_[i],baseCoord_-1);//subtract one so you don't overwrite the axis
knPolygon.addPoint(x1_[i],baseCoord_-1);

g.drawPolygon(knPolygon);
g.fillPolygon(knPolygon);
}
}

Instead of the old line drawing method, I created a new method to draw a polygon. You can use any of the JDK graphics calls to create whatever you need on your chart.

The files modified for this example
AreaChartOverlayKN.java
DeviceAreaChartKN.java
OverlayExample.java

This article was:   Helpful | Not helpful
Report an issue
Article ID: 1034
Last updated: 02 Feb, 2008
Revision: 1
Views: 5002
Posted: 19 Jan, 1999 by Dean J.
Updated: 02 Feb, 2008 by Dean J.
Attached files


Others in this category