skip to main content
Gantt > Programmer's documentation > Developing with the JViews Charts SDK > Displaying and Writing a Grid > Writing a new grid
 
Writing a new grid
The complete source code can be found in <installdir>/jviews-charts/samples/realtime/src/realtime/SimpleGrid.java.
You can create a new type of grid by subclassing the IlvGrid class and overriding one or both of the draw methods.
A grid draws its grid lines according to the grid line values. These values are used to determine the anchor point of a grid line on the associated axis and are expressed in the data coordinate system.
By default, the draw method computes the grid line values to match the step of the scale of the associated axis and invokes the draw method with these values as parameter to perform the drawing operations.
Change the way the grid line values are computed by default to have grid lines equally spaced from a specified delta, and no longer snapped on the major ticks of the scale graduation.
1. Extend the IlvGrid class.
 class SimpleGrid extends IlvGrid
The spacing between the two grid lines is specified at construction time as a constructor parameter and expressed in the data coordinate system by a double value.
The grid constructor is:
 double spacing; public SimpleGrid(Paint majPaint,
double spacing) { super(majPaint); this.spacing = spacing;
}
2. Override the draw(Graphics) method.
Since you want to change the way the grid line values are computed, not the way they are drawn, you only need to override the draw method to change its default implementation.
This method computes the grid line values according to the current visible range, and iterates over it with a step equal to the specified spacing:
 public void draw(Graphics g) { if (getChart()
== null || spacing <= 0) return; IlvDataInterval
itv = getAxis().getVisibleRange(); IlvDoubleArray gridlines =
new IlvDoubleArray(16); double val = Math.ceil(itv.getMin()/spacing)*spacing;
     while (itv.isInside(val)) { gridlines.add(val);
     val += spacing; } if (gridlines.size() > 0)
     draw(g, gridlines, true); }
The implementation is quite simple: starting from the visible range minimum bound, you iterate on the grid line values by adding the expected spacing to the previous value until the visible range maximum bound is reached. Then, if at least one grid line value has been computed, you call the draw(Graphics, IlvDoubleArray, boolean) method to draw the grid with your own values.
Another example of a custom IlvGrid subclass can be found in <installdir>/jviews-charts/samples/monitor/src/monitor/MemoryMonitor.java. In this example, the IlvGrid class is extended in order to display a fixed number of grid lines. You can find the source code of this class in the FixedGrid.java file.

Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.