plot

package module
v0.0.0-...-6b31088 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: MIT Imports: 5 Imported by: 8

README

plot

plot is a plotting library with the focus on visualizing benchmark results.

Everything here is subject to change.

TODO

  • Add text box
  • Implement text origin
  • Create coordinate abstraction, so that density / violin and other plots can be rotated by just swapping their axes.
  • Implement column bar charts
  • Create an abstraction for dataset, so that sorting can be reused
  • Add color and style palettes
  • Fix density plot scaling
  • Fix violin plot scaling
  • Figure out how to find the 50% percentile of densities

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DurationTo

func DurationTo(durations []time.Duration, scale time.Duration) []float64

DurationTo converts durations to the specified scale.

func DurationToNanoseconds

func DurationToNanoseconds(durations []time.Duration) []float64

DurationToNanoseconds converts an slice of durations to nanoseconds.

func DurationToSeconds

func DurationToSeconds(durations []time.Duration) []float64

DurationToSeconds converts an slice of durations to seconds.

func Int32sToFloat64s

func Int32sToFloat64s(xs []int32) []float64

Int32sToFloat64s convers a slice of int32-s to float64-s.

func Int64sToFloat64s

func Int64sToFloat64s(xs []int64) []float64

Int64sToFloat64s convers a slice of int64-s to float64-s.

func IntsToFloat64s

func IntsToFloat64s(xs []int) []float64

IntssToFloat64s convers a slice of ints to float64-s.

Types

type AutomaticTicks

type AutomaticTicks struct{}

AutomaticTicks tries to automatically figure out which ticks to use.

func (AutomaticTicks) Ticks

func (ticks AutomaticTicks) Ticks(axis *Axis) []Tick

Ticks automatically calculates appropriate ticks for an axis.

type Axis

type Axis struct {
	// Min value of the axis (in value space)
	Min float64
	// Max value of the axis (in value space)
	Max float64

	Flip bool

	Ticks      Ticks
	MajorTicks int
	MinorTicks int

	Transform AxisTransform
}

Axis defines an axis that defines how values are transformed to canvas space.

func NewAxis

func NewAxis() *Axis

NewAxis creates a new axis.

func NewPercentilesAxis

func NewPercentilesAxis() *Axis

NewPercentilesAxis creates X axis for plotting percentiles. It uses 5 digits of precision max.

func (*Axis) FromCanvas

func (axis *Axis) FromCanvas(s Length, screenMin, screenMax Length) float64

FromCanvas converts canvas point to value point.

func (*Axis) Include

func (axis *Axis) Include(min, max float64)

Include ensures that min and max can be displayed on the axis.

func (*Axis) IsValid

func (axis *Axis) IsValid() bool

IsValid returns whether axis has been defined.

func (*Axis) MakeNice

func (axis *Axis) MakeNice()

MakeNice tries to adjust min, max such they look nice given the MajorTicks and MinorTicks.

func (*Axis) SetScreenLog1P

func (axis *Axis) SetScreenLog1P(compress float64)

SetScreenLog1P sets axis to logarithm space.

func (*Axis) ToCanvas

func (axis *Axis) ToCanvas(v float64, screenMin, screenMax Length) Length

ToCanvas converts value to canvas space.

type AxisGroup

type AxisGroup struct {
	X, Y *Axis
	Elements
}

AxisGroup allows sub-elements to have different different axes defined rather than the top-level plot.

func NewAxisGroup

func NewAxisGroup(els ...Element) *AxisGroup

NewAxisGroup creates a new axis group.

func (*AxisGroup) Draw

func (group *AxisGroup) Draw(plot *Plot, canvas Canvas)

Draw draws elements bound to this axis-group creating an axis automatically if necessary.

func (*AxisGroup) Update

func (group *AxisGroup) Update()

Update updates the axis values.

type AxisTransform

type AxisTransform interface {
	ToCanvas(axis *Axis, v float64, screenMin, screenMax Length) Length
	FromCanvas(axis *Axis, s Length, screenMin, screenMax Length) float64
}

AxisTransform transforms values between canvas and value-space.

type Bar

type Bar struct {
	Style
	Label string

	DynamicWidth    bool
	DynamicMinWidth float64

	Data []Point
}

Bar implements a stacked-bar plot.

func NewBar

func NewBar(label string, points []Point) *Bar

NewBar creates a bar plot from the given points.

func (*Bar) Draw

func (bar *Bar) Draw(plot *Plot, canvas Canvas)

Draw draws the element to canvas.

func (*Bar) Stats

func (bar *Bar) Stats() Stats

Stats calculates stats from the values.

type Canvas

type Canvas interface {
	Bounds() Rect
	Layer(index int) Canvas
	Clip(r Rect) Canvas
	Context(r Rect) Canvas
	Text(text string, at Point, style *Style)
	Poly(points []Point, style *Style)
	Rect(r Rect, style *Style)
}

Canvas describes interface for drawing graphics.

type Dataset

type Dataset interface {
	Element
	// TODO: remove and replace with recommended Axis
	Stats() Stats
}

Dataset represents an Element that contains data

type Density

type Density struct {
	Style
	Label string

	Kernel     Length
	Normalized bool
	Data       []float64 // sorted
}

Density implements density plot using cubic-pulse kernel.

func NewDensity

func NewDensity(label string, values []float64) *Density

NewDensity creates a density plot from the given values.

func (*Density) Draw

func (line *Density) Draw(plot *Plot, canvas Canvas)

Draw draws the element to canvas.

func (*Density) Stats

func (line *Density) Stats() Stats

Stats calculates statistics of values.

type Element

type Element interface {
	Draw(plot *Plot, canvas Canvas)
}

Element is a drawable plot element.

type Elements

type Elements []Element

Elements defines a list of elements.

func (*Elements) Add

func (els *Elements) Add(el Element)

Add appends an element to the list.

func (*Elements) AddGroup

func (els *Elements) AddGroup(adds ...Element)

AddGroup appends elements as a single group to the list.

func (Elements) Draw

func (els Elements) Draw(plot *Plot, canvas Canvas)

Draw draws the elements drawn over each other.

func (Elements) Stats

func (els Elements) Stats() Stats

Stats calculates the stats from all elements.

type Gizmo

type Gizmo struct {
	Center Point
}

Gizmo implements drawing X and Y axis.

func NewGizmo

func NewGizmo() *Gizmo

NewGizmo creates a new gizmo element.

func (*Gizmo) Draw

func (gizmo *Gizmo) Draw(plot *Plot, canvas Canvas)

Draw draws the element to canvas.

type Grid

type Grid struct {
	GridTheme
}

Grid implements faceted background.

func NewGrid

func NewGrid() *Grid

NewGrid creates a new grid plot.

func (*Grid) Draw

func (grid *Grid) Draw(plot *Plot, canvas Canvas)

Draw draws the element to canvas.

type GridTheme

type GridTheme struct {
	Fill  color.Color
	Major color.Color
	Minor color.Color
}

GridTheme is a default style for grid.

func (*GridTheme) IsZero

func (theme *GridTheme) IsZero() bool

IsZero checks whether theme has been defined.

type HFlex

type HFlex struct {
	Margin Rect
	// contains filtered or unexported fields
}

HFlex implements horizontally stacked elements with non-equal sizes.

func NewHFlex

func NewHFlex() *HFlex

NewHFlex creates a horizontally flexing elements.

func (*HFlex) Add

func (stack *HFlex) Add(fixedSize float64, el Element)

Add adds an element with fixed size.

func (*HFlex) AddGroup

func (stack *HFlex) AddGroup(fixedSize float64, adds ...Element)

AddGroup adds a group of elements with fixed size.

func (*HFlex) Draw

func (stack *HFlex) Draw(plot *Plot, canvas Canvas)

Draw draws elements.

func (*HFlex) Stats

func (stack *HFlex) Stats() Stats

Stats calculates the stats from all elements.

type HStack

type HStack struct {
	Margin Rect
	Elements
}

HStack implements horizontally stacked elements.

func NewHStack

func NewHStack(els ...Element) *HStack

NewHStack creates a collection of elements that are horizontally stacked.

func (*HStack) Draw

func (stack *HStack) Draw(plot *Plot, canvas Canvas)

