linechart

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: Apache-2.0 Imports: 18 Imported by: 38

Documentation

Overview

Package linechart contains a widget that displays line charts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValueFormatterRound added in v0.10.0

func ValueFormatterRound(value float64) string

ValueFormatterRound is a formatter that will receive a float64 value and will round to the nearest value without decimals.

func ValueFormatterSingleUnitSeconds added in v0.10.0

func ValueFormatterSingleUnitSeconds(seconds float64) string

ValueFormatterSingleUnitSeconds is a formatter that will receive seconds unit in the float64 argument and will return a pretty format in one single unit without decimals, it doesn't round, it truncates. Received seconds that are NaN will be ignored and return an empty string.

Types

type LineChart

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

LineChart draws line charts.

Each line chart has an identifying label and a set of values that are plotted.

The size of the two axes is determined from the values. The X axis will have a number of evenly distributed data points equal to the largest count of values among all the labeled line charts. The Y axis will be sized so that it can conveniently accommodate the largest value among all the labeled line charts. This determines the used scale.

LineChart supports mouse based zoom, zooming is achieved by either highlighting an area on the graph (left mouse clicking and dragging) or by using the mouse scroll button.

Implements widgetapi.Widget. This object is thread-safe.

func New

func New(opts ...Option) (*LineChart, error)

New returns a new line chart widget.

func (*LineChart) Draw

func (lc *LineChart) Draw(cvs *canvas.Canvas, meta *widgetapi.Meta) error

Draw draws the values as line charts. Implements widgetapi.Widget.Draw.

func (*LineChart) Keyboard

func (lc *LineChart) Keyboard(k *terminalapi.Keyboard, meta *widgetapi.EventMeta) error

Keyboard implements widgetapi.Widget.Keyboard.

func (*LineChart) Mouse

func (lc *LineChart) Mouse(m *terminalapi.Mouse, meta *widgetapi.EventMeta) error

Mouse implements widgetapi.Widget.Mouse.

func (*LineChart) Options

func (lc *LineChart) Options() widgetapi.Options

Options implements widgetapi.Widget.Options.

func (*LineChart) Series

func (lc *LineChart) Series(label string, values []float64, opts ...SeriesOption) error

Series sets the values that should be displayed as the line chart with the provided label. The values that should not be displayed on the line chart should be represented as math.NaN values on the values slice. Subsequent calls with the same label replace any previously provided values.

func (*LineChart) ValueCapacity added in v0.7.0

func (lc *LineChart) ValueCapacity() int

ValueCapacity returns the number of values that could be fit onto the X axis without a need to rescale the X axis. This is essentially the number of available pixels on the braille canvas based on the width of the LineChart as observed on the last call to draw. Returns zero if draw wasn't called.

Note that this capacity changes each time the terminal resizes, so there is no guarantee this remains the same next time Draw is called. Should be used as a hint only.

type Option

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

Option is used to provide options to New().

func AxesCellOpts

func AxesCellOpts(co ...cell.Option) Option

AxesCellOpts set the cell options for the X and Y axes.

func XAxisUnscaled added in v0.7.0

func XAxisUnscaled() Option

XAxisUnscaled when provided, stops the LineChart from rescaling the X axis when it can't fit all the values in the series, instead the LineCharts only displays the last n values that fit into its width. This is useful to create an impression of values rolling through the linechart right to left. Note that this results in hiding of values from the beginning of the series that didn't fit completely and might hide some shorter series as this effectively makes the X axis start at a non-zero value.

The default behavior is to rescale the X axis to display all the values. This option takes no effect if all the values on the series fit into the LineChart area.

func XLabelCellOpts

func XLabelCellOpts(co ...cell.Option) Option

XLabelCellOpts set the cell options for the labels on the X axis.

func XLabelsHorizontal added in v0.7.0

func XLabelsHorizontal() Option

XLabelsHorizontal makes the labels under the X axis flow horizontally. This is the default option.

func XLabelsVertical added in v0.7.0

func XLabelsVertical() Option

XLabelsVertical makes the labels under the X axis flow vertically. Defaults to labels that flow horizontally.

func YAxisAdaptive added in v0.6.0

func YAxisAdaptive() Option

