DrawGrid:
Filter:
Classes | GUI > Accessories

DrawGrid

Draws grid lines on a UserView for plotting
Location: NOT INSTALLED!

Description

DrawGrid is used to draw GridLines and its labels on a UserView. It is notably used by Plotter to draw the grid lines on a plot but can also be used to add grid lines to any UserView, e.g. behind sliders or another GUI element.

See the example below for its basic use in a UserView.

Note that DrawGrid does not hold any reference to the UserView but is meant to have its -draw method called inside of the -drawFunc of the UserView. It only needs to know what bounds to draw the grid lines in and what the horizontal and vertical GridLines are.

Class Methods

.new

Arguments:

bounds

A Point or Rect describing the size and position of the grid within the parent view (not including any labels).

horzGrid

A grid lines object for the x-axis, instantiated via GridLines, or ControlSpec: -grid method, or nil (resulting in no grid lines).

vertGrid

A grid lines object for the y-axis, see horzGrid.

Returns:

A DrawGrid.

Discussion:

The warp behavior of the horizGrid and vertGrid is based on the warp behavior of the ControlSpec used by the grid lines object assigned to each axis.

Multiple DrawGrid may be used to draw grids on a single UserView.

See -preview if you'd like to preview modifications of this DrawGrid. See Examples below.

Inherited class methods

Instance Methods

.draw

This draws to the currently active UserView. This method is meant to be called from inside the -drawFunc of a UserView.

Returns:

nil

Discussion:

See the example below for its basic use in a UserView, including how to manage its bounds when the enclosing view resizes.

.horzGrid

Set the x-axis grid lines.

Arguments:

g

An AbstractGridLines subclass, instantiated via GridLines, or ControlSpec: -grid method, or nil (resulting in no grid lines).

Returns:

Self.

.vertGrid

Set the y-axis grid lines.

Arguments:

g

An AbstractGridLines subclass, instantiated via GridLines, or ControlSpec: -grid method, or nil (resulting in no grid lines).

Returns:

Self.

.bounds

Get/set bounds describing the extents of the grid (not including any labels).

Arguments:

b

A Rect.

Returns:

A Rect.

.font

Get/set the font used by the grid lines labels.

Arguments:

f

A Font.

Returns:

A Font.

.fontColor

Get/set the font color.

Arguments:

c

A Color.

Returns:

A Color.

.gridColors

Set the colors of the grid lines for each axis.

Arguments:

colors

An Array of two colors for the x and y grid lines, respectively.

Returns:

Self.

.opacity

Get/set opacity.

Returns:

A Float.

.smoothing

A Boolean which turns on/off anti-aliasing. See Pen: *smoothing.

Returns:

A Boolean.

.linePattern

Set the line dash pattern. The value should be a FloatArray of values that specify the lengths of the alternating dashes and spaces. For example, FloatArray[10.0, 3.0, 5.0, 3.0], for dashes of lengths 10.0 and 5.0 pixels, separated by spaces of 3.0 pixels. See Pen: *lineDash.

Returns:

Self.

.x

A DrawGridX object that draws the x (horizontal) axis. In general you shouldn't need to set this.

Returns:

A DrawGridX.

.y

A DrawGridY object that draws the y (vertical) axis. In general you shouldn't need to set this.

Returns:

A DrawGridY.

.numTicks

Set the approximate number of grid lines ("ticks") for each axis. If set, the number of ticks is fixed and numTicks takes precedence over -tickSpacing. If nil, the number of grid lines change with the view size, constrained by the tickSpacing. Default: nil.

See Examples below.

Arguments:

x

Approximate number of grid lines ("ticks") for the x-axis.

y

Approximate number of grid lines for the y-axis.

Discussion:

The resulting number of ticks is approximate because of the underlying algorithm in AbstractGridLines: -niceNum, which tries to find suitable values for the grid lines based on the data range and your requested numTicks. You can observe the behavior of GridLines:-niceNum with this snippet:

.tickSpacing

Set the minimum spacing between grid lines ("ticks") for each axis. The number of grid lines will change with the view size, but won't be spaced less than this tickSpacing, allowing you to control the density of grid lines. However if -numTicks is not nil, it takes precedence over tickSpacing.

See Examples below.

Arguments:

x

Minimum spacing between grid lines ("ticks") on the x-axis (pixels, default: 64).

y

Minimum spacing between grid lines on the y-axis (pixels, default: 64).

.preview

Preview this DrawGrid object by creating a window with a view showing the DrawGrid in its current state.

If the DrawGrid is modified, you can -refresh the returned UserView to see its updated state, or simply call -preview again to update the view (or create the preview again if the Window was closed). See Examples below.

Returns:

A UserView which draws this DrawGrid.

.copy

Safely make a copy of this object and its working members.

Returns:

A new DrawGrid.

Inherited instance methods

Examples

Basic use in a UserView

Testing and modifying

For previewing the look and feel of your GridLines, you can render your DrawGrid using the -preview method:

Use DrawGrid's convenience methods to set the grids' properties, then refresh the view:

Or just call .preview again and it will refresh the existing view:

Proterties of the individual x- and y-grids can also be accessed and changed separately: