chart

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package chart draws a sheet's charts: as text for any terminal (block elements for bars, braille for lines, half blocks for pies), and the plot area as an image for terminals with kitty graphics. Both share one layout, so axis labels, category labels and the legend stay terminal text around the image, in the terminal's own font.

Index

Constants

View Source
const (
	SVGCellW = 9
	SVGCellH = 21
)

SVG charts are the text chart and the image in one vector drawing, for the HTML export: the axes, labels and legend Draw lays out, as text on a grid of SVGCellW by SVGCellH pixels, and the plot area as the shapes Image paints, as SVG shapes rather than pixels. Colors are classes the page styles, so a chart follows the page's light or dark palette:

ct-ax, ct-lb, ct-mu  text of the axes, the labels and messages
ct-s0 .. ct-s5       text in a series' color
cf-0 .. cf-5         shapes filled in a series' color
cs-0 .. cs-5         lines stroked in a series' color
cs-gl                gridlines
cs-box               axes, in their text class's color
cs-gap               the gaps between a pie's slices
View Source
const Colors = 6

Colors is how many series colors there are; later series reuse them.

View Source
const QueryID = 31

Query is the probe for kitty graphics support: terminals that have it answer with an OK for image QueryID; others stay silent.

Variables

This section is empty.

Functions

func Delete

func Delete(id int, wrap func(string) string) string

Delete returns the sequence that frees image id and its placements.

func Image

func Image(t sheet.ChartType, d sheet.ChartData, w, h int, o Options, pal Palette) *image.RGBA

Image draws the plot area of the chart Draw lays out with the same arguments, cw by ch pixels per cell, on a transparent background. It returns nil when there's nothing to plot.

func Placeholder

func Placeholder(row, col int) string

Placeholder returns the text of the cell at row, col of an image's placement. The foreground color must name the image: the 256-color index of its id.

func Query

func Query() string

Query returns the probe.

func SVG added in v0.4.0

func SVG(t sheet.ChartType, d sheet.ChartData, w, h int, o Options, title string) string

SVG draws d as a chart of type t in w by h cells, as an svg element of w*SVGCellW by h*SVGCellH pixels whose title is title.

func Sixel added in v0.3.0

func Sixel(img *image.RGBA, bg color.RGBA, registers int) string

Sixel returns the DCS sequence that draws img at the cursor, every pixel composited over bg, in at most registers colors (at least 2, at most 256).

func Transmit

func Transmit(id int, img *image.RGBA, cols, rows int, wrap func(string) string) string

Transmit returns the sequences that send img as image id, with a virtual placement of cols by rows cells for placeholders to show. Sending an id again replaces the image. wrap, if not nil, wraps each chunk, e.g. for tmux passthrough.

Types

type Cell

type Cell struct {
	Text string // one grapheme; "" continues a wide one to the left
	Fg   Role
	Bg   Role // only the pie's half blocks set a background
}

Cell is one terminal cell of a drawn chart.

type Grid

type Grid struct {
	W, H  int
	Cells []Cell
	// Plot is the area an image covers, in cells. It is empty when the
	// chart has nothing to plot.
	Plot image.Rectangle
}

Grid is a drawn chart, W by H cells.

func Draw

func Draw(t sheet.ChartType, d sheet.ChartData, w, h int, o Options) *Grid

Draw draws d as a chart of type t in w by h cells.

func (*Grid) At

func (g *Grid) At(x, y int) *Cell

At returns the cell at x, y, or nil outside the grid.

func (*Grid) String

func (g *Grid) String() string

String returns the grid as plain text, for tests.

type Options

type Options struct {
	// Image leaves the plot area blank, for an image drawn by Image.
	Image bool
	// CellW and CellH are the size of a terminal cell in pixels; they set
	// a pie's proportions. Zero means the usual 1:2.
	CellW, CellH int
	// Chart is the chart's own settings: stacking, the value axis, the
	// gridlines and the legend.
	Chart sheet.ChartOptions
}

Options control drawing.

type Palette

type Palette struct {
	Series [Colors]color.RGBA
	Grid   color.RGBA
}

Palette holds the colors an image draws with: the terminal's colors for the series roles, so the image matches the text legend, and a faint color for gridlines.

type Role

type Role uint8

Role is what a cell of a drawn chart shows, so the UI can style it with its theme.

const (
	None     Role = iota // background
	Axis                 // axis lines and tick marks
	Label                // tick values, category labels and legend text
	Muted                // messages such as "No numbers to chart"
	Gridline             // gridlines
	Series               // Series+i is the color of series (or slice) i
)

func SeriesRole

func SeriesRole(i int) Role

SeriesRole is the role of series i.

Jump to

Keyboard shortcuts

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