Categorygithub.com/nanitor/go-chart
modulepackage
1.1.0
Repository: https://github.com/nanitor/go-chart.git
Documentation: pkg.go.dev

# README

go-chart

Build Status

Package chart is a very simple golang native charting library that supports timeseries and continuous line charts.

The v1.0 release has been tagged so things should be more or less stable, if something changes please log an issue.

Installation

To install chart run the following:

> go get -u github.com/wcharczuk/go-chart

Most of the components are interchangeable so feel free to crib whatever you want.

Output Examples

Spark Lines:

Single axis:

Two axis:

Simple Moving Average:

Bollinger Bounds:

Code Examples

Actual chart configurations and examples can be found in the ./examples/ directory. They are web servers, so start them with go run main.go then access http://localhost:8080 to see the output.

Usage

Everything starts with the chart.Chart object. The bare minimum to draw a chart would be the following:


import (
    ...
    "bytes"
    ...
    "github.com/wcharczuk/go-chart" //exposes "chart"
)

graph := chart.Chart{
    Series: []chart.Series{
        chart.ContinuousSeries{
            XValues: []float64{1.0, 2.0, 3.0, 4.0},
            YValues: []float64{1.0, 2.0, 3.0, 4.0},
        },
    },
}

buffer := bytes.NewBuffer([]byte{})
err := graph.Render(chart.PNG, buffer)

Explanation of the above: A chart can have many Series, a Series is a collection of things that need to be drawn according to the X range and the Y range(s).

Here, we have a single series with x range values as float64s, rendered to a PNG. Note; we can pass any type of io.Writer into Render(...), meaning that we can render the chart to a file or a resonse or anything else that implements io.Writer.

API Overview

Everything on the chart.Chart object has defaults that can be overriden. Whenever a developer sets a property on the chart object, it is to be assumed that value will be used instead of the default. One complication here is any object's root chart.Style object (i.e named Style) and the Show property specifically, if any other property is set and the Show property is unset, it is assumed to be it's default value of False.

The best way to see the api in action is to look at the examples in the ./examples/ directory.

Design Philosophy

I wanted to make a charting library that used only native golang, that could be stood up on a server (i.e. it had built in fonts).

The goal with the API itself is to have the "zero value be useful", and to require the user to not code more than they absolutely needed.

Contributions

This library is super early but contributions are welcome.

# Packages

No description provided by the author
No description provided by the author

# Functions

AbsInt returns the absolute value of an integer.
CreateLegend returns a legend renderable function.
DrawAnnotation draws an anotation with a renderer.
DrawBoundedSeries draws a series that implements BoundedValueProvider.
DrawBox draws a box with a given style.
DrawHistogramSeries draws a value provider as boxes from 0.
DrawLineSeries draws a line series with a renderer.
DrawText draws text with a given style.
DrawTextCentered draws text with a given style centered.
FloatValueFormatter is a ValueFormatter for float64.
FloatValueFormatterWithFormat is a ValueFormatter for float64 with a given format.
GenerateGridLines generates grid lines.
GenerateTicksWithStep generates a set of ticks.
GetDefaultFont returns the default font (Roboto-Medium).
GetDefaultSeriesStrokeColor returns a color from the default list by index.
GetRoundToForDelta returns a `roundTo` value for a given delta.
MaxInt returns the maximum of a set of integers.
MeasureAnnotation measures how big an annotation would be.
MinAndMax returns both the min and max in one pass.
MinAndMaxOfTime returns the min and max of a given set of times in one pass.
MinInt returns the minimum of a set of integers.
NewRingBuffer creates a new, empty, RingBuffer.
NewRingBufferFromSlice createsa ring buffer out of a slice.
NewRingBufferWithCapacity creates a new RingBuffer pre-allocated with the given capacity.
PercentDifference computes the percentage difference between two values.
PercentValueFormatter is a formatter for percent values.
PNG returns a new png/raster renderer.
RoundDown rounds down to a given roundTo value.
RoundUp rounds up to a given roundTo value.
Seq produces an array of floats from [start,end] by optional steps.
SeqDays generates a sequence of timestamps by day, from -days to today.
SeqRand generates a random sequence.
Slices generates N slices that span the total.
SVG returns a new png/raster renderer.
TimeHourValueFormatter is a ValueFormatter for timestamps.
TimeMinuteValueFormatter is a ValueFormatter for timestamps.
TimeToFloat64 returns a float64 representation of a time.
TimeValueFormatter is a ValueFormatter for timestamps.
TimeValueFormatterWithFormat is a ValueFormatter for timestamps with a given format.

# Constants

