Categorygithub.com/walle/go-chart
modulepackage
2.0.0-rc.2+incompatible
Repository: https://github.com/walle/go-chart.git
Documentation: pkg.go.dev

# README

go-chart

Build StatusGo Report Card

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.

Master should now be on the v2.x codebase, which brings a couple new features and better handling of basics like axes labeling etc. Per usual, see _examples for more information.

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:

Other Chart Types

Pie Chart:

The code for this chart can be found in _examples/pie_chart/main.go.

Stacked Bar:

The code for this chart can be found in _examples/stacked_bar/main.go.

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

# Functions

Float64ToTime returns a time from a float64.
FloatValueFormatter is a ValueFormatter for float64.
FloatValueFormatterWithFormat is a ValueFormatter for float64 with a given format.
GenerateContinuousTicks generates a set of ticks.
GenerateGridLines generates grid lines.
GetAlternateColor returns a color from the default list by index.
GetDefaultColor returns a color from the default list by index.
GetDefaultFont returns the default font (Roboto-Medium).
LastValueAnnotation returns an annotation series of just the last value of a value provider.
Legend returns a legend renderable function.
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.
PercentValueFormatter is a formatter for percent values.
PNG returns a new png/raster renderer.
StyleShow is a prebuilt style with the `Show` property set to true.
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

AllDaysMask is a bitmask of all the days of the week.
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.
DefaultBarSpacing is the default pixel spacing between bars.
DefaultBarWidth is the default pixel width of bars in a bar chart.
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.
DefaultLineSpacing is the default vertical distance between lines of text.
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.
TextHorizontalAlignCenter left aligns a string horizontally so that there are equal pixels to the left and to the right of a string within a box.
TextHorizontalAlignLeft aligns a string horizontally so that it's left ligature starts at horizontal pixel 0.
TextHorizontalAlignRight right aligns a string horizontally so that the right ligature ends at the right-most pixel of a box.
TextHorizontalAlignUnset is the unset state for text horizontal alignment.
TextVerticalAlignBaseline aligns text according to the "baseline" of the string, or where a normal ascender begins.
TextVerticalAlignBottom aligns the text according to the lowers pixel of any of the ligatures (ex.
TextVerticalAlignMiddle aligns the text so that there is an equal amount of space above and below the top and bottom of the ligatures.
TextVerticalAlignMiddleBaseline aligns the text veritcally so that there is an equal number of pixels above and below the baseline of the string.
TextVerticalAlignTop alignts the text so that the top of the ligatures are at y-pixel 0 in the container.
TextVerticalAlignUnset is the unset state for vertical alignment options.
TextWrapNone will spill text past horizontal boundaries.
TextWrapRune will split a string on a rune (i.e.
TextWrapUnset is the unset state for text wrap options.
TextWrapWord will split a string on words (i.e.
TickPositionBetweenTicks draws the labels for a tick between the previous and current tick.
TickPositionUnderTick draws the tick below the tick.
TickPositionUnset means to use the default tick position.
WeekDaysMask is a bitmask of all the weekdays of the week.
WeekendDaysMask is a bitmask of the weekend days of the week.
YAxisPrimary is the primary axis.
YAxisSecondary is the secondary axis.

# Variables

ColorAlternateBlue is a alternate theme color.
ColorAlternateGray is a alternate theme color.
ColorAlternateGreen is a alternate theme color.
ColorAlternateLightGray is a alternate theme color.
ColorAlternateYellow is a alternate theme color.
ColorBlack is the basic theme black color.
ColorBlue is the basic theme blue color.
ColorCyan is the basic theme cyan color.
ColorGreen is the basic theme green color.
ColorLightGray is the basic theme light gray color.
ColorOrange is the basic theme orange color.
ColorRed is the basic theme red color.
ColorWhite is white.
ColorYellow is the basic theme yellow color.
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.
Date contains utility functions that operate on dates.
DaysOfWeek are all the time.Weekday in an array for utility purposes.
DefaultAlternateColors are a couple alternate colors.
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.
DefaultColors are a couple default series colors.
DefaultFillColor is the default fill color.
DefaultGridLineColor is the default grid line color.
DefaultStrokeColor is the default chart border color.
DefaultTextColor is the default chart text color.
Draw contains helpers for drawing common objects.
Epoch is unix epoc saved for utility purposes.
Math contains helper methods for common math operations.
NASDAQClose is when NASDAQ closes.
NASDAQOpen is when NASDAQ opens.
NYSEArcaClose is when NYSEARCA closes.
NYSEArcaOpen is when NYSEARCA opens.
NYSEClose is when the NYSE closes.
NYSEOpen is when the NYSE opens.
Sequence contains some sequence utilities.
Text contains utilities for text.
WeekDays are the business time.Weekday in an array.
WeekendDays are the weekend time.Weekday in an array.

# Structs

AnnotationSeries is a series of labels on the chart.
BarChart is a chart that draws bars on a range.
BollingerBandsSeries draws bollinger bands for an inner series.
Box represents the main 4 dimensions of a box.
Chart is what we're drawing.
ContinuousRange represents a boundary for a set of numbers.
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.
LinearRegressionSeries is a series that plots the n-nearest neighbors linear regression for the values.
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.
MarketHoursRange is a special type of range that compresses a time range into just the market (i.e.
MaxSeries draws a horizontal line at the maximum value of the inner series.
MinSeries draws a horizontal line at the minimum value of the inner series.
PieChart is a chart that draws sections of a circle based on percentages.
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.
StackedBar is a bar within a StackedBarChart.
StackedBarChart is a chart that draws sections of a bar based on percentages.
Style is a simple style set.
TextStyle encapsulates text style options.
Tick represents a label on an axis.
TimeSeries is a line on a chart.
Value is a chart value.
Value2 is a two axis value.
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`.
GridLineProvider is a type that provides grid lines.
IsZeroable is a type that returns if it's been set or not.
LastValueProvider is a special type of value provider that can return it's (potentially computed) last value.
NameProvider is a type that returns a name.
Range is a.
Renderer represents the basic methods required to draw a chart.
Series is an alias to Renderable.
Stringable is a type that has a string representation.
StyleProvider is a type that returns a style.
TicksProvider is a type that provides ticks.
ValueFormatterProvider is a series that has custom formatters.
ValueProvider is a type that produces values.

# Type aliases

ConcatSeries is a special type of series that concatenates its `InnerSeries`.
HolidayProvider is a function that returns if a given time falls on a holiday.
Renderable is a function that can be called to render custom elements on the chart.
RendererProvider is a function that returns a renderer.
TextHorizontalAlign is an enum for the horizontal alignment options.
TextVerticalAlign is an enum for the vertical alignment options.
TextWrap is an enum for the word wrap options.
TickPosition is an enumeration of possible tick drawing positions.
Ticks is an array of ticks.
ValueFormatter is a function that takes a value and produces a string.
Values is an array of Value.
YAxisType is a type of y-axis; it can either be primary or secondary.