feat: support horizontal bar chart

This commit is contained in:
vicanso 2022-06-15 23:30:37 +08:00
parent b69728dd12
commit 3f24521593
15 changed files with 677 additions and 91 deletions

View file

@ -0,0 +1,94 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/vicanso/go-charts"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "horizontal-bar-chart.png")
err = ioutil.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
p, err := charts.NewPainter(charts.PainterOptions{
Width: 800,
Height: 600,
Type: charts.ChartOutputPNG,
})
if err != nil {
panic(err)
}
_, err = charts.NewHorizontalBarChart(p, charts.HorizontalBarChartOption{
Title: charts.TitleOption{
Text: "World Population",
},
Padding: charts.Box{
Top: 20,
Right: 40,
Bottom: 20,
Left: 20,
},
Legend: charts.NewLegendOption([]string{
"2011",
"2012",
}),
YAxisOptions: charts.NewYAxisOptions([]string{
"Brazil",
"Indonesia",
"USA",
"India",
"China",
"World",
}),
SeriesList: []charts.Series{
{
Type: charts.ChartTypeHorizontalBar,
Data: charts.NewSeriesDataFromValues([]float64{
18203,
23489,
29034,
104970,
131744,
630230,
}),
},
{
Type: charts.ChartTypeHorizontalBar,
Data: charts.NewSeriesDataFromValues([]float64{
19325,
23438,
31000,
121594,
134141,
681807,
}),
},
},
}).Render()
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}