Draw draws elements horizontally stacked equally dividing space.

type Label

type Label struct {
	Placement Point
	Style
	Text string
}

Label describes a plot element that draws a text to relative position.

func NewXLabel

func NewXLabel(text string) *Label

NewXLabel creates a text label that is placed according to relative placement.

func (*Label) Draw

func (label *Label) Draw(plot *Plot, canvas Canvas)

Draw draws the element to canvas.

type Length

type Length = float64

Length defines canvas space size.

type Line

type Line struct {
	Style
	Label string

	Data []Point
}

Line implements a simple line plot.

func NewLine

func NewLine(label string, points []Point) *Line

NewLine creates a new line element from the given points.

func (*Line) Draw

func (line *Line) Draw(plot *Plot, canvas Canvas)

Draw draws the element to canvas.

func (*Line) Stats

func (line *Line) Stats() Stats

Stats calculates element statistics.

type Log1pTransform

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

Log1pTransform implements logarithmic transform.

func NewLog1pTransform

func NewLog1pTransform(base float64) *Log1pTransform

NewLog1pTransform implements a logarithmic axis transform.

func (*Log1pTransform) FromCanvas

func (tx *Log1pTransform) FromCanvas(axis *Axis, s Length, screenMin, screenMax Length) float64

FromCanvas converts canvas point to value point.

func (*Log1pTransform) ToCanvas

func (tx *Log1pTransform) ToCanvas(axis *Axis, v float64, screenMin, screenMax Length) Length

ToCanvas converts value to canvas space.

type ManualTicks

type ManualTicks []Tick

ManualTicks allows to manually place and label ticks.

func (ManualTicks) Ticks

func (ticks ManualTicks) Ticks(axis *Axis) []Tick

Ticks calculates ticks for specified axis.

type Margin

type Margin struct {
	Amount Rect
	Elements
}

Margin is a collection which is drawn with a margin.

func NewMargin

func NewMargin(amount Rect, els ...Element) *Margin

NewMargin creates a elements groups.

func (*Margin) Draw

func (margin *Margin) Draw(plot *Plot, canvas Canvas)

Draw draws the elements drawn over each other.

type OptimizedLine

type OptimizedLine struct {
	Style
	Label string

	Data []Point

	ThresholdPx float64
}

OptimizedLine implements a simple line plot.

func NewOptimizedLine

func NewOptimizedLine(label string, points []Point, thresholdPx float64) *OptimizedLine

NewOptimizedLine creates a new line element that tries to optimize drawing.

func (*OptimizedLine) Draw

func (line *OptimizedLine) Draw(plot *Plot, canvas Canvas)

Draw draws the element to canvas.

func (*OptimizedLine) Stats

func (line *OptimizedLine) Stats() Stats

Stats calculates element statistics.

type PercentileTransform

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

PercentilesTransform implements axis transform for percentiles X axis.

func NewPercentileTransform

func NewPercentileTransform(levels int) *PercentileTransform

NewPercentileTransform creates a transform for X axis with the specified max levels.

func (*PercentileTransform) FromCanvas

func (tx *PercentileTransform) FromCanvas(axis *Axis, s Length, screenMin, screenMax Length) float64

FromCanvas converts screen value s to value.

func (*PercentileTransform) ToCanvas

func (tx *PercentileTransform) ToCanvas(axis *Axis, v float64, screenMin, screenMax Length) Length

ToCanvas converts value v to canvas space.

type Percentiles

type Percentiles struct {
	Style
	Label string
	Data  []Point
}

Percentiles implements drawing percentile line.

func NewPercentiles

func NewPercentiles(label string, values []float64) *Percentiles

NewPercentiles creates percentiles from values.

func (*Percentiles) Draw

func (line *Percentiles) Draw(plot *Plot, canvas Canvas)

Draw draws the element to canvas.

func (*Percentiles) Stats

func (line *Percentiles) Stats() Stats

Stats calculates element statistics.

type Plot

type Plot struct {
	// X, Y are the axis information
	X, Y   *Axis
	Margin Rect
	Elements
	// DefaultStyle
	Theme
}

Plot defines a combination of elements that can be drawn to the canvas.

