plot

package
v0.0.0-...-7c6133f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2016 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

plot provides an API for setting up plots, and primitives for drawing on plots.

Plot is the basic type for creating a plot, setting the title, axis labels, legend, tick marks, etc. Types implementing the Plotter interface can draw to the data area of a plot using the primitives made available by this package. Some standard implementations of the Plotter interface can be found in the code.google.com/p/plotinum/plotter package which is documented here: http://godoc.org/code.google.com/p/plotinum/plotter

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConstantTicks

func ConstantTicks(ts []Tick) func(float64, float64) []Tick

ConstantTicks returns a function suitable for the Tick.Marker field of an Axis. This function returns the given set of ticks.

func LinearScale

func LinearScale(min, max, x float64) float64

LinearScale an be used as the value of an Axis.Scale function to set the axis to a standard linear scale.

func LogScale

func LogScale(min, max, x float64) float64

LocScale can be used as the value of an Axis.Scale function to set the axis to a log scale.

Types

type Axis

type Axis struct {
	// Min and Max are the minimum and maximum data
	// values represented by the axis.
	Min, Max float64

	Label struct {
		// Text is the axis label string.
		Text string

		// TextStyle is the style of the axis label text.
		TextStyle
	}

	// LineStyle is the style of the axis line.
	LineStyle

	// Padding between the axis line and the data.  Having
	// non-zero padding ensures that the data is never drawn
	// on the axis, thus making it easier to see.
	Padding vg.Length

	Tick struct {
		// Label is the TextStyle on the tick labels.
		Label TextStyle

		// LineStyle is the LineStyle of the tick lines.
		LineStyle

		// Length is the length of a major tick mark.
		// Minor tick marks are half of the length of major
		// tick marks.
		Length vg.Length

		// Marker returns the tick marks.  Any tick marks
		// returned by the Marker function that are not in
		// range of the axis are not drawn.
		Marker func(min, max float64) []Tick
	}

	// Scale transforms a value given in the data coordinate system
	// to the normalized coordinate system of the axis—its distance
	// along the axis as a fraction of the axis range.
	Scale func(min, max, x float64) float64
}

An Axis represents either a horizontal or vertical axis of a plot.

func (*Axis) Norm

func (a *Axis) Norm(x float64) float64

Norm returns the value of x, given in the data coordinate system, normalized to its distance as a fraction of the range of this axis. For example, if x is a.Min then the return value is 0, and if x is a.Max then the return value is 1.

type BoxGlyph

type BoxGlyph struct{}

BoxGlyph is a glyph that draws a filled square.

func (BoxGlyph) DrawGlyph

func (BoxGlyph) DrawGlyph(da *DrawArea, sty GlyphStyle, pt Point)

DrawGlyph implements the Glyph interface.

type CircleGlyph

type CircleGlyph struct{}

CircleGlyph is a glyph that draws a solid circle.

func (CircleGlyph) DrawGlyph

func (c CircleGlyph) DrawGlyph(da *DrawArea, sty GlyphStyle, pt Point)

DrawGlyph implements the GlyphDrawer interface.

type CrossGlyph

type CrossGlyph struct{}

CrossGlyph is a glyph that draws a big X.

func (CrossGlyph) DrawGlyph

func (CrossGlyph) DrawGlyph(da *DrawArea, sty GlyphStyle, pt Point)

DrawGlyph implements the Glyph interface.

type DataRanger

type DataRanger interface {
	// DataRange returns the range of X and Y values.
	DataRange() (xmin, xmax, ymin, ymax float64)
}

DataRanger wraps the DataRange method.

type DrawArea

type DrawArea struct {
	vg.Canvas
	Rect
}

A DrawArea is a vector graphics canvas along with an associated Rect defining a section of the canvas to which drawing should take place.

func MakeDrawArea

func MakeDrawArea(c interface {
	vg.Canvas
	Size() (vg.Length, vg.Length)
}) DrawArea

MakeDrawArea returns a new DrawArea for a canvas with a Size method.

func MakeDrawAreaSize

func MakeDrawAreaSize(c vg.Canvas, w, h vg.Length) DrawArea

MakeDrawAreaSize returns a new DrawArea of the given size for a canvas.

func (*DrawArea) Center

func (da *DrawArea) Center() Point

Center returns the center point of the area

func (*DrawArea) ClipLinesX

func (da *DrawArea) ClipLinesX(lines ...[]Point) (clipped [][]Point)

ClipLineX returns a slice of lines that represent the given line clipped in the X direction.

func (*DrawArea) ClipLinesXY

func (da *DrawArea) ClipLinesXY(lines ...[]Point) [][]Point

ClipLineXY returns a slice of lines that represent the given line clipped in both X and Y directions.

func (*DrawArea) ClipLinesY

