feat: support line chart render function

This commit is contained in:
vicanso 2022-06-12 11:55:37 +08:00
parent b394e1b49f
commit c4045cfbbe
11 changed files with 1012 additions and 46 deletions

111
examples/line_chart/main.go Normal file
View file

@ -0,0 +1,111 @@
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, "line-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.NewLineChart(p, charts.LineChartOption{
Padding: charts.Box{
Left: 10,
Top: 10,
Right: 10,
Bottom: 10,
},
XAxis: charts.NewXAxisOption([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}),
SeriesList: charts.SeriesList{
charts.NewSeriesFromValues([]float64{
120,
132,
101,
134,
90,
230,
210,
}),
charts.NewSeriesFromValues([]float64{
220,
182,
191,
234,
290,
330,
310,
}),
charts.NewSeriesFromValues([]float64{
150,
232,
201,
154,
190,
330,
410,
}),
charts.NewSeriesFromValues([]float64{
320,
332,
301,
334,
390,
330,
320,
}),
charts.NewSeriesFromValues([]float64{
820,
932,
901,
934,
1290,
1330,
1320,
}),
},
}).Render()
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

View file

@ -409,6 +409,7 @@ func main() {
Bottom: 20,
})
// grid
top += 50
charts.NewGridPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
@ -422,6 +423,7 @@ func main() {
StrokeColor: drawing.ColorBlue,
}).Render()
// legend
top += 100
charts.NewLegendPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
@ -440,6 +442,7 @@ func main() {
FontColor: drawing.ColorBlack,
}).Render()
// legend
top += 30
charts.NewLegendPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
@ -460,6 +463,7 @@ func main() {
FontColor: drawing.ColorBlack,
}).Render()
// legend
top += 30
charts.NewLegendPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
@ -479,6 +483,7 @@ func main() {
FontColor: drawing.ColorBlack,
}).Render()
// axis bottom
top += 100
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
@ -500,6 +505,7 @@ func main() {
FontColor: drawing.ColorBlack,
}).Render()
// axis top
top += 50
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
@ -523,11 +529,12 @@ func main() {
FontColor: drawing.ColorBlack,
}).Render()
// axis left
top += 50
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 10,
Right: p.Width() - 1,
Right: 60,
Bottom: top + 200,
})), charts.AxisPainterOption{
Position: charts.PositionLeft,
@ -544,6 +551,51 @@ func main() {
FontSize: 12,
FontColor: drawing.ColorBlack,
}).Render()
// axis right
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 100,
Right: 150,
Bottom: top + 200,
})), charts.AxisPainterOption{
Position: charts.PositionRight,
Data: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
StrokeColor: drawing.ColorBlack,
FontSize: 12,
FontColor: drawing.ColorBlack,
}).Render()
// axis left no tick
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 150,
Right: 300,
Bottom: top + 200,
})), charts.AxisPainterOption{
BoundaryGap: charts.FalseFlag(),
Position: charts.PositionLeft,
Data: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
FontSize: 12,
FontColor: drawing.ColorBlack,
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack.WithAlpha(100),
}).Render()
buf, err := p.Bytes()
if err != nil {