barchart

package
v0.0.0-...-49fd92a Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: MIT Imports: 5 Imported by: 0

README

barchart

Makes barcharts.

Usage

Pass [][2]int values to BarChartXYs:

data := [][2]int{
    {0, 1},
    {1, 3},
    {2, 4},
    {3, 6},
    {4, 8},
    // nil,
    // nil,
    {7, 15},
    {8, 10},
    {9, 7},
    {10, 5},
    {11, 3},
    {12, 2},
    {13, 1},
    {14, 0},
    {15, 20},
}

plot := BarChartXYs(data)

You can use the Fprint utility to create this Unicode graph (the graph looks better with fonts that draw unicode blocks properly):

if err := Fprint(os.Stdout, plot, Linear(19)); err != nil {
    panic(err)
}

Yields :

0   █ 1
1   ██▉ 3
2   ███▉ 4
3   █████▊ 6
4   ███████▋ 8
5   nil
6   nil
7   ██████████████▎ 15
8   █████████▋ 10
9   ██████▋ 7
10  ████▊ 5
11  ██▉ 3
12  ██ 2
13  █ 1
14  ▏ 0
15  ███████████████████▏ 20

If your console font is Monaco, one of the blocks look weird. Use Menlo. =)

Docs?

Godocs!

License

MIT license.

Documentation

Overview

Package barchart is a helper to quickly create barchart from pairs of integers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprint

func Fprint(w io.Writer, p BarChart, s ScaleFunc) error

Fprint plots p as a Unicode XY plot, using scale s. The results is something like:

0   █ 1
1   ██▉ 3
2   ███▉ 4
3   █████▊ 6
4   ███████▋ 8
5   nil
6   nil
7   ██████████████▎ 15
8   █████████▋ 10
9   ██████▋ 7
10  ████▊ 5
11  ██▉ 3
12  ██ 2
13  █ 1
14  ▏ 0
15  ███████████████████▏ 20

func Fprintf

func Fprintf(w io.Writer, p BarChart, width int, s ScaleFunc, x, y FormatFunc) error

Fprintf plots p as a Unicode XY plot of width, scaling Y values with s and rendering axis labels with x and y format funcs. As in the example, you can render pretty things like:

12:06:00.098  █████▋ 12MB
12:06:00.213  ████▋ 8.0MB
12:06:00.329  ██████▋ 10MB
12:06:00.444  ▏ 3.0MB
12:06:00.559  nil
12:06:00.675  nil
12:06:00.790  ██████████████▉ 22MB
12:06:00.906  ████████████▏ 16MB
12:06:01.021  ████▋ 8.0MB
12:06:01.136  ▏ 3.0MB
12:06:01.252  █████▋ 9.0MB
12:06:01.367  ███▊ 7.0MB
12:06:01.483  ███████████▏ 15MB
12:06:01.598  ████▋ 8.0MB

Types

type BarChart

type BarChart struct {
	MinX, MaxX, MinY, MaxY int
	// contains filtered or unexported fields
}

BarChart of XY points.

func BarChartXYs

func BarChartXYs(xys [][2]int) BarChart

BarChartXYs builds a BarChart using pairwise X and Y []float64.

Example
data := [][2]int{
	{0, 1},
	{1, 3},
	{2, 4},
	{3, 6},
	{4, 8},
	// nil,
	// nil,
	{7, 15},
	{8, 10},
	{9, 7},
	{10, 5},
	{11, 3},
	{12, 2},
	{13, 1},
	{14, 0},
	{15, 20},
}

plot := BarChartXYs(data)

if err := Fprint(os.Stdout, plot, Linear(19)); err != nil {
	panic(err)
}
Output:

0   █ 1
1   ██▉ 3
2   ███▉ 4
3   █████▊ 6
4   ███████▋ 8
5   nil
6   nil
7   ██████████████▎ 15
8   █████████▋ 10
9   ██████▋ 7
10  ████▊ 5
11  ██▉ 3
12  ██ 2
13  █ 1
14  ▏ 0
15  ███████████████████▏ 20
Example (Time)
base := time.Date(1984, 01, 01, 00, 00, 00, 00, time.UTC)
baseplus := func(i int) int {
	return int(base.Add(time.Duration(i) * time.Millisecond).UnixNano())
}
mb := 1000000

r := rand.New(rand.NewSource(42))

data := [][2]int{
	{baseplus(0), r.Intn(20) * mb},
	{baseplus(100), r.Intn(20) * mb},
	{baseplus(200), r.Intn(20) * mb},
	{baseplus(300), r.Intn(20) * mb},
	{baseplus(400), r.Intn(20) * mb},
	// nil,
	// nil,
	{baseplus(700), r.Intn(20) * mb},
	{baseplus(800), r.Intn(20) * mb},
	{baseplus(900), r.Intn(20) * mb},
	{baseplus(1000), r.Intn(20) * mb},
	{baseplus(1100), r.Intn(20) * mb},
	{baseplus(1200), r.Intn(20) * mb},
	{baseplus(1300), r.Intn(20) * mb},
	{baseplus(1400), r.Intn(20) * mb},
	{baseplus(1500), r.Intn(20) * mb},
}

xfmt := func(x float64) string {
	return time.Unix(base.Unix(), int64(x)).Format("15:04:05.000")
}
yfmt := func(y float64) string {
	return humanize.Bytes(uint64(y))
}

plot := BarChartXYs(data)

if err := Fprintf(os.Stdout, plot, len(data), Linear(13), xfmt, yfmt); err != nil {
	panic(err)
}
// 19:00:00.000  ██████▏ 12MB
// 19:00:00.115  ███▌ 8.0MB
// 19:00:00.230  ████▊ 10MB
// 19:00:00.346  ▏ 3.0MB
// 19:00:00.461  nil
// 19:00:00.576  nil
// 19:00:00.692  █████████████▏ 22MB
// 19:00:00.807  ████████▉ 16MB
// 19:00:00.923  ███▌ 8.0MB
// 19:00:01.038  ▏ 3.0MB
// 19:00:01.153  ████▏ 9.0MB
// 19:00:01.269  ██▊ 7.0MB
// 19:00:01.384  ████████▎ 15MB
// 19:00:01.500  ███▌ 8.0MB
Output:

func (*BarChart) Add

func (p *BarChart) Add(x, y int)

Add a XY point to the plot.

func (*BarChart) ScaleXYs

func (p *BarChart) ScaleXYs(xWidth int, s ScaleFunc) []XYf

ScaleXYs aggregates the XY values together in a dense form. Empty slots between two Xs are left nil to represent the absence of data. The values are scaled using s.

func (*BarChart) XYs

func (p *BarChart) XYs() []*XY

XYs aggregates the XY values together in a dense form. Empty slots between two Xs are left nil to represent the absence of data.

type FormatFunc

type FormatFunc func(v float64) string

FormatFunc formats a float into the proper string form. Used to print meaningful axe labels.

type ScaleFunc

type ScaleFunc func(min, max, value float64) float64

ScaleFunc is the type to implement to scale an histogram.

func Linear

func Linear(width int) ScaleFunc

Linear builds a ScaleFunc that will linearly scale the values of an histogram so that they do not exceed width.

type XY

type XY struct{ X, Y int }

XY point in a 2D plot.

type XYf

type XYf struct {
	X          float64
	Y, ScaledY *float64
}

XYf point in a 2D plot.

Jump to

Keyboard shortcuts

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