axes

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2020 License: Apache-2.0 Imports: 8 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

func RequiredHeight

func RequiredHeight(max int, customLabels map[int]string, lo LabelOrientation) int

RequiredHeight calculates the minimum height required in order to draw the X axis and its labels.

func RequiredWidth

func RequiredWidth(minVal, maxVal float64) int

RequiredWidth calculates the minimum width required in order to draw the Y axis and its labels when displaying values that have this minimum and maximum among all the series.

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 LabelOrientation

type LabelOrientation int

LabelOrientation represents the orientation of text labels.

const (
	// LabelOrientationHorizontal is the default label orientation where text
	// flows horizontally.
	LabelOrientationHorizontal LabelOrientation = iota

	// LabelOrientationVertical is an orientation where text flows vertically.
	LabelOrientationVertical
)

func (LabelOrientation) String

func (lo LabelOrientation) String() string

String implements fmt.Stringer()

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, opts ...ValueOption) *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 ValueOption added in v0.10.0

type ValueOption interface {
	// contains filtered or unexported methods
}

ValueOption is used to provide options to the NewValue function.

func ValueFormatter added in v0.10.0

func ValueFormatter(formatter func(float64) string) ValueOption

ValueFormatter sets a custom formatter for 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

	// Properties are the properties that were used on the call to NewXDetails.
	Properties *XProperties
}

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

func NewXDetails

func NewXDetails(cvsAr image.Rectangle, xp *XProperties) (*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.

func (*XDetails) String

func (xd *XDetails) String() string

String implements fmt.Stringer.

type XProperties

type XProperties struct {
	// Min is the minimum value on the axis, i.e. the position of the first
	// displayed value from the series.
	Min int
	// Max is the maximum value on the axis, i.e. the position of the last
	// displayed value from the series.
	Max int
	// ReqYWidth is the width required for the Y axis and its labels.
	ReqYWidth int
	// CustomLabels are the desired labels for the X axis, these are preferred
	// if provided.
	CustomLabels map[int]string
	// LO is the desired orientation of labels under the X axis.
	LO LabelOrientation
}

XProperties are the properties of the X axis.

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(min, max int, graphWidth, nonZeroDecimals int) (*XScale, error)

NewXScale calculates the scale of the X axis, given the boundary values 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 boundary values must be positive or zero and must be min <= max. 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) String

func (xs *XScale) String() string

String implements fmt.Stringer.

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 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.
	// The Y coordinate of Start is less than the Y coordinate of 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.

func NewYDetails

func NewYDetails(cvsAr image.Rectangle, yp *YProperties) (*YDetails, error)

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

type YProperties

type YProperties struct {
	// Min is the minimum value on the axis.
	Min float64
	// Max is the maximum value on the axis.
	Max float64
	// ReqXHeight is the height required for the X axis and its labels.
	ReqXHeight int
	// ScaleMode determines how the Y axis scales.
	ScaleMode YScaleMode
	// ValueFormatter is the formatter used to format numeric values to string representation.
	ValueFormatter func(float64) string
}

YProperties are the properties of the Y axis.

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, valueFormatter func(float64) string) (*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) String added in v0.8.0

func (ys *YScale) String() string

String implements fmt.Stringer.

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

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

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