go-chart/README.md

96 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

2016-07-07 23:37:57 -04:00
go-chart
========
2023-09-11 16:17:58 -04:00
[![Continuous Integration](https://github.com/wcharczuk/go-chart/actions/workflows/ci.yml/badge.svg)](https://github.com/wcharczuk/go-chart/actions/workflows/ci.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/wcharczuk/go-chart)](https://goreportcard.com/report/github.com/wcharczuk/go-chart)
2016-07-07 23:37:57 -04:00
2019-09-10 00:02:48 -04:00
Package `chart` is a very simple golang native charting library that supports timeseries and continuous line charts.
2016-07-07 23:37:57 -04:00
2019-09-10 00:04:05 -04:00
Master should now be on the v3.x codebase, which overhauls the api significantly. Per usual, see `examples` for more information.
2016-08-07 14:05:57 -04:00
2016-07-07 23:37:57 -04:00
# Installation
To install `chart` run the following:
```bash
2023-09-11 16:19:37 -04:00
> go get github.com/wcharczuk/go-chart/v2@latest
2016-07-07 23:37:57 -04:00
```
2018-10-12 12:35:03 -04:00
Most of the components are interchangeable so feel free to crib whatever you want.
2016-07-07 23:37:57 -04:00
2018-10-12 12:35:03 -04:00
# Output Examples
2016-07-07 23:37:57 -04:00
2016-07-16 23:53:46 -04:00
Spark Lines:
2016-07-07 23:37:57 -04:00
2016-08-07 01:00:22 -04:00
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/tvix_ltm.png)
2016-07-07 23:37:57 -04:00
2016-07-16 23:53:46 -04:00
Single axis:
2016-07-07 23:37:57 -04:00
2016-08-07 01:00:22 -04:00
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/goog_ltm.png)
2016-07-07 23:37:57 -04:00
2016-07-16 23:53:46 -04:00
Two axis:
2016-07-07 23:37:57 -04:00
2016-08-07 01:00:22 -04:00
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/two_axis.png)
2016-07-08 01:30:16 -04:00
2016-07-28 22:17:35 -04:00
# Other Chart Types
2016-07-08 01:30:16 -04:00
2016-07-28 22:17:35 -04:00
Pie Chart:
2016-07-08 01:30:16 -04:00
2016-08-07 01:00:22 -04:00
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/pie_chart.png)
2016-07-16 23:53:46 -04:00
2019-09-10 00:04:05 -04:00
The code for this chart can be found in `examples/pie_chart/main.go`.
2016-07-28 22:19:26 -04:00
2016-07-30 12:39:50 -04:00
Stacked Bar:
2016-08-07 01:00:22 -04:00
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/stacked_bar.png)
2016-07-30 12:39:50 -04:00
2019-09-10 00:04:05 -04:00
The code for this chart can be found in `examples/stacked_bar/main.go`.
2016-07-30 12:39:50 -04:00
2016-07-16 23:53:46 -04:00
# Code Examples
2019-09-10 00:04:05 -04:00
Actual chart configurations and examples can be found in the `./examples/` directory. They are simple CLI programs that write to `output.png` (they are also updated with `go generate`.
2016-07-16 23:53:46 -04:00
# Usage
2016-07-08 01:30:16 -04:00
2016-07-16 23:53:46 -04:00
Everything starts with the `chart.Chart` object. The bare minimum to draw a chart would be the following:
2016-07-10 21:36:39 -04:00
2016-07-16 23:53:46 -04:00
```golang
2016-07-10 21:36:39 -04:00
2016-07-16 23:53:46 -04:00
import (
...
"bytes"
...
2023-09-11 16:19:37 -04:00
"github.com/wcharczuk/go-chart/v2" //exposes "chart"
2016-07-16 23:53:46 -04:00
)
2016-07-10 21:36:39 -04:00
2016-07-13 01:18:34 -04:00
graph := chart.Chart{
2016-07-13 01:19:49 -04:00
Series: []chart.Series{
2016-07-16 23:53:46 -04:00
chart.ContinuousSeries{
XValues: []float64{1.0, 2.0, 3.0, 4.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0},
2016-07-13 01:19:49 -04:00
},
},
}
2016-07-14 15:41:56 -04:00
2016-07-16 23:53:46 -04:00
buffer := bytes.NewBuffer([]byte{})
err := graph.Render(chart.PNG, buffer)
```
2016-07-14 15:41:56 -04:00
2016-07-16 23:53:46 -04:00
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).
2016-07-14 15:41:56 -04:00
2016-07-16 23:53:46 -04:00
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`.
2016-07-15 20:00:50 -04:00
2016-07-16 23:53:46 -04:00
# API Overview
2016-07-15 20:00:50 -04:00
2019-09-10 00:02:48 -04:00
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.
2016-07-15 20:00:50 -04:00
2016-08-07 01:00:22 -04:00
The best way to see the api in action is to look at the examples in the `./_examples/` directory.
2016-07-15 20:00:50 -04:00
2016-07-07 23:37:57 -04: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
2019-09-10 00:02:48 -04:00
Contributions are welcome though this library is in a holding pattern for the forseable future.