func (da *DrawArea) ClipLinesY(lines ...[]Point) (clipped [][]Point)

ClipLineY returns a slice of lines that represent the given line clipped in the Y direction.

func (*DrawArea) ClipPolygonX

func (da *DrawArea) ClipPolygonX(pts []Point) []Point

ClipPolygonX returns a slice of lines that represent the given polygon clipped in the X direction.

func (*DrawArea) ClipPolygonXY

func (da *DrawArea) ClipPolygonXY(pts []Point) []Point

ClipPolygonXY returns a slice of lines that represent the given polygon clipped in both X and Y directions.

func (*DrawArea) ClipPolygonY

func (da *DrawArea) ClipPolygonY(pts []Point) []Point

ClipPolygonY returns a slice of lines that represent the given polygon clipped in the Y direction.

func (*DrawArea) Contains

func (da *DrawArea) Contains(p Point) bool

Contains returns true if the DrawArea contains the point.

func (*DrawArea) ContainsX

func (da *DrawArea) ContainsX(x vg.Length) bool

Contains returns true if the DrawArea contains the x coordinate.

func (*DrawArea) ContainsY

func (da *DrawArea) ContainsY(y vg.Length) bool

ContainsY returns true if the DrawArea contains the y coordinate.

func (*DrawArea) DrawGlyph

func (da *DrawArea) DrawGlyph(sty GlyphStyle, pt Point)

DrawGlyph draws the given glyph to the draw area. If the point is not within the DrawArea or the sty.Shape is nil then nothing is drawn.

func (*DrawArea) DrawGlyphNoClip

func (da *DrawArea) DrawGlyphNoClip(sty GlyphStyle, pt Point)

DrawGlyphNoClip draws the given glyph to the draw area. If the sty.Shape is nil then nothing is drawn.

func (*DrawArea) FillPolygon

func (da *DrawArea) FillPolygon(clr color.Color, pts []Point)

FillPolygon fills a polygon with the given color.

func (*DrawArea) FillText

func (da *DrawArea) FillText(sty TextStyle, x, y vg.Length, xalign, yalign float64, txt string)

FillText fills lines of text in the draw area. The text is offset by its width times xalign and its height times yalign. x and y give the bottom left corner of the text befor e it is offset.

func (*DrawArea) SetLineStyle

func (da *DrawArea) SetLineStyle(sty LineStyle)

SetLineStyle sets the current line style

func (*DrawArea) StrokeLine2

func (da *DrawArea) StrokeLine2(sty LineStyle, x0, y0, x1, y1 vg.Length)

StrokeLine2 draws a line between two points in the given DrawArea.

func (*DrawArea) StrokeLines

func (da *DrawArea) StrokeLines(sty LineStyle, lines ...[]Point)

StrokeLines draws a line connecting a set of points in the given DrawArea.

func (*DrawArea) X

func (da *DrawArea) X(x float64) vg.Length

X returns the value of x, given in the unit range, in the drawing coordinates of this draw area. A value of 0, for example, will return the minimum x value of the draw area and a value of 1 will return the maximum.

func (*DrawArea) Y

func (da *DrawArea) Y(y float64) vg.Length

Y returns the value of x, given in the unit range, in the drawing coordinates of this draw area. A value of 0, for example, will return the minimum y value of the draw area and a value of 1 will return the maximum.

type GlyphBox

type GlyphBox struct {
	// The glyph location in normalized coordinates.
	X, Y float64

	// Rect is the offset of the glyph's minimum drawing
	// point relative to the glyph location and its size.
	Rect
}

A GlyphBox describes the location of a glyph and the offset/size of its bounding box.

If the Rect.Size.X is non-positive (<= 0) then the GlyphBox is ignored when computing the horizontal padding, and likewise with Rect.Size.Y and the vertical padding.

type GlyphBoxer

type GlyphBoxer interface {
	GlyphBoxes(*Plot) []GlyphBox
}

GlyphBoxer wraps the GlyphBoxes method. It should be implemented by things that meet the Plotter interface that draw glyphs so that their glyphs are not clipped if drawn near the edge of the DrawArea.

When computing padding, the plot ignores GlyphBoxes as follows: If the Size.X > 0 and the X value is not in range of the X axis then the box is ignored. If Size.Y > 0 and the Y value is not in range of the Y axis then the box is ignored.

Also, GlyphBoxes with Size.X <= 0 are ignored when computing horizontal padding and GlyphBoxes with Size.Y <= 0 are ignored when computing vertical padding. This is useful for things like box plots and bar charts where the boxes and bars are considered to be glyphs in the X direction (and thus need padding), but may be clipped in the Y direction (and do not need padding).

type GlyphDrawer

type GlyphDrawer interface {
	// DrawGlyph draws the glyph at the given
	// point, with the given color and radius.
	DrawGlyph(*DrawArea, GlyphStyle, Point)
}

