printer

package
v0.0.0-...-0e64b7e Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: MIT Imports: 7 Imported by: 0

README

printer

Go Reference

An ascii histogram generator for histogram/go

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bar

func Bar(length int) string

Bar generates the actual .Bar with the max length provided

func Count

func Count(format string) string

Count generates a .Count value using the supplied Printf format

func CountIf

func CountIf(pos string, neg string) string

Pct generates a .Pct value using the `pos` Printf format if the value is not zero, `pos` on a zero value. useful for hiding zero values

func FloatFormat

func FloatFormat(length int) []string

FloatFormat generates a tabulated row with the bar length provided when the value is a float

func Fprint

func Fprint[T histogram.Indexable](w io.Writer, h histogram.Histogram[T], formats ...string) error

Fprint writes a histogram to an io.Writer using the format strings provided The format strings use the text/template syntax and write to a text/tabwriter multiple format strings are joined in order using \t to create aligned columns and each line automatically gets a \n, so no need to add an EOL. Values passed to the template:

.Total : the total number of records in the histogram (int)
.Pct   : percentage represesented by a bucket value relative to .Total (float64)
.Min   : lower bound of the bucket, inclusive
.Max   : higher bound of the bucket, exclusive except on the final bucket
.Count : count of values in the bucket

[formatting.go](.formatting.go) contains some helper functions for common formats. If no formats are passed, it uses `IntFormat(5)` by default.

Example
data := []float64{
	0.1,
	0.2, 0.21, 0.22, 0.22,
	0.4,
	0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59,
	0.6,
	// 0.7 is empty
	// 0.8,
	0.9,
	1.0,
	0.3, // intenionally out of order
	// 1000,
}
hist := histogram.Create(9, data)
Fprint(os.Stdout, hist, FloatFormat(50)...)
Output:

0.1-0.2  5%   █████                                                  1
0.2-0.3  25%  █████████████████████████                              5
0.3-0.4  0%   ▏                                                      -
0.4-0.5  5%   █████                                                  1
0.5-0.6  50%  ██████████████████████████████████████████████████    10
0.6-0.7  5%   █████                                                  1
0.7-0.8  0%   ▏                                                      -
0.8-0.9  0%   ▏                                                      -
0.9-1    10%  ██████████                                             2

func IntFormat

func IntFormat(length int) []string

IntFormat generates a tabulated row with the bar length provided when the value is an int

func Pct

func Pct(format string) string

Pct generates a .Pct value using the supplied Printf format

func Range

func Range(format string) string

Range generates a .Min and .Max values using the supplied Printf format

func StringFormat

func StringFormat(length int) []string

StringFormat generates a tabulated row with the bar length provided when the value implements fmt.Stringer (time.Duration for example)

Types

This section is empty.

Jump to

Keyboard shortcuts

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