drawing | ||
images | ||
testserver | ||
.travis.yml | ||
annotation_series_test.go | ||
annotation_series.go | ||
axis.go | ||
box_test.go | ||
box.go | ||
chart_test.go | ||
chart.go | ||
continuous_series.go | ||
defaults.go | ||
drawing_helpers.go | ||
grid_line_test.go | ||
grid_line.go | ||
LICENSE | ||
range_test.go | ||
range.go | ||
raster_renderer.go | ||
README.md | ||
renderable.go | ||
renderer_provider.go | ||
renderer.go | ||
roboto.go | ||
series.go | ||
style_test.go | ||
style.go | ||
tick_test.go | ||
tick.go | ||
time_series_test.go | ||
time_series.go | ||
util_test.go | ||
util.go | ||
value_formatter_provider.go | ||
value_formatter_test.go | ||
value_formatter.go | ||
value_provider.go | ||
vector_renderer_test.go | ||
vector_renderer.go | ||
xaxis_test.go | ||
xaxis.go | ||
yaxis_test.go | ||
yaxis.go |
go-chart
Package chart
is a very simple golang native charting library that supports timeseries and continuous
line charts.
The API is still in a bit of flux, so it is adviseable to wait until I tag a v1.0 release before using in a production capacity.
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.
Usage
The chart code to produce the above is as follows:
// note this assumes that xvalues and yvalues
// have been pulled from a pricing service.
graph := chart.Chart{
Width: 1024,
Height: 400,
YAxis: chart.YAxis {
Style: chart.Style{
Show: true,
},
},
XAxis: chart.XAxis {
Style: chart.Style{
Show: true,
},
},
Series: []chart.Series{
chart.TimeSeries{
XValues: xvalues,
YValues: yvalues,
Style: chart.Style {
FillColor: chart.DefaultSeriesStrokeColors[0].WithAlpha(64),
},
},
chart.AnnotationSeries{
Name: "Last Value",
Style: chart.Style{
Show: true,
StrokeColor: chart.DefaultSeriesStrokeColors[0],
},
Annotations: []chart.Annotation{
chart.Annotation{
X: chart.TimeToFloat64(xvalues[len(xvalues)-1]),
Y: yvalues[len(yvalues)-1],
Label: chart.FloatValueFormatter(yvalues[len(yvalues)-1]),
},
},
},
},
}
graph.Render(chart.PNG, buffer) //thats it!
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.
Alternate Usage
You can alternately turn a bunch of features off and constrain the proportions to something like a spark line:
The code to produce the above would be:
// 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)
2 Y-Axis Charts
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
.
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.