A GlyphDrawer wraps the DrawGlyph function.

type GlyphStyle

type GlyphStyle struct {
	// Color is the color used to draw the glyph.
	color.Color

	// Radius specifies the size of the glyph's radius.
	Radius vg.Length

	// Shape draws the shape of the glyph.
	Shape GlyphDrawer
}

A GlyphStyle specifies the look of a glyph used to draw a point on a plot.

func (GlyphStyle) Rect

func (g GlyphStyle) Rect() Rect

Rect returns the rectangle surrounding this glyph, assuming that it is drawn centered at 0,0

type Legend

type Legend struct {
	// TextStyle is the style given to the legend
	// entry texts.
	TextStyle

	// Padding is the amount of padding to add
	// betweeneach entry of the legend.  If Padding
	// is zero then entries are spaced based on the
	// font size.
	Padding vg.Length

	// Top and Left specify the location of the legend.
	// If Top is true the legend is located along the top
	// edge of the plot, otherwise it is located along
	// the bottom edge.  If Left is true then the legend
	// is located along the left edge of the plot, and the
	// text is positioned after the icons, otherwise it is
	// located along the right edge and the text is
	// positioned before the icons.
	Top, Left bool

	// XOffs and YOffs are added to the legend's
	// final position.
	XOffs, YOffs vg.Length

	// ThumbnailWidth is the width of legend thumbnails.
	ThumbnailWidth vg.Length
	// contains filtered or unexported fields
}

A Legend gives a description of the meaning of different data elements of the plot. Each legend entry has a name and a thumbnail, where the thumbnail shows a small sample of the display style of the corresponding data.

func (*Legend) Add

func (l *Legend) Add(name string, thumbs ...Thumbnailer)

Add adds an entry to the legend with the given name. The entry's thumbnail is drawn as the composite of all of the thumbnails.

type LineStyle

type LineStyle struct {
	// Color is the color of the line.
	Color color.Color

	// Width is the width of the line.
	Width vg.Length

	Dashes   []vg.Length
	DashOffs vg.Length
}

LineStyle describes what a line will look like.

type Plot

type Plot struct {
	Title struct {
		// Text is the text of the plot title.  If
		// Text is the empty string then the plot
		// will not have a title.
		Text string

		// Padding is the amount of padding
		// between the bottom of the title and
		// the top of the plot.
		Padding vg.Length

		TextStyle
	}

	// BackgroundColor is the background color of the plot.
	// The default is White.
	BackgroundColor color.Color

	// X and Y are the horizontal and vertical axes
	// of the plot respectively.
	X, Y Axis

	// Legend is the plot's legend.
	Legend Legend
	// contains filtered or unexported fields
}

Plot is the basic type representing a plot.

func New

func New() (*Plot, error)

New returns a new plot with some reasonable default settings.

func (*Plot) Add

func (p *Plot) Add(ps ...Plotter)

Add adds a Plotters to the plot.

If the plotters implements DataRanger then the minimum and maximum values of the X and Y axes are changed if necessary to fit the range of the data.

When drawing the plot, Plotters are drawn in the order in which they were added to the plot.

func (*Plot) DataDrawArea

func (p *Plot) DataDrawArea(da DrawArea) DrawArea

DataDrawArea returns a new DrawArea that is the subset of the given draw area into which the plot data will be drawn.

func (*Plot) Draw

func (p *Plot) Draw(da DrawArea)

Draw draws a plot to a DrawArea.

Plotters are drawn in the order in which they were added to the plot. Plotters that implement the GlyphBoxer interface will have their GlyphBoxes taken into account when padding the plot so that none of their glyphs are clipped.

func (*Plot) DrawGlyphBoxes

func (p *Plot) DrawGlyphBoxes(da *DrawArea)

DrawGlyphBoxes draws red outlines around the plot's GlyphBoxes. This is intended for debugging.

func (*Plot) GlyphBoxes

func (p *Plot) GlyphBoxes(*Plot) (boxes []GlyphBox)

GlyphBoxes returns the GlyphBoxes for all plot data that meet the GlyphBoxer interface.

func (*Plot) HideAxes

func (p *Plot) HideAxes()

HideAxes hides the X and Y axes.

func (*Plot) HideX

func (p *Plot) HideX()

HideX configures the X axis so that it will not be drawn.

func (*Plot) HideY

func (p *Plot) HideY()

HideY configures the Y axis so that it will not be drawn.

func (*Plot) NominalX

func (p *Plot) NominalX(names ...string)

NominalX configures the plot to have a nominal X axis—an X axis with names instead of numbers. The X location corresponding to each name are the integers, e.g., the x value 0 is centered above the first name and 1 is above the second name, etc. Labels for x values that do not end up in range of the X axis will not have tick marks.