func New

func New() *Plot

New creates a new empty plot.

func (*Plot) Draw

func (plot *Plot) Draw(canvas Canvas)

Draw draws plot to the specified canvas, creating axes automatically when necessary.

type Point

type Point struct{ X, Y Length }

Point describes a canvas position or offset.

func P

func P(x, y Length) Point

P is a convenience func for creating a point.

func Points

func Points(x, y []float64) []Point

Points creates a slice of points by combining two slices. If x or y is nil, then it will enumerate them with indices.

func Ps

func Ps(cs ...Length) []Point

Ps creates points from pairs.

func (Point) Add

func (a Point) Add(b Point) Point

Add calculates a + b.

func (Point) Empty

func (a Point) Empty() bool

Empty returns whether the point is (0, 0)

func (Point) Max

func (a Point) Max(b Point) Point

Max returns the max coordinates of a and b.

func (Point) Min

func (a Point) Min(b Point) Point

Min returns the min coordinates of a and b.

func (Point) Neg

func (a Point) Neg() Point

Neg negates the point coordinates.

func (Point) Scale

func (a Point) Scale(v float64) Point

Scale scales the point by v.

func (Point) Sub

func (a Point) Sub(b Point) Point

Sub calculates a - b.

func (Point) XY

func (a Point) XY() (x, y Length)

XY returns x and y coordinates.

type Rect

type Rect struct{ Min, Max Point }

Rect defines a position.

func R

func R(x0, y0, x1, y1 Length) Rect

R is a convenience func for creating a rectangle.

func (Rect) Column

func (r Rect) Column(i, count int) Rect

Column returns a i-th column when the rect is split into count columns.

func (Rect) Empty

func (r Rect) Empty() bool

Empty returns whether the rectangle is empty.

func (Rect) Inset

func (r Rect) Inset(by Rect) Rect

Inset shrinks the rect by the given rect.

func (Rect) Offset

func (r Rect) Offset(by Point) Rect

Offset moves the given rectangle.

func (Rect) Points

func (r Rect) Points() []Point

Points returns corners of the rectangle.

func (Rect) Row

func (r Rect) Row(i, count int) Rect

Row returns a i-th row when the rect is split into count rows.

func (Rect) Shrink

func (r Rect) Shrink(radius Point) Rect

Shrink shrinks the rect by the sides.

func (Rect) Size

func (r Rect) Size() Point

Size returns the size of the rect.

func (Rect) UnitLocation

func (r Rect) UnitLocation(u Point) Point

UnitLocation converts relative position to rect bounds.

func (Rect) Zero

func (r Rect) Zero() Rect

Zero returns rect such that the top-left corner is at (0, 0)

type ScreenSpaceTransform

type ScreenSpaceTransform struct {
	Transform func(v float64) float64
	Inverse   func(v float64) float64
}

ScreenSpaceTransform transforms using a custom func.

func (*ScreenSpaceTransform) FromCanvas

func (tx *ScreenSpaceTransform) FromCanvas(axis *Axis, s Length, screenMin, screenMax Length) float64

FromCanvas converts canvas point to value point.

func (*ScreenSpaceTransform) ToCanvas

func (tx *ScreenSpaceTransform) ToCanvas(axis *Axis, v float64, screenMin, screenMax Length) Length

ToCanvas converts value to canvas space.

type Stats

type Stats struct {
	Min    Point
	Center Point
	Max    Point
}

Stats describes a data points.

func PointsStats

func PointsStats(points []Point) Stats

PointsStats calculates statistics of points.

type Style

type Style struct {
	Stroke color.Color
	Fill   color.Color
	Size   Length

	// line only
	Dash       []Length
	DashOffset []Length

	// text only
	Font     string
	Rotation float64
	Origin   Point // {-1..1, -1..1}

	// SVG
	Class string
}

Style represents a drawing style for an element.

func (*Style) IsZero

func (style *Style) IsZero() bool

IsZero checks whether style has been assigned.

type Textbox

type Textbox struct {
	Margin Rect
	Style
	Lines []string
}

Textbox implements drawing text on plots.

func NewTextbox

func NewTextbox(line ...string) *Textbox

NewTextbox creates a new text graphic with the specified lines.

func (*Textbox) Add

func (box *Textbox) Add(text string)

Add adds a line to the text box.

func (*Textbox) Draw

func (box *Textbox) Draw(plot *Plot, canvas Canvas)

Draw draws text to the canvas.

type Theme

type Theme struct {
	Line      Style
	Font      Style
	FontSmall Style
	Fill      Style
	Bar       Style

	Grid GridTheme
}

Theme is a collection of different default styles.

func NewTheme

func NewTheme() Theme

NewTheme creates a theme with default values.

type Tick

type Tick struct {
	Minor bool
	Label string
	Value float64
}

Tick represents a division on plot.

type TickLabels

type TickLabels struct {
	X *TickLabelsX
	Y *TickLabelsY
}

TickLabels implements drawing tick labels.

func NewTickLabels

func NewTickLabels() *TickLabels

NewTickLabels creates a new tick labelling element.

func (*TickLabels) Draw

func (labels *TickLabels) Draw(plot *Plot, canvas Canvas)

Draw draws tick labels to canvas using axes from plot.

type TickLabelsX

type TickLabelsX struct {
	Enabled bool
	// Side determines position of the labels. -1 = min, 0 = center, 1 = max
	Side  float64
	Style Style
}

TickLabelsX implements drawing tick labels.

func NewTickLabelsX

func NewTickLabelsX() *TickLabelsX

NewTickLabelsX creates a new tick labelling element for X axis.

func (*TickLabelsX) Draw

func (labels *TickLabelsX) Draw(plot *Plot, canvas Canvas)

Draw draws tick labels to canvas using axes from plot.

type TickLabelsY

type TickLabelsY struct {
	Enabled bool
	// Side determines position of the labels. -1 = min, 0 = center, 1 = max
	Side  float64
	Style Style
}

TickLabelsY implements drawing tick labels.

func NewTickLabelsY

func NewTickLabelsY() *TickLabelsY

NewTickLabelsY creates a new tick labelling element for X axis.

func (*TickLabelsY) Draw

func (labels *TickLabelsY) Draw(plot *Plot, canvas Canvas)

Draw draws tick labels to canvas using axes from plot.

type Ticks

type Ticks interface {
	Ticks(axis *Axis) []Tick
}

Ticks represents an approach to calculating tick position and label.

type VFlex

type VFlex struct {
	Margin Rect
	// contains filtered or unexported fields
}

VFlex implements horizontally stacked elements with non-equal sizes.

func NewVFlex

func NewVFlex() *VFlex

NewVFlex creates a vertically flexing elements.

func (*VFlex) Add

func (stack *VFlex) Add(fixedSize float64, el Element)

Add adds an element with fixed size.

func (*VFlex) AddGroup

func (stack *VFlex) AddGroup(fixedSize float64, adds ...Element)

AddGroup adds a group of elements with fixed size.

func (*VFlex) Draw

func (stack *VFlex) Draw(plot *Plot, canvas Canvas)

Draw draws elements.

func (*VFlex) Stats

func (stack *VFlex) Stats() Stats

Stats calculates the stats from all elements.

type VStack

type VStack struct {
	Margin Rect
	Elements
}

VStack implements vertically stacked elements.

func NewVStack

func NewVStack(els ...Element) *VStack

NewVStack creates a collection of elements that are vertically stacked.

func (*VStack) Draw

func (stack *VStack) Draw(plot *Plot, canvas Canvas)

Draw draws elements vertically stacked equally dividing space.

type Violin

type Violin struct {
	Style
	Label string

	Side       float64
	Kernel     Length
	Normalized bool
	Data       []float64 // sorted
}

Violin implements violin plot using cubic-pulse for kernel function.

func NewViolin

func NewViolin(label string, values []float64) *Violin

NewViolin creates a new violin element using the specified values.

func (*Violin) Draw

func (line *Violin) Draw(plot *Plot, canvas Canvas)

Draw draws the element to canvas.

func (*Violin) Stats

func (line *Violin) Stats() Stats

Stats calculates element statistics.

Directories

Path Synopsis
cmd
plotgio module

Jump to

Keyboard shortcuts

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