axes

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package axes calculates the required layout and draws the X and Y axes of a line chart.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Label

type Label struct {
	// Value if the value to be displayed.
	Value *Value

	// Position of the label within the canvas.
	Pos image.Point
}

Label is one value label on an axis.

type Value

type Value struct {
	// Value is the original unmodified value.
	Value float64
	// Rounded is the value rounded up to the nonZeroPlaces number of non-zero
	// decimal places.
	Rounded float64
	// ZeroDecimals indicates how many decimal places in Rounded have a value
	// of zero.
	ZeroDecimals int
	// NonZeroDecimals indicates the rounding precision used, it is provided on
	// a call to newValue.
	NonZeroDecimals int
	// contains filtered or unexported fields
}

Value represents one value.

func NewTextValue

func NewTextValue(text string) *Value

NewTextValue constructs a value out of the provided text.

func NewValue

func NewValue(v float64, nonZeroDecimals int) *Value

NewValue returns a new instance representing the provided value, rounding the value up to the specified number of non-zero decimal places.

func (*Value) String

func (v *Value) String() string

String implements fmt.Stringer.

func (*Value) Text

func (v *Value) Text() string

Text returns textual representation of the value.

type XDetails

type XDetails struct {
	// Start is the point where the X axis starts.
	// Both coordinates of Start are less than End.
	Start image.Point
	// End is the point where the X axis ends.
	End image.Point

	// Scale is the scale of the X axis.
	Scale *XScale

	// Labels are the labels for values on the X axis in an increasing order.
	Labels []*Label
}

XDetails contain information about the X axis that will be drawn onto the canvas.

func NewXDetails

func NewXDetails(numPoints int, yStart image.Point, cvsAr image.Rectangle, customLabels map[int]string) (*XDetails, error)

NewXDetails retrieves details about the X axis required to draw it on a canvas of the provided area. The yStart is the point where the Y axis starts. The numPoints is the number of points in the largest series that will be plotted. customLabels are the desired labels for the X axis, these are preferred if provided.

type XScale

type XScale struct {
	// Min is the minimum value on the axis.
	Min *Value
	// Max is the maximum value on the axis.
	Max *Value
	// Step is the step in the value between pixels.
	Step *Value

	// GraphWidth is the width in cells of the area on the canvas that is
	// dedicated to the graph.
	GraphWidth int
	// contains filtered or unexported fields
}

XScale is the scale of the X axis.

func NewXScale

func NewXScale(numPoints int, graphWidth, nonZeroDecimals int) (*XScale, error)

NewXScale calculates the scale of the X axis, given the number of data points in the series and the width on the canvas that is available to the X axis. The nonZeroDecimals dictates rounding of the calculated scale, see NewValue for details. The numPoints must be zero or positive number. The graphWidth must be a positive number.

func (*XScale) CellLabel

func (xs *XScale) CellLabel(x int) (*Value, error)

CellLabel given an X coordinate of a cell on the canvas, determines value of the label that should be next to it. The X coordinate must be within the graphWidth provided to NewXScale. X coordinates grow right. The returned value is rounded to the nearest int, rounding half away from zero.

func (*XScale) PixelToValue

func (xs *XScale) PixelToValue(x int) (float64, error)

PixelToValue given a X coordinate of the pixel, returns its value according to the scale. The coordinate must be within bounds of the canvas width provided to NewXScale. X coordinates grow right.

func (*XScale) ValueToCell

func (xs *XScale) ValueToCell(v int) (int, error)

ValueToCell given a value, determines the X coordinate of the cell that most closely represents the value on the line chart according to the scale. The value must be within the bounds provided to NewXScale. X coordinates grow right.

func (*XScale) ValueToPixel

func (xs *XScale) ValueToPixel(v int) (int, error)

ValueToPixel given a value, determines the X coordinate of the pixel that most closely represents the value on the line chart according to the scale. The value must be within the bounds provided to NewXScale. X coordinates grow right.

type Y

type Y struct {
	// contains filtered or unexported fields
}

Y tracks the state of the Y axis throughout the lifetime of a line chart. Implements lazy resize of the axis to decrease visual "jumping". This object is not thread-safe.

func NewY

func NewY(minVal, maxVal float64) *Y

NewY returns a new Y instance. The minVal and maxVal represent the minimum and maximum value that will be displayed on the line chart among all of the series.

func (*Y) Details

func (y *Y) Details(cvsAr image.Rectangle, mode YScaleMode) (*YDetails, error)

Details retrieves details about the Y axis required to draw it on a canvas of the provided area.

func (*Y) RequiredWidth

func (y *Y) RequiredWidth() int

RequiredWidth calculates the minimum width required in order to draw the Y axis.

func (*Y) Update

func (y *Y) Update(minVal, maxVal float64)

Update updates the stored minVal and maxVal.

type YDetails

type YDetails struct {
	// Width in character cells of the Y axis and its character labels.
	Width int

	// Start is the point where the Y axis starts.
	// Both coordinates of Start are less than End.
	Start image.Point
	// End is the point where the Y axis ends.
	End image.Point

	// Scale is the scale of the Y axis.
	Scale *YScale

	// Labels are the labels for values on the Y axis in an increasing order.
	Labels []*Label
}

YDetails contain information about the Y axis that will be drawn onto the canvas.

type YScale

type YScale struct {
	// Min is the minimum value on the axis.
	Min *Value
	// Max is the maximum value on the axis.
	Max *Value
	// Step is the step in the value between pixels.
	Step *Value

	// GraphHeight is the height in cells of the area on the canvas that is
	// dedicated to the graph itself.
	GraphHeight int
	// contains filtered or unexported fields
}

YScale is the scale of the Y axis.

func NewYScale

func NewYScale(min, max float64, graphHeight, nonZeroDecimals int, mode YScaleMode) (*YScale, error)

NewYScale calculates the scale of the Y axis, given the boundary values and the height of the graph. The nonZeroDecimals dictates rounding of the calculated scale, see NewValue for details. Max must be greater or equal to min. The graphHeight must be a positive number.

func (*YScale) CellLabel

func (ys *YScale) CellLabel(y int) (*Value, error)

CellLabel given a Y coordinate of a cell on the canvas, determines value of the label that should be next to it. The Y coordinate must be within the graphHeight provided to NewYScale. Y coordinates grow down.

func (*YScale) PixelToValue

func (ys *YScale) PixelToValue(y int) (float64, error)

PixelToValue given a Y coordinate of the pixel, returns its value according to the scale. The coordinate must be within bounds of the graph height provided to NewYScale. Y coordinates grow down.

func (*YScale) ValueToPixel

func (ys *YScale) ValueToPixel(v float64) (int, error)

ValueToPixel given a value, determines the Y coordinate of the pixel that most closely represents the value on the line chart according to the scale. The value must be within the bounds provided to NewYScale. Y coordinates grow down.

type YScaleMode added in v0.6.0

type YScaleMode int

YScaleMode determines whether the Y scale is anchored to the zero value.

const (
	// YScaleModeAnchored is a mode in which the Y scale always starts at value
	// zero regardless of the min and max on the series.
	YScaleModeAnchored YScaleMode = iota

	// YScaleModeAdaptive is a mode where the Y scale adapts its base value
	// according to the min and max on the series.
	// I.e. it starts at min for all-positive series and at max for
	// all-negative series.
	YScaleModeAdaptive
)

func (YScaleMode) String added in v0.6.0

func (ysm YScaleMode) String() string

String implements fmt.Stringer()

Jump to

Keyboard shortcuts

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