barchart

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: MIT Imports: 7 Imported by: 14

Documentation

Overview

Package barchart implements a canvas that displays a bar chart with bars going either horizontally or vertically.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BarData

type BarData struct {
	Label  string
	Values []BarValue
}

BarData contains a label for the bar and a list of BarValues. If displaying vertical bars, the length of the label string will be bounded by the width of each bar. If displaying horizontal bars, the length of the label string will not be bounded and will affect where the vertical axis line starts.

type BarValue

type BarValue struct {
	Name  string
	Value float64
	Style lipgloss.Style
}

BarValue contain bar segment name, value and style for drawing.

type Model

type Model struct {
	Canvas       canvas.Model
	AxisStyle    lipgloss.Style // style applied when drawing axis
	LabelStyle   lipgloss.Style // style applied when drawing axis labels
	AutoMaxValue bool           // whether to automatically set max value when adding data
	AutoBarWidth bool           // whether to automatically set bar width
	// contains filtered or unexported fields
}

Model contains state of a barchart

func New

func New(w, h int, opts ...Option) Model

New returns a barchart Model initialized with given width, height and various options. By default, barchart will automatically scale bar to new maximum data values, bar width to fill up the canvas and have one gap between bars, and display bars vertically. If the given data values are too small compared to the max value, then it is possible that the rendering of the bars will not be accurate in terms of proportions due to the limitations of the block element runes.

func (*Model) BarDataFromPoint

func (m *Model) BarDataFromPoint(p canvas.Point) (r BarData)

BarDataFromPoint returns a possible BarData containing all BarValues drawn for the rune on the barchart canvas at the given Point.

func (*Model) BarGap

func (m *Model) BarGap() int

BarGap returns number of empty spaces between bars.

func (*Model) BarWidth

func (m *Model) BarWidth() int

BarWidth returns the bar width drawn for each bar.

func (*Model) Clear

func (m *Model) Clear()

Clear will reset barchart canvas and data.

func (*Model) Draw

func (m *Model) Draw()

Draw will display the the scaled data values as bars on to the barchart canvas. The order of the bars will be displayed from left to right, or from top to bottom in the same order as the data was inserted.

func (*Model) Height

func (m *Model) Height() int

Height returns barchart height.

func (*Model) Horizontal

func (m *Model) Horizontal() bool

Horizontal returns whether displaying bars horizontally.

func (Model) Init

func (m Model) Init() tea.Cmd

func (*Model) MaxValue

func (m *Model) MaxValue() float64

MaxValue returns expected maximum data value.

func (*Model) Push

func (m *Model) Push(lv BarData)

Push adds given BarData to barchart data set. Negative values will be treated as the value 0. Data will be scaled using expected max value and barchart size.

func (*Model) PushAll

func (m *Model) PushAll(lv []BarData)

PushAll adds all data values in []BarData to barchart data set. Negative values will be treated as the value 0. Data will be scaled using expected max value and barchart size.

func (*Model) Resize

func (m *Model) Resize(w, h int)

Resize will change barchart display width and height. Existing data values will be updated to new scaling.

func (*Model) Scale

func (m *Model) Scale() float64

Scale returns data scaling factor.

func (*Model) SetBarGap

func (m *Model) SetBarGap(g int)

SetBarGap will set the number of empty spaces between each bar.

func (*Model) SetBarWidth

func (m *Model) SetBarWidth(w int)

SetBarWidth will set the bar width drawn of each bar. If AutoBarWidth is enabled, then this method will do nothing.

func (*Model) SetHorizontal

func (m *Model) SetHorizontal(b bool)

SetHorizontal will either enable or disable drawing bars horizontally on to the barchart canvas.

func (*Model) SetMax

func (m *Model) SetMax(f float64)

SetMax will update the expected maximum values and scale factor. Existing values will be updated to new scaling.

func (*Model) SetShowAxis

func (m *Model) SetShowAxis(b bool)

SetShowAxis will either enable or disable drawing axis and labels on to the barchart canvas.

func (*Model) SetZoneManager

func (m *Model) SetZoneManager(zm *zone.Manager)

SetZoneManager enables mouse functionality by setting a bubblezone.Manager to the linechart. To disable mouse functionality after enabling, call SetZoneManager on nil.

func (*Model) ShowAxis

func (m *Model) ShowAxis() bool

ShowAxis returns whether drawing axis and labels on to the barchart canvas.

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update forwards bubbletea Msg to underlying canvas.

func (Model) View

func (m Model) View() (r string)

View returns a string used by the bubbletea framework to display the barchart.

func (*Model) Width

func (m *Model) Width() int

Width returns barchart width.

func (*Model) ZoneID

func (m *Model) ZoneID() string

ZoneID will return linechart zone ID used by zone Manager.

func (*Model) ZoneManager

func (m *Model) ZoneManager() *zone.Manager

ZoneManager will return linechart zone Manager.

type Option

type Option func(*Model)

Option is used to set options when initializing a barchart. Example:

bc := New(width, height, WithMaxValue(someValue))

func WithBarGap

func WithBarGap(g int) Option

WithBarGap sets the empty spaces between bars when drawing.

func WithBarWidth

func WithBarWidth(w int) Option

WithBarWidth sets the bar widths when when drawing.

func WithDataSet

func WithDataSet(d []BarData) Option

WithDataSet adds all bar data values in []BarData to barchart data set.

func WithHorizontalBars

func WithHorizontalBars() Option

WithHorizontalBars displays bars horizontally from right to left, with each bar displayed from top to bottom.

func WithMaxValue

func WithMaxValue(f float64) Option

WithMaxValue sets the expected maximum data value to given float64.

func WithNoAutoBarWidth

func WithNoAutoBarWidth() Option

WithNoAutoBarWidth disables automatically setting the bar widths to fill the canvas when drawing.

func WithNoAutoMaxValue

func WithNoAutoMaxValue() Option

WithNoAutoMaxValue disables automatically setting the max value if new data greater than the current max is added.

func WithNoAxis

func WithNoAxis() Option

WithNoAxis disables drawing axis and labels on to the barchart.

func WithStyles

func WithStyles(as lipgloss.Style, ls lipgloss.Style) Option

WithStyles sets the axis line and label string styles.

func WithZoneManager

func WithZoneManager(zm *zone.Manager) Option

WithZoneManager sets the bubblezone Manager used when processing bubbletea Msg mouse events in Update().

Jump to

Keyboard shortcuts

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