feat: support box option for chart

This commit is contained in:
vicanso 2022-02-12 12:41:14 +08:00
parent 4262b148ca
commit bff06b2aa5
3 changed files with 17 additions and 219 deletions

View file

@ -539,10 +539,11 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
"Walnut Brownie",
},
},
Height: 20,
Padding: chart.Box{
Left: 250,
Top: -80,
Box: chart.Box{
Top: 20,
Left: 400,
Right: 500,
Bottom: 120,
},
SeriesList: charts.NewPieSeriesList([]float64{
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() {
http.HandleFunc("/", indexHandler)
http.ListenAndServe(":3012", nil)