| 1 | go-chart |
| 2 | ======== |
| 3 | [](https://github.com/wcharczuk/go-chart/actions/workflows/ci.yml) [](https://goreportcard.com/report/github.com/wcharczuk/go-chart) |
| 4 | |
| 5 | Package `chart` is a very simple golang native charting library that supports timeseries and continuous line charts. |
| 6 | |
| 7 | Master should now be on the v3.x codebase, which overhauls the api significantly. Per usual, see `examples` for more information. |
| 8 | |
| 9 | # Installation |
| 10 | |
| 11 | To install `chart` run the following: |
| 12 | |
| 13 | ```bash |
| 14 | > go get github.com/wcharczuk/go-chart/v2@latest |
| 15 | ``` |
| 16 | |
| 17 | Most of the components are interchangeable so feel free to crib whatever you want. |
| 18 | |
| 19 | # Output Examples |
| 20 | |
| 21 | Spark Lines: |
| 22 | |
| 23 |  |
| 24 | |
| 25 | Single axis: |
| 26 | |
| 27 |  |
| 28 | |
| 29 | Two axis: |
| 30 | |
| 31 |  |
| 32 | |
| 33 | # Other Chart Types |
| 34 | |
| 35 | Pie Chart: |
| 36 | |
| 37 |  |
| 38 | |
| 39 | The code for this chart can be found in `examples/pie_chart/main.go`. |
| 40 | |
| 41 | Stacked Bar: |
| 42 | |
| 43 |  |
| 44 | |
| 45 | The code for this chart can be found in `examples/stacked_bar/main.go`. |
| 46 | |
| 47 | # Code Examples |
| 48 | |
| 49 | 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`. |
| 50 | |
| 51 | # Usage |
| 52 | |
| 53 | Everything starts with the `chart.Chart` object. The bare minimum to draw a chart would be the following: |
| 54 | |
| 55 | ```golang |
| 56 | |
| 57 | import ( |
| 58 | ... |
| 59 | "bytes" |
| 60 | ... |
| 61 | "github.com/wcharczuk/go-chart/v2" //exposes "chart" |
| 62 | ) |
| 63 | |
| 64 | graph := chart.Chart{ |
| 65 | Series: []chart.Series{ |
| 66 | chart.ContinuousSeries{ |
| 67 | XValues: []float64{1.0, 2.0, 3.0, 4.0}, |
| 68 | YValues: []float64{1.0, 2.0, 3.0, 4.0}, |
| 69 | }, |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | buffer := bytes.NewBuffer([]byte{}) |
| 74 | err := graph.Render(chart.PNG, buffer) |
| 75 | ``` |
| 76 | |
| 77 | 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). |
| 78 | |
| 79 | 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`. |
| 80 | |
| 81 | # API Overview |
| 82 | |
| 83 | 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. |
| 84 | |
| 85 | The best way to see the api in action is to look at the examples in the `./_examples/` directory. |
| 86 | |
| 87 | # Design Philosophy |
| 88 | |
| 89 | 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). |
| 90 | |
| 91 | 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. |
| 92 | |
| 93 | # Contributions |
| 94 | |
| 95 | Contributions are welcome though this library is in a holding pattern for the forseable future. |