Categorygithub.com/t-mw/go-chart
modulepackage
2.0.1+incompatible
Repository: https://github.com/t-mw/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
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

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).
Jet is a color map provider based on matlab's jet color map.
LastValueAnnotation returns an annotation series of just the last value of a value provider.
Legend returns a legend renderable function.
LegendLeft is a legend that is designed for longer series lists.
LegendThin is a legend that doesn't obscure the chart area.
NewBox returns a new (set) box.
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.
StyleTextDefaults returns a style for drawing outside a chart context.
SVG returns a new png/raster renderer.
TimeDateValueFormatter is a ValueFormatter for timestamps.
TimeHourValueFormatter is a ValueFormatter for timestamps.
TimeMinuteValueFormatter is a ValueFormatter for timestamps.
TimeValueFormatter is a ValueFormatter for timestamps.
TimeValueFormatterWithFormat returns a time formatter with a given format.
Viridis creates a color map provider.

# Constants

ContentTypePNG is the png mime type.
ContentTypeSVG is the svg mime type.
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.
DefaultBackgroundStrokeWidth is the default stroke on the chart background.
DefaultBarSpacing is the default pixel spacing between bars.
DefaultBarWidth is the default pixel width of bars in a bar chart.
DefaultCanvasStrokeWidth is the default stroke on the chart canvas.
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.
DefaultDotWidth is the default chart dot width.
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.
DefaultSeriesLineWidth is the default line width.
DefaultSimpleMovingAveragePeriod is the default number of values to average.
DefaultStrokeWidth is the default chart 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.
Disabled indicates if the value should be interpreted as set intentionally to zero.
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.
YAxisPrimary is the primary axis.
YAxisSecondary is the secondary axis.

# Variables

AlternateColorPalette represents the default palatte.
BoxZero is a preset box that represents an intentional zero value.
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.
ColorTransparent is a transparent (alpha zero) 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.
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.
DefaultColorPalette represents the default palatte.
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.
Text contains utilities for text.

# 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.
BoxCorners is a box with independent corners.
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.
ImageWriter is a special type of io.Writer that produces a final image.
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.
Point is an X,Y pair.
PolynomialRegressionSeries implements a polynomial regression over a given inner series.
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.
BoundedLastValuesProvider is a special type of value provider that can return it's (potentially computed) bounded last value.
BoundedValuesProvider allows series to return a range.
ColorPalette is a set of colors that.
FullBoundedValuesProvider is an interface that combines `BoundedValuesProvider` and `BoundedLastValuesProvider`.
FullValuesProvider is an interface that combines `ValuesProvider` and `LastValuesProvider`.
GridLineProvider is a type that provides grid lines.
IsZeroable is a type that returns if it's been set or not.
LastValuesProvider 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 common interface for a range of values.
Renderer represents the basic methods required to draw a chart.
RGBACollector is a render target for 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.
ValuesProvider is a type that produces values.

# Type aliases

ColorProvider is a general provider for color ranges based on values.
ConcatSeries is a special type of series that concatenates its `InnerSeries`.
DotColorProvider is a provider for dot color.
Renderable is a function that can be called to render custom elements on the chart.
RendererProvider is a function that returns a renderer.
SizeProvider is a provider for integer size.
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.