chart

package module
v0.0.0-...-fd21f57 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2019 License: MIT Imports: 10 Imported by: 2

README

Build Status Coverage Status Go Report Card GoDoc

Go Chart Lib

Dead simple rrd like bandwidth charts with focus on pixel perfect rendering of source data.

Written in Go, the output can either be an interactive SVG chart or a static PNG image.

It was written to be able to show tens or hundreds of charts in seconds without interactivity in mind.

The SVG image allows basic analytics to be performed on the chart, like measurements of time or volume, showing/hiding datasets and showing a weighted moving average on demand.

Source data can be upsampled using a simple stretch method (bar charts) or downsampled using the largest triangle three buckets algorithm.

The javascript embedded in the SVG image does not have any dependencies.

Examples

Example screenshot:

View as interactive SVG

Example Screenshot

Screenshots from the example website:

Basics from example website

Example Usage

Example demo app:

go get github.com/tomarus/chart 
go run $GOPATH/src/github.com/tomarus/chart/examples/main.go
open http://localhost:3000

Example /proc monitoring app (linux only):

go get github.com/tomarus/chart
go run $GOPATH/src/github.com/tomarus/chart/examples/sysmon/main.go
open http://localhost:3001

Code example:

import (
    "github.com/tomarus/chart"
    "github.com/tomarus/chart/svg"
)
opts := &chart.Options{
    Title:  "Traffic",
    Image:  svg.New(), // or png.New()
    Size:   "auto",    // big is 1440px, small is 720px, auto is size of dataset
    Height: 300,       // Defaults to -1, when size=auto height=width/4, otherwise set fixed height
    Scheme: "white",   // or black/random/pink/solarized or hsl:180,0.5,0.25
    Start:  start_epoch,
    End:    end_epoch,
    W:      w,
    SIBase: 1000, // or use 1024 to scale, only used when axes are not specified.
    // If you don't specify axes, they will be automatically calculated using some defaults.
    Axes: []*axis.Axis{
        axis.NewTime(axis.Bottom, "Mon 15:04").Duration(8 * time.Hour).Grid(4),
        axis.NewSI(axis.Left, 1000).Ticks(4).Grid(2),
    },
}
c, err := chart.NewChart(opts)
if err != nil {
    panic(err)
}
err = c.AddData(&data.Options{Title: "My Data Description"}, []yourData)
if err != nil {
    panic(err)
}
w.Header().Set("Content-Type", "image/svg+xml")
err = c.Render()
if err != nil {
    panic(err)
}

Notes

This is an experimental work in progress for my own personal educational and research purposes.

This project has just started and a lot of stuf is still missing or incomplete. The API will not be stable until 1.0.0 is tagged in git.

This is a small list of ideas, todos and limitations:

  • Custom lines and markers, like 95th percentile line, downtime markers, etc
  • Add support negative values
  • It supports only area charts atm
  • Only 4 sources per chart supported currently

Documentation

Overview

Package chart generates interactive svg or png charts from time series data.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chart

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

Chart is the main chart type used for all operations.

Example
opts := &Options{
	Title:  "Title on top of the chart",
	Image:  svg.New(), // or png.New()
	Size:   "big",     // big is 1440px, small is 720px, auto is size of dataset
	Height: 300,       // Defaults to -1, when size=auto height is set to width/4, otherwise set fixed height
	Width:  900,       // If a width is supplied, height is implied and both are used in stead of size setting
	Scheme: "white",   // or black/random/pink/solarized or hsl:180,0.5,0.25
	Theme:  "light",   // default is dark.
	Start:  time.Now().AddDate(0, 0, -1).Unix(),
	End:    time.Now().Unix(),
	W:      os.Stdout,
	Axes: []*axis.Axis{
		axis.NewTime(axis.Bottom, "01-02 15:04").Duration(4 * time.Hour).Grid(4),
		axis.NewSI(axis.Left, 1000).Ticks(4).Grid(2),
		//
		// - Example custom time format
		// axis.New(axis.Bottom, func(in float64) string {
		//	return time.Unix(int64(in), 0).Format("01-02 15:04")
		// 	return in.(time.Time).Format("2006-01-02")
		// }).Duration(4 * time.Hour).Grid(),
		//
		// - Example other custom format
		// axis.New(axis.Left, func(in float64) string {
		// 	return fmt.Sprintf("%.1f", in/3.14)
		// }).Ticks(5).Grid(),
	},
}

c, err := NewChart(opts)
if err != nil {
	panic(err)
}

exdata := make([]float64, 256)
for i := 0; i < 255; i++ {
	exdata[i] = float64(i)
}
warn := c.AddData(&data.Options{Type: "area", Title: "My Data Description"}, exdata)
if err != nil {
	fmt.Println(warn)
}
c.Render()
Output:

func NewChart

func NewChart(o *Options) (*Chart, error)

NewChart initializes a new svg chart.

func (*Chart) AddData

func (c *Chart) AddData(opt *data.Options, d []float64) (err error)

AddData adds a single data set.

func (*Chart) Render

func (c *Chart) Render() error

Render renders the final image to the io.Writer.

type Options

type Options struct {
	Title         string      // guess what, leave empty to hide
	Size          string      // big is 1440px, small is 720px, auto is size of dataset
	Width, Height int         // overrides Size
	Scheme        string      // palette colorscheme, default "white"
	Theme         string      // if random scheme is used, set to "light" to use light colors, otherwise a dark theme is generated
	Start, End    int64       // start + end epoch of data
	Image         image.Image // the chart image type, chart.SVG{} or chart.PNG{}
	W             io.Writer   // output writer to write image to
	SIBase        int         // SI Base for auto axis calculation, default is 1000.
	Axes          []*axis.Axis
}

Options defines a type used to initialize a Chart using NewChart()

Directories

Path Synopsis
sysmon
Command sysmon is a basic /proc system monitoring tool storing only the last 15 minutes of data, sampled each second, in memory.
Command sysmon is a basic /proc system monitoring tool storing only the last 15 minutes of data, sampled each second, in memory.
Package svg provides the svg interface for tomarus chart lib.
Package svg provides the svg interface for tomarus chart lib.

Jump to

Keyboard shortcuts

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