ggg

package module
v0.0.0-...-17c8c72 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: BSD-3-Clause Imports: 17 Imported by: 0

README

ggg

Data manipulation and plotting package for Go.

package main

import (
	"image/png"
	"log"
	"math"
	"os"

	. "github.com/mknyszek/ggg"
	_ "github.com/mknyszek/ggg/themes/dark"
)

var (
	colX = NewColumn[int]("x")
	colY = NewColumn[float64]("y")
	colS = NewColumn[string]("name")
)

func main() {
	// Create a simple dataset.
	d := Empty()
	d.AddColumn(colX)
	d.AddColumn(colY)
	d.AddColumn(colS)

	i := 1
	for row := range d.Grow(100) {
		colS.Set(d, row, "mackeral")
		colX.Set(d, row, i)
		colY.Set(d, row, math.Cos(float64(i)/100.0))
		i++
	}
	i = 1
	for row := range d.Grow(100) {
		colS.Set(d, row, "herring")
		colX.Set(d, row, i)
		colY.Set(d, row, math.Sin(float64(i)/100.0))
		i++
	}

	// Plot it.
	p := LinePlot(d, colX, colY, colS).Presentation(
		Title("My Chart"),
		XAxis("boxes", LogScale(10)),
		YAxis("tons of fish"),
	)

	// Render and write out the plot.
	im, err := p.Render("dark", 2160, 1440)
	if err != nil {
		log.Fatal(err)
	}
	file, err := os.Create("./out.png")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	if err := png.Encode(file, im); err != nil {
		log.Fatal(err)
	}
}

Goals

  • Flexible data model.
  • Type safety.
  • Grammer-of-graphics-style visualization.

Status

Experimental work-in-progress. Don't expect backwards-compatibility.

Possible future features

Data model:

  • Deleting rows.
  • Joins across datasets.
  • Pivoting.

Visualization:

  • 2D layers (areas, etc.).
  • Bar geom.
  • Legends.
  • Point shapes.
  • Faceting.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterTheme

func RegisterTheme(th *Theme) bool

Types

type AnyColumn

type AnyColumn interface {
	Name() string
	// contains filtered or unexported methods
}

AnyColumn is a way to refer to Column[T] for all T.

type AnyLayer

type AnyLayer interface {
	// contains filtered or unexported methods
}

type AxisOption

type AxisOption func(*axis)

func Limits

func Limits(min, max float64) AxisOption

func LogScale

func LogScale(base int) AxisOption

func Ticks

func Ticks(ticks ...float64) AxisOption

type Column

type Column[T any] struct {
	// contains filtered or unexported fields
}

Column represents a column of uniformly-typed values in a particular Dataset.

func ConvertFunc

func ConvertFunc[T, S any](d *Dataset, c Column[T], conv func(T) (S, error)) (Column[S], error)

ConvertFunc converts a column of type T to a column of type S in the dataset and returns the new column. Returns an error if any conv call returns an error.

func NewColumn

func NewColumn[T any](name string) Column[T]

NewColumn returns a new column with the provided name that may be used to access and mutate a dataset.

func (Column[T]) All

func (c Column[T]) All(d *Dataset) iter.Seq[T]

All returns an iterator over all values in the column in the dataset.

func (Column[T]) Delete

func (c Column[T]) Delete(d *Dataset)

Delete removes a column from a dataset.

func (Column[T]) Get

func (c Column[T]) Get(d *Dataset, row int) T

Get retrieves a value in the dataset at a particular row for this column.

func (Column[T]) In

func (c Column[T]) In(d *Dataset) bool

In returns true if the column exists in a dataset.

func (Column[T]) Name

func (c Column[T]) Name() string

Name returns the name of the column.

func (Column[T]) Set

func (c Column[T]) Set(d *Dataset, row int, value T)

Set sets a value in the dataset at a particular row for this column.

func (Column[T]) String

func (c Column[T]) String() string

String returns a debug string for the column.

func (Column[T]) Valid

func (c Column[T]) Valid() bool

Valid returns true if this is a valid column created with NewColumn.

type Dataset

type Dataset struct {
	// contains filtered or unexported fields
}

Dataset represents some data. The data are structured as a series of rows with uniformly-typed columns.

func Empty

func Empty() *Dataset

Empty returns an empty dataset.

func (*Dataset) AddColumn

func (d *Dataset) AddColumn(c AnyColumn) bool

AddColumn adds a new column to the dataset's structure. If the dataset already has rows, the column's data will be zero-initialized.

func (*Dataset) ColumnNames

func (d *Dataset) ColumnNames() iter.Seq[string]

ColumnNames returns an iterator over the names of columns in the dataset.

func (*Dataset) Columns

func (d *Dataset) Columns() int

Columns returns the number of columns in the dataset.

func (*Dataset) Filter

func (d *Dataset) Filter(f Filter) iter.Seq[int]

Filter returns an iterator over all rows that are accepted by the provided filter.

func (*Dataset) Grow

func (d *Dataset) Grow(n int) iter.Seq[int]

Grow adds new rows to the dataset and returns an iterator producing those new rows. Returns an iterator over the new row indices.

func (*Dataset) ParseFloat

