go-chart/README.md

103 lines
3.1 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.
# 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{
X: float64(xvalues[len(xvalues)-1].Unix()), //todo: helpers for this.
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
You can alternately turn a bunch of features off and constrain the proportions to something like a spark line:
![](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-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.