go-chart/README.md

202 lines
6.7 KiB
Markdown
Raw Normal View History

2016-07-08 05:37:57 +02:00
go-chart
========
2016-07-11 01:28:20 +02:00
[![Build Status](https://travis-ci.org/wcharczuk/go-chart.svg?branch=master)](https://travis-ci.org/wcharczuk/go-chart)
2016-07-08 05:37:57 +02:00
Package `chart` is a very simple golang native charting library that supports timeseries and continuous
line charts.
2016-07-15 05:52:05 +02:00
The v1.0 release has been tagged so things should be more or less stable, if something changes please log an issue.
2016-07-12 03:48:51 +02:00
2016-07-08 05:37:57 +02:00
# Installation
To install `chart` run the following:
```bash
> go get -u github.com/wcharczuk/go-chart
```
Most of the components are interchangeable so feel free to crib whatever you want.
# Usage
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/images/goog_ltm.png)
The chart code to produce the above is as follows:
```go
// note this assumes that xvalues and yvalues
// have been pulled from a pricing service.
graph := chart.Chart{
Width: 1024,
Height: 400,
2016-07-10 20:30:51 +02:00
YAxis: chart.YAxis {
Style: chart.Style{
Show: true,
},
2016-07-08 05:37:57 +02:00
},
2016-07-10 20:30:51 +02:00
XAxis: chart.XAxis {
Style: chart.Style{
Show: true,
},
2016-07-08 05:37:57 +02:00
},
Series: []chart.Series{
chart.TimeSeries{
XValues: xvalues,
YValues: yvalues,
2016-07-10 20:31:53 +02:00
Style: chart.Style {
FillColor: chart.DefaultSeriesStrokeColors[0].WithAlpha(64),
},
2016-07-08 05:37:57 +02:00
},
2016-07-10 20:30:51 +02:00
chart.AnnotationSeries{
2016-07-10 20:32:15 +02:00
Name: "Last Value",
2016-07-10 20:30:51 +02:00
Style: chart.Style{
Show: true,
StrokeColor: chart.DefaultSeriesStrokeColors[0],
},
Annotations: []chart.Annotation{
chart.Annotation{
2016-07-11 08:06:14 +02:00
X: chart.TimeToFloat64(xvalues[len(xvalues)-1]),
2016-07-10 20:30:51 +02:00
Y: yvalues[len(yvalues)-1],
Label: chart.FloatValueFormatter(yvalues[len(yvalues)-1]),
},
},
},
2016-07-08 05:37:57 +02:00
},
}
graph.Render(chart.PNG, buffer) //thats it!
```
2016-07-10 20:30:51 +02:00
The key areas to note are that we have to explicitly turn on two features, the axes and add the last value label annotation series. When calling `.Render(..)` we add a parameter, `chart.PNG` that tells the renderer to use a raster renderer. Another option is to use `chart.SVG` which will use the vector renderer and create an svg representation of the chart.
2016-07-08 05:37:57 +02:00
2016-07-08 07:30:16 +02:00
# Alternate Usage
2016-07-13 07:18:34 +02:00
You can alternately leave a bunch of features turned off and constrain the proportions to something like a spark line:
2016-07-08 07:30:16 +02:00
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/images/tvix_ltm.png)
The code to produce the above would be:
```go
// note this assumes that xvalues and yvalues
// have been pulled from a pricing service.
graph := chart.Chart{
Width: 1024,
Height: 100,
Series: []chart.Series{
chart.TimeSeries{
XValues: xvalues,
YValues: yvalues,
},
},
}
graph.Render(chart.PNG, buffer)
```
2016-07-11 03:36:39 +02:00
# 2 Y-Axis Charts
2016-07-11 03:37:20 +02:00
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/images/two_axis.png)
2016-07-11 03:36:39 +02:00
It is also possible to draw series against 2 separate y-axis with their own ranges (usually good for comparison charts).
In order to map the series to an alternate axis make sure to set the `YAxis` property of the series to `YAxisSecondary`.
2016-07-13 07:18:34 +02:00
```go
graph := chart.Chart{
2016-07-13 07:19:49 +02:00
Title: stock.Name,
TitleStyle: chart.Style{
Show: false,
},
Width: width,
Height: height,
XAxis: chart.XAxis{
Style: chart.Style{
Show: true,
},
},
YAxis: chart.YAxis{
Style: chart.Style{
Show: true,
},
},
Series: []chart.Series{
chart.TimeSeries{
Name: "vea",
XValues: vx,
YValues: vy,
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultSeriesStrokeColor(0),
FillColor: chart.GetDefaultSeriesStrokeColor(0).WithAlpha(64),
},
},
chart.TimeSeries{
Name: "spy",
XValues: cx,
YValues: cy,
YAxis: chart.YAxisSecondary, // key (!)
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultSeriesStrokeColor(1),
FillColor: chart.GetDefaultSeriesStrokeColor(1).WithAlpha(64),
},
},
chart.AnnotationSeries{
Name: fmt.Sprintf("%s - Last Value", "vea"),
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultSeriesStrokeColor(0),
2016-07-13 07:18:34 +02:00
},
2016-07-13 07:19:49 +02:00
Annotations: []chart.Annotation{
chart.Annotation{
X: float64(vx[len(vx)-1].Unix()),
Y: vy[len(vy)-1],
Label: fmt.Sprintf("%s - %s", "vea", chart.FloatValueFormatter(vy[len(vy)-1])),
2016-07-13 07:18:34 +02:00
},
2016-07-13 07:19:49 +02:00
},
},
chart.AnnotationSeries{
Name: fmt.Sprintf("%s - Last Value", "goog"),
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultSeriesStrokeColor(1),
},
YAxis: chart.YAxisSecondary, // key (!)
Annotations: []chart.Annotation{
chart.Annotation{
X: float64(cx[len(cx)-1].Unix()),
Y: cy[len(cy)-1],
Label: fmt.Sprintf("%s - %s", "goog", chart.FloatValueFormatter(cy[len(cy)-1])),
2016-07-13 07:18:34 +02:00
},
},
2016-07-13 07:19:49 +02:00
},
},
}
graph.Render(chart.PNG, buffer)
2016-07-13 07:18:34 +02:00
```
2016-07-14 21:41:56 +02:00
# Moving Averages
2016-07-14 21:43:36 +02:00
You can now also graph a moving average of a series using a special `MovingAverageSeries` that takes an `InnerSeries` as a required argument.
2016-07-14 21:41:56 +02:00
2016-07-14 22:39:42 +02:00
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/images/ma_goog_ltm.png)
2016-07-14 21:41:56 +02:00
There is a helper method, `GetLastValue` on the `MovingAverageSeries` to aid in creating a last value annotation for the series.
2016-07-16 02:00:50 +02:00
# More Intense Technical Analysis
2016-07-16 02:01:51 +02:00
You can also have series that produce two values, i.e. a series that implements `BoundedValueProvider` and the `GetBoundedValue(int)(x,y1,y2 float64)` method. An example of a `BoundedValueProvider` is the included `BollingerBandsSeries`.
2016-07-16 02:00:50 +02:00
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/images/spy_ltm_bbs.png)
Like the `MovingAverageSeries` this series takes an `InnerSeries` argument as required, and defaults to 10 samples and a `K` value of 2.0 (or two standard deviations in either direction).
2016-07-08 05:37:57 +02:00
# 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.