DefaultAnnotationDeltaWidth is the width of the left triangle out of annotations.
DefaultAnnotationFontSize is the font size of annotations.
DefaultAxisFontSize is the font size of the axis labels.
DefaultAxisLineWidth is the line width of the axis lines.
DefaultChartHeight is the default chart height.
DefaultChartWidth is the default chart width.
DefaultDateFormat is the default date format.
DefaultDateHourFormat is the date format for hour timestamp formats.
DefaultDateMinuteFormat is the date format for minute range timestamp formats.
DefaultDPI is the default dots per inch for the chart.
DefaultEMAPeriod is the default EMA period used in the sigma calculation.
DefaultFloatFormat is the default float format.
DefaultFontSize is the default font size.
DefaultHorizontalTickWidth is half the margin.
DefaultMACDPeriodPrimary is the long window.
DefaultMACDPeriodSecondary is the short window.
DefaultMACDSignalPeriod is the signal period to compute for the MACD.
DefaultMinimumFontSize is the default minimum font size.
DefaultMinimumTickHorizontalSpacing is the minimum distance between horizontal ticks.
DefaultMinimumTickVerticalSpacing is the minimum distance between vertical ticks.
DefaultPercentValueFormat is the default percent format.
DefaultSimpleMovingAveragePeriod is the default number of values to average.
DefaultStrokeWidth is the default chart line/stroke width.
DefaultTickCount is the default number of ticks to show.
1024.
DefaultTitleFontSize is the default title font size.
DefaultTitleTop is the default distance from the top of the chart to put the title.
DefaultVerticalTickHeight is half the margin.
DefaultXAxisMargin is the default distance from bottom of the canvas to the x axis labels.
DefaultYAxisMargin is the default distance from the right of the canvas to the y axis labels.
YAxisPrimary is the primary axis.
YAxisSecondary is the secondary axis.

# Variables

DashArrayDashesLarge is a dash array that represents '----- ----- -----' style stroke dashes.
DashArrayDashesMedium is a dash array that represents '-- -- --' style stroke dashes.
DashArrayDashesSmall is a dash array that represents '- - -' style stroke dashes.
DashArrayDots is a dash array that represents '....' style stroke dashes.
DefaultAnnotationFillColor is the default annotation background color.
DefaultAnnotationPadding is the padding around an annotation.
DefaultAxisColor is the default chart axis line color.
DefaultBackgroundColor is the default chart background color.
DefaultBackgroundPadding is the default canvas padding config.
DefaultBackgroundStrokeColor is the default chart border color.
DefaultCanvasColor is the default chart canvas color.
DefaultCanvasStrokeColor is the default chart canvas stroke color.
DefaultFillColor is the default fill color.
DefaultGridLineColor is the default grid line color.
DefaultSeriesStrokeColors are a couple default series colors.
DefaultStrokeColor is the default chart border color.
DefaultTextColor is the default chart text color.

# Structs

Annotation is a label on the chart.
AnnotationSeries is a series of labels on the chart.
BollingerBandsSeries draws bollinger bands for an inner series.
Box represents the main 4 dimensions of a box.
Chart is what we're drawing.
ContinuousSeries represents a line on a chart.
EMASeries is a computed series.
GridLine is a line on a graph canvas.
HistogramSeries is a special type of series that draws as a histogram.
MACDLineSeries is a series that computes the inner ema1-ema2 value as a series.
MACDSeries computes the difference between the MACD line and the MACD Signal line.
MACDSignalSeries computes the EMA of the MACDLineSeries.
Range represents a boundary for a set of numbers.
RingBuffer is a fifo buffer that is backed by a pre-allocated array, instead of allocating a whole new node object for each element (which saves GC churn).
SMASeries is a computed series.
Style is a simple style set.
Tick represents a label on an axis.
TimeSeries is a line on a chart.
XAxis represents the horizontal axis.
YAxis is a veritcal rule of the range.

# Interfaces

Axis is a chart feature detailing what values happen where.
BoundedLastValueProvider is a special type of value provider that can return it's (potentially computed) bounded last value.
BoundedValueProvider allows series to return a range.
FullBoundedValueProvider is an interface that combines `BoundedValueProvider` and `BoundedLastValueProvider`.
FullValueProvider is an interface that combines `ValueProvider` and `LastValueProvider`.
LastValueProvider is a special type of value provider that can return it's (potentially computed) last value.
Renderer represents the basic methods required to draw a chart.
Series is an alias to Renderable.
ValueFormatterProvider is a series that has custom formatters.
ValueProvider is a type that produces values.

# Type aliases

Float is an alias for float64 that provides a better .String() method.
Renderable is a function that can be called to render custom elements on the chart.
RendererProvider is a function that returns a renderer.
Ticks is an array of ticks.
ValueFormatter is a function that takes a value and produces a string.
YAxisType is a type of y-axis; it can either be primary or secondary.