func (d *Dataset) ParseFloat(c Column[string]) (Column[float64], error)

func (*Dataset) ParseInt

func (d *Dataset) ParseInt(c Column[string]) (Column[int64], error)

func (*Dataset) ParseUint

func (d *Dataset) ParseUint(c Column[string]) (Column[uint64], error)

func (*Dataset) Print

func (d *Dataset) Print(w io.Writer) error

Print dumps out a summary of the dataset in a nicely-formatted manner to w.

func (*Dataset) Rows

func (d *Dataset) Rows() int

Rows returns the number of rows in the dataset.

type Filter

type Filter interface {
	Accept(d *Dataset, row int) bool
}

func And

func And(f ...Filter) Filter

func EqualTo

func EqualTo[T comparable](c Column[T], value T) Filter

func FilterBy

func FilterBy[T any](c Column[T], f func(T) bool) Filter

func GreaterThan

func GreaterThan[T cmp.Ordered](c Column[T], value T) Filter

func GreaterThanOrEqualTo

func GreaterThanOrEqualTo[T cmp.Ordered](c Column[T], value T) Filter

func In

func In[T comparable](c Column[T], values ...T) Filter

func LessThan

func LessThan[T cmp.Ordered](c Column[T], value T) Filter

func LessThanOrEqualTo

func LessThanOrEqualTo[T cmp.Ordered](c Column[T], value T) Filter

func Matches

func Matches(c Column[string], r *regexp.Regexp) Filter

func Not

func Not(f Filter) Filter

func NotEqualTo

func NotEqualTo[T comparable](c Column[T], value T) Filter

func Or

func Or(f ...Filter) Filter

type Geom

type Geom struct {
	// contains filtered or unexported fields
}

func Line

func Line(color Mapping[color.Color], size Mapping[float64]) *Geom

func Point

func Point(color Mapping[color.Color], size Mapping[float64]) *Geom

func (*Geom) Dimensions

func (g *Geom) Dimensions() int

type Layer

type Layer[X, Y Scalar] struct {
	Data *Dataset
	X    Column[X]
	Y    Column[Y]
	Stat Statistic[Y]
	Geom *Geom
}

type Mapping

type Mapping[O comparable] struct {
	// contains filtered or unexported fields
}

func Constant

func Constant[O comparable](value O) Mapping[O]

func Identity

func Identity[T comparable](col Column[T]) Mapping[T]

func NiceColors

func NiceColors[I comparable](col Column[I]) Mapping[color.Color]

func PaletteColor

func PaletteColor(i uint64) Mapping[color.Color]

func Scale

func Scale[I, O comparable](col Column[I], f func(I) O) Mapping[O]

func ScaleLinear

func ScaleLinear[I, O Scalar](col Column[I], i0, i1 I, o0, o1 O) Mapping[O]

func ScaleOrdinal

func ScaleOrdinal[I, O comparable](col Column[I], i []I, o []O, alt O) Mapping[O]

type Plot

type Plot struct {
	// contains filtered or unexported fields
}

func LinePlot

func LinePlot[X, Y Scalar, S comparable](d *Dataset, x Column[X], y Column[Y], series Column[S]) *Plot

LinePlot is a helper to create a simple line plot where the values of the series column determine how to group the data.

func NewPlot

func NewPlot() *Plot

func (*Plot) Layer

func (p *Plot) Layer(l AnyLayer) *Plot

func (*Plot) Presentation

func (p *Plot) Presentation(opts ...PresentationOption) *Plot

func (*Plot) Render

func (p *Plot) Render(theme string, width, height int) (image.Image, error)

type PresentationOption

type PresentationOption func(*presentOpts)

func Title

func Title(title string) PresentationOption

func XAxis

func XAxis(title string, aOpts ...AxisOption) PresentationOption

func YAxis

func YAxis(title string, aOpts ...AxisOption) PresentationOption

type Scalar

type Scalar interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
}

type Statistic

type Statistic[T Scalar] struct {
	// contains filtered or unexported fields
}

func Confidence

func Confidence[T Scalar](confidence float64) Statistic[T]

func ConfidenceNormal

func ConfidenceNormal[T Scalar](confidence float64) Statistic[T]

func Count

func Count[T Scalar]() Statistic[T]

func Mean

func Mean[T Scalar]() Statistic[T]

func Sum

func Sum[T Scalar]() Statistic[T]

func (Statistic[T]) Apply

func (s Statistic[T]) Apply(values iter.Seq[T]) []float64

func (Statistic[T]) ApplyInto

func (s Statistic[T]) ApplyInto(values iter.Seq[T], result []float64)

func (Statistic[T]) Dimensions

func (s Statistic[T]) Dimensions() int

func (Statistic[T]) Valid

func (s Statistic[T]) Valid() bool

type Theme

type Theme struct {
	Name                  string
	ForegroundColor       color.Color
	GridlineColor         color.Color
	ChartBackgroundColor  color.Color
	BorderBackgroundColor color.Color
	SeriesPalette         func(uint64) color.Color
	TitleFont             *truetype.Font
	AxisFont              *truetype.Font
	AnnotationFont        *truetype.Font
}

Directories

Path Synopsis
themes
third_party

Jump to

Keyboard shortcuts

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