feat: support box option for chart
This commit is contained in:
parent
4262b148ca
commit
bff06b2aa5
3 changed files with 17 additions and 219 deletions
2
chart.go
2
chart.go
|
|
@ -56,6 +56,7 @@ type ChartOption struct {
|
||||||
Height int
|
Height int
|
||||||
Parent *Draw
|
Parent *Draw
|
||||||
Padding chart.Box
|
Padding chart.Box
|
||||||
|
Box chart.Box
|
||||||
SeriesList []Series
|
SeriesList []Series
|
||||||
BackgroundColor drawing.Color
|
BackgroundColor drawing.Color
|
||||||
Children []ChartOption
|
Children []ChartOption
|
||||||
|
|
@ -337,6 +338,7 @@ func chartBasicRender(opt *ChartOption) (*basicRenderResult, error) {
|
||||||
Height: opt.getHeight(),
|
Height: opt.getHeight(),
|
||||||
},
|
},
|
||||||
PaddingOption(opt.Padding),
|
PaddingOption(opt.Padding),
|
||||||
|
BoxOption(opt.Box),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
10
draw.go
10
draw.go
|
|
@ -71,6 +71,16 @@ func PaddingOption(padding chart.Box) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BoxOption(box chart.Box) Option {
|
||||||
|
return func(d *Draw) error {
|
||||||
|
if box.IsZero() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
d.Box = box
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewDraw(opt DrawOption, opts ...Option) (*Draw, error) {
|
func NewDraw(opt DrawOption, opts ...Option) (*Draw, error) {
|
||||||
if opt.Parent == nil && (opt.Width <= 0 || opt.Height <= 0) {
|
if opt.Parent == nil && (opt.Width <= 0 || opt.Height <= 0) {
|
||||||
return nil, errors.New("parent and width/height can not be nil")
|
return nil, errors.New("parent and width/height can not be nil")
|
||||||
|
|
|
||||||
|
|
@ -539,10 +539,11 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
"Walnut Brownie",
|
"Walnut Brownie",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Height: 20,
|
Box: chart.Box{
|
||||||
Padding: chart.Box{
|
Top: 20,
|
||||||
Left: 250,
|
Left: 400,
|
||||||
Top: -80,
|
Right: 500,
|
||||||
|
Bottom: 120,
|
||||||
},
|
},
|
||||||
SeriesList: charts.NewPieSeriesList([]float64{
|
SeriesList: charts.NewPieSeriesList([]float64{
|
||||||
435.9,
|
435.9,
|
||||||
|
|
@ -575,221 +576,6 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func indexHandlerBak(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.URL.Path != "/" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
zero := float64(0)
|
|
||||||
outputType := r.URL.Query().Get("type")
|
|
||||||
chartOption := charts.ChartOption{
|
|
||||||
// Theme: "dark",
|
|
||||||
Type: outputType,
|
|
||||||
Title: charts.TitleOption{
|
|
||||||
Left: charts.PositionCenter,
|
|
||||||
Style: chart.Style{
|
|
||||||
FontColor: chart.ColorAlternateBlue,
|
|
||||||
},
|
|
||||||
SubtextStyle: chart.Style{
|
|
||||||
FontColor: chart.ColorRed,
|
|
||||||
},
|
|
||||||
Text: "Stacked Line",
|
|
||||||
Subtext: "Hello World!",
|
|
||||||
},
|
|
||||||
Padding: chart.Box{
|
|
||||||
Left: 5,
|
|
||||||
Top: 15,
|
|
||||||
Bottom: 5,
|
|
||||||
Right: 10,
|
|
||||||
},
|
|
||||||
YAxisList: []charts.YAxisOption{
|
|
||||||
{
|
|
||||||
Min: &zero,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Formatter: "{value} °C",
|
|
||||||
// Max: charts.NewFloatPoint(24),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Legend: charts.LegendOption{
|
|
||||||
Data: []string{
|
|
||||||
"Email",
|
|
||||||
"Union Ads",
|
|
||||||
// "Video Ads",
|
|
||||||
},
|
|
||||||
Left: charts.PositionLeft,
|
|
||||||
// Orient: charts.OrientVertical,
|
|
||||||
},
|
|
||||||
XAxis: charts.XAxisOption{
|
|
||||||
Data: []string{
|
|
||||||
"Mon",
|
|
||||||
"Tue",
|
|
||||||
"Wed",
|
|
||||||
"Thu",
|
|
||||||
"Fri",
|
|
||||||
"Sat",
|
|
||||||
"Sun",
|
|
||||||
},
|
|
||||||
// SplitNumber: 4,
|
|
||||||
// BoundaryGap: charts.FalseFlag(),
|
|
||||||
},
|
|
||||||
SeriesList: []charts.Series{
|
|
||||||
{
|
|
||||||
// Type: charts.ChartTypeBar,
|
|
||||||
MarkPoint: charts.SeriesMarkPoint{
|
|
||||||
Data: []charts.SeriesMarkPointData{
|
|
||||||
{
|
|
||||||
Type: "max",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: "min",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MarkLine: charts.SeriesMarkLine{
|
|
||||||
Data: []charts.SeriesMarkLineData{
|
|
||||||
// {
|
|
||||||
// Type: "max",
|
|
||||||
// },
|
|
||||||
{
|
|
||||||
Type: "average",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Data: charts.NewSeriesDataFromValues([]float64{
|
|
||||||
2.0,
|
|
||||||
4.9,
|
|
||||||
7.0,
|
|
||||||
23.2,
|
|
||||||
25.6,
|
|
||||||
76.7,
|
|
||||||
135.6,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
// {
|
|
||||||
// // Type: charts.ChartTypeBar,
|
|
||||||
// Data: charts.NewSeriesDataFromValues([]float64{
|
|
||||||
// 2.6,
|
|
||||||
// 5.9,
|
|
||||||
// 9.0,
|
|
||||||
// 26.4,
|
|
||||||
// 28.7,
|
|
||||||
// 70.7,
|
|
||||||
// 175.6,
|
|
||||||
// }),
|
|
||||||
// },
|
|
||||||
{
|
|
||||||
Data: charts.NewSeriesDataFromValues([]float64{
|
|
||||||
2.0,
|
|
||||||
2.2,
|
|
||||||
3.3,
|
|
||||||
4.5,
|
|
||||||
6.3,
|
|
||||||
10.2,
|
|
||||||
20.3,
|
|
||||||
}),
|
|
||||||
YAxisIndex: 1,
|
|
||||||
},
|
|
||||||
// {
|
|
||||||
// Data: []charts.SeriesData{
|
|
||||||
// {
|
|
||||||
// Value: 220,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 182,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 191,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 234,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 290,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 330,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 310,
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Data: []charts.SeriesData{
|
|
||||||
// {
|
|
||||||
// Value: 150,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 232,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 201,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 154,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 190,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 330,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Value: 410,
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
},
|
|
||||||
// Children: []charts.ChartOption{
|
|
||||||
// {
|
|
||||||
// Padding: chart.Box{
|
|
||||||
// Left: 350,
|
|
||||||
// Top: 0,
|
|
||||||
// },
|
|
||||||
// Legend: charts.LegendOption{
|
|
||||||
// Show: charts.FalseFlag(),
|
|
||||||
// },
|
|
||||||
// Width: 150,
|
|
||||||
// Height: 150,
|
|
||||||
// SeriesList: []charts.Series{
|
|
||||||
// charts.NewSeriesFromValues([]float64{
|
|
||||||
// 1048,
|
|
||||||
// }, charts.ChartTypePie),
|
|
||||||
// {
|
|
||||||
// Data: charts.NewSeriesDataFromValues([]float64{
|
|
||||||
// 735,
|
|
||||||
// }),
|
|
||||||
// Radius: "50%",
|
|
||||||
// Name: "test",
|
|
||||||
// },
|
|
||||||
// charts.NewSeriesFromValues([]float64{
|
|
||||||
// 580,
|
|
||||||
// }),
|
|
||||||
// charts.NewSeriesFromValues([]float64{
|
|
||||||
// 484,
|
|
||||||
// }),
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
}
|
|
||||||
d, err := charts.Render(chartOption)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
buf, _ := d.Bytes()
|
|
||||||
|
|
||||||
if outputType == "png" {
|
|
||||||
w.Header().Set("Content-Type", "image/png")
|
|
||||||
w.Write(buf)
|
|
||||||
} else {
|
|
||||||
data := bytes.ReplaceAll([]byte(html), []byte("{{body}}"), buf)
|
|
||||||
w.Header().Set("Content-Type", "text/html")
|
|
||||||
w.Write(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/", indexHandler)
|
http.HandleFunc("/", indexHandler)
|
||||||
http.ListenAndServe(":3012", nil)
|
http.ListenAndServe(":3012", nil)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue