feat: support fill area of line chart

This commit is contained in:
vicanso 2022-08-25 20:19:05 +08:00
parent 93e03856ca
commit dc1a89d3ff
6 changed files with 130 additions and 1 deletions

View file

@ -0,0 +1,74 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/vicanso/go-charts/v2"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "area-line-chart.png")
err = ioutil.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
values := [][]float64{
{
120,
132,
101,
134,
90,
230,
210,
},
}
p, err := charts.LineRender(
values,
charts.TitleTextOptionFunc("Line"),
charts.XAxisDataOptionFunc([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}),
charts.LegendLabelsOptionFunc([]string{
"Email",
}, "50"),
func(opt *charts.ChartOption) {
opt.Legend.Padding = charts.Box{
Top: 5,
Bottom: 10,
}
opt.FillArea = true
},
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

View file

@ -2,6 +2,7 @@ package main
import (
"bytes"
"fmt"
"net/http"
"strconv"
@ -261,6 +262,35 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
},
},
},
{
Title: charts.TitleOption{
Text: "Line Area",
},
Legend: charts.NewLegendOption([]string{
"Email",
}),
XAxis: charts.NewXAxisOption([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}),
SeriesList: []charts.Series{
charts.NewSeriesFromValues([]float64{
120,
132,
101,
134,
90,
230,
210,
}),
},
FillArea: true,
},
// 柱状图
{
Title: charts.TitleOption{
@ -1935,5 +1965,6 @@ func echartsHandler(w http.ResponseWriter, req *http.Request) {
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/echarts", echartsHandler)
fmt.Println("http://127.0.0.1:3012/")
http.ListenAndServe(":3012", nil)
}