YAxisAdaptive makes the Y axis adapt its base value depending on the provided series. Without this option, the Y axis always starts at the zero value regardless of values available in the series. When this option is specified and the series don't contain value zero, the Y axis will be adapted to the minimum value for all-positive series or the maximum value for all-negative series. The Y axis still starts at the zero value if the series contain both positive and negative values.

func YAxisCustomScale added in v0.7.0

func YAxisCustomScale(min, max float64) Option

YAxisCustomScale when provided, the scale of the Y axis will be based on the specified minimum and maximum value instead of determining those from the LineChart series. Useful to visually stabilize the Y axis for LineChart applications that continuously feed values. The default behavior is to continuously determine the minimum and maximum value from the series before drawing the LineChart. Even when this option is provided, the LineChart would still rescale the Y axis if a value is encountered that is outside of the range specified here, i.e. smaller than the minimum or larger than the maximum. Both the minimum and the maximum must be valid numbers and the minimum must be smaller than the maximum.

Providing this option also sets YAxisAdaptive.

func YAxisFormattedValues added in v0.10.0

func YAxisFormattedValues(vfmt ValueFormatter) Option

YAxisFormattedValues sets a value formatter for the Y axis values. If a formatter is set, it will format the values with the desired ValueFormatter and will use the retuning string from the formatter instead of the numeric value to represent this value on the Y axis.

func YLabelCellOpts

func YLabelCellOpts(co ...cell.Option) Option

YLabelCellOpts set the cell options for the labels on the Y axis.

func ZoomHightlightColor added in v0.7.0

func ZoomHightlightColor(c cell.Color) Option

ZoomHightlightColor sets the background color of the area that is selected with mouse in order to zoom the linechart. Defaults to color number 235.

func ZoomStepPercent added in v0.7.0

func ZoomStepPercent(perc int) Option

ZoomStepPercent sets the zooming step on each mouse scroll event as the percentage of the size of the X axis. The value must be in range 0 < value <= 100. Defaults to zoom.DefaultScrollStep.

type SeriesOption

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

SeriesOption is used to provide options to Series.

func SeriesCellOpts

func SeriesCellOpts(co ...cell.Option) SeriesOption

SeriesCellOpts sets the cell options for this series. Note that the braille canvas has resolution of 2x4 pixels per cell, but each cell can only have one set of cell options set. Meaning that where series share a cell, the last drawn series sets the cell options. Series are drawn in alphabetical order based on their name.

func SeriesXLabels

func SeriesXLabels(labels map[int]string) SeriesOption

SeriesXLabels is used to provide custom labels for the X axis. The argument maps the positions in the provided series to the desired label. The labels are only used if they fit under the axis. Custom labels are property of the line chart, since there is only one X axis, providing multiple custom labels overwrites the previous value.

type ValueFormatter added in v0.10.0

type ValueFormatter func(value float64) string

ValueFormatter will be used to format values onto string based representation. The received float64 value could be a math.NaN value.

func ValueFormatterRoundWithSuffix added in v0.10.0

func ValueFormatterRoundWithSuffix(suffix string) ValueFormatter

ValueFormatterRoundWithSuffix is a factory that returns a formatter that will receive a float64 value and will round to the nearest value without decimals adding a suffix to the final value string representation.

func ValueFormatterSingleUnitDuration added in v0.10.0

func ValueFormatterSingleUnitDuration(unit time.Duration, decimals int) ValueFormatter

ValueFormatterSingleUnitDuration is a factory to create a custom duration in a single unit representation formatter based on a unit and the decimals to truncate. If the received decimal value is negative it will fallback to a 0 decimal value. The result value formatter handles NaN values, if the value formatter receives a NaN float64 it will return an empty string.

func ValueFormatterSuffix added in v0.10.0

func ValueFormatterSuffix(decimals int, suffix string) ValueFormatter

ValueFormatterSuffix is a factory that returns a formatter that will receive a float64 value and return a string representation with the desired number of decimal truncated and a suffix.

Directories

Path Synopsis
internal
axes
Package axes calculates the required layout and draws the X and Y axes of a line chart.
Package axes calculates the required layout and draws the X and Y axes of a line chart.
zoom
Package zoom contains code that tracks the current zoom level.
Package zoom contains code that tracks the current zoom level.
Binary linechartdemo displays a linechart widget.
Binary linechartdemo displays a linechart widget.

Jump to

Keyboard shortcuts

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