func (*Plot) NominalY

func (p *Plot) NominalY(names ...string)

NominalY is like NominalX, but for the Y axis.

func (*Plot) Save

func (p *Plot) Save(width, height float64, file string) (err error)

Save saves the plot to an image file. Width and height are specified in inches, and the file format is determined by the extension. Supported extensions are .eps, .jpg, .jpeg, .pdf, .png, .svg, and .tiff.

func (*Plot) Transforms

func (p *Plot) Transforms(da *DrawArea) (x, y func(float64) vg.Length)

Transforms returns functions to transfrom from the x and y data coordinate system to the draw coordinate system of the given draw area.

type Plotter

type Plotter interface {
	// Plot draws the data to a DrawArea.
	Plot(DrawArea, *Plot)
}

Plotter is an interface that wraps the Plot method. Some standard implementations of Plotter can be found in the code.google.com/p/plotinum/plotter package, documented here: http://go.pkgdoc.org/code.google.com/p/plotinum/plotter

type PlusGlyph

type PlusGlyph struct{}

PlusGlyph is a glyph that draws a plus sign

func (PlusGlyph) DrawGlyph

func (PlusGlyph) DrawGlyph(da *DrawArea, sty GlyphStyle, pt Point)

DrawGlyph implements the Glyph interface.

type Point

type Point struct {
	X, Y vg.Length
}

A Point is a location in 2d space.

Points are used for drawing, not for data. For data, see the XYer interface.

func Pt

func Pt(x, y vg.Length) Point

Pt returns a point from x, y coordinates.

type PyramidGlyph

type PyramidGlyph struct{}

PyramidGlyph is a glyph that draws a filled triangle.

func (PyramidGlyph) DrawGlyph

func (PyramidGlyph) DrawGlyph(da *DrawArea, sty GlyphStyle, pt Point)

DrawGlyph implements the Glyph interface.

type Rect

type Rect struct {
	Min, Size Point
}

A Rect represents a Rectangular region of 2d space.

func (Rect) Max

func (r Rect) Max() Point

Max returns the maxmium x and y values of a Rect.

type RingGlyph

type RingGlyph struct{}

RingGlyph is a glyph that draws the outline of a circle.

func (RingGlyph) DrawGlyph

func (RingGlyph) DrawGlyph(da *DrawArea, sty GlyphStyle, pt Point)

DrawGlyph implements the Glyph interface.

type SquareGlyph

type SquareGlyph struct{}

SquareGlyph is a glyph that draws the outline of a square.

func (SquareGlyph) DrawGlyph

func (SquareGlyph) DrawGlyph(da *DrawArea, sty GlyphStyle, pt Point)

DrawGlyph implements the Glyph interface.

type TextStyle

type TextStyle struct {
	// Color is the text color.
	Color color.Color

	// Font is the font description.
	Font vg.Font
}

TextStyle describes what text will look like.

func (TextStyle) Height

func (sty TextStyle) Height(txt string) vg.Length

Height returns the height of the text when using the given font.

func (TextStyle) Rect

func (sty TextStyle) Rect(txt string) Rect

Rect returns a rectangle giving the bounds of this text assuming that it is drawn at 0, 0

func (TextStyle) Width

func (sty TextStyle) Width(txt string) (max vg.Length)

Width returns the width of lines of text when using the given font.

type Thumbnailer

type Thumbnailer interface {
	// Thumbnail draws an thumbnail representing
	// a legend entry.  The thumbnail will usually show
	// a smaller representation of the style used
	// to plot the corresponding data.
	Thumbnail(da *DrawArea)
}

Thumbnailer wraps the Thumbnail method, which draws the small image in a legend representing the style of data.

type Tick

type Tick struct {
	// Value is the data value marked by this Tick.
	Value float64

	// Label is the text to display at the tick mark.
	// If Label is an empty string then this is a minor
	// tick mark.
	Label string
}

A Tick is a single tick mark on an axis.

func DefaultTicks

func DefaultTicks(min, max float64) (ticks []Tick)

DefaultTicks is suitable for the Tick.Marker field of an Axis, it returns a resonable default set of tick marks.

func LogTicks

func LogTicks(min, max float64) []Tick

LogTicks is suitable for the Tick.Marker field of an Axis, it returns tick marks suitable for a log-scale axis.

func (Tick) IsMinor

func (t Tick) IsMinor() bool

IsMinor returns true if this is a minor tick mark.

type TriangleGlyph

type TriangleGlyph struct{}

TriangleGlyph is a glyph that draws the outline of a triangle.

func (TriangleGlyph) DrawGlyph

func (TriangleGlyph) DrawGlyph(da *DrawArea, sty GlyphStyle, pt Point)

DrawGlyph implements the Glyph interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL