Skip to content

BarChart

BarChartHorizontal (default) draws bars left-to-right with labels on the left. BarChartVertical draws bars bottom-to-top with labels along the bottom. Choose based on label length — horizontal works better for long labels.

By default bars use the theme accent color. Override per-bar by setting BarChartData.Color to a tcell.Color.

BarChart preview

BarChart renders read-only horizontal or vertical bar charts. Update data with SetItems; the component redraws on the next tview draw cycle.

Constructor func NewBarChart() *BarChart

Usage

chart := components.NewBarChart().
SetTitle("Requests by region").
SetOrientation(components.BarVertical).
SetItems(
components.BarItem{Label: "us-east", Value: 1420, Color: tcell.ColorGreen},
components.BarItem{Label: "eu-west", Value: 890},
components.BarItem{Label: "ap-south", Value: 320},
).
SetShowValues(true).
SetValueFormat("%.0f")
fmt.Println(chart != nil)

Methods

Method Signature Description
AddItem func (c *BarChart) AddItem(item BarItem) *BarChart AddItem appends a bar item
Clear func (c *BarChart) Clear() *BarChart Clear removes all items
GetFieldHeight func (c *BarChart) GetFieldHeight() int GetFieldHeight returns preferred height
SetAutoScale func (c *BarChart) SetAutoScale(enabled bool) *BarChart SetAutoScale enables/disables automatic range
SetBarGap func (c *BarChart) SetBarGap(gap int) *BarChart SetBarGap sets gap between bars
SetBarWidth func (c *BarChart) SetBarWidth(width int) *BarChart SetBarWidth sets bar width (vertical orientation)
SetChars func (c *BarChart) SetChars(filled, empty rune) *BarChart SetChars sets filled and empty bar characters
SetItems func (c *BarChart) SetItems(items ...BarItem) *BarChart SetItems sets all bar items
SetOnSelect func (c *BarChart) SetOnSelect(fn func(index int, item BarItem)) *BarChart SetOnSelect sets callback for bar selection
SetOrientation func (c *BarChart) SetOrientation(o BarOrientation) *BarChart SetOrientation sets horizontal or vertical bars
SetRange func (c *BarChart) SetRange(min, max float64) *BarChart SetRange sets fixed value range (disables auto-scale)
SetShowLabels func (c *BarChart) SetShowLabels(show bool) *BarChart SetShowLabels shows/hides bar labels
SetShowValues func (c *BarChart) SetShowValues(show bool) *BarChart SetShowValues shows/hides values on bars
SetTitle func (c *BarChart) SetTitle(title string) *BarChart SetTitle sets the chart title
SetValueFormat func (c *BarChart) SetValueFormat(format string) *BarChart SetValueFormat sets printf format for values
SetValues func (c *BarChart) SetValues(values []float64, labels []string) *BarChart SetValues sets values with auto-generated labels

Types

type BarItem struct BarItem is a single bar entry. Color 0 falls back to the theme accent color.
FieldTypeDescription
Label string
Value float64
Color tcell.Color 0 = use theme accent
type BarOrientation BarOrientation controls whether bars are drawn horizontally (left-to-right) or vertically (bottom-to-top). Horizontal works better for long labels.
ConstantDescription
BarHorizontal
BarVertical