feat: support rect legend

This commit is contained in:
vicanso 2022-04-14 20:43:32 +08:00
parent a5754bb1b3
commit a713c3023e
4 changed files with 45 additions and 24 deletions

View file

@ -235,10 +235,13 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
"Sat", "Sat",
"Sun", "Sun",
}), }),
Legend: charts.NewLegendOption([]string{ Legend: charts.LegendOption{
Data: []string{
"Rainfall", "Rainfall",
"Evaporation", "Evaporation",
}), },
Icon: charts.LegendIconRect,
},
SeriesList: []charts.Series{ SeriesList: []charts.Series{
charts.NewSeriesFromValues([]float64{ charts.NewSeriesFromValues([]float64{
120, 120,

2
go.mod
View file

@ -12,6 +12,6 @@ require (
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a // indirect golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
) )

4
go.sum
View file

@ -13,8 +13,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I= github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I=
github.com/wcharczuk/go-chart/v2 v2.1.0/go.mod h1:yx7MvAVNcP/kN9lKXM/NTce4au4DFN99j6i1OwDclNA= github.com/wcharczuk/go-chart/v2 v2.1.0/go.mod h1:yx7MvAVNcP/kN9lKXM/NTce4au4DFN99j6i1OwDclNA=
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a h1:LnH9RNcpPv5Kzi15lXg42lYMPUf0x8CuPv1YnvBWZAg= golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 h1:LRtI4W37N+KFebI/qV0OFiLUv4GLOWeEW5hn/KEJvxE=
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View file

@ -48,8 +48,14 @@ type LegendOption struct {
Align string Align string
// The layout orientation of legend, it can be horizontal or vertical, default is horizontal. // The layout orientation of legend, it can be horizontal or vertical, default is horizontal.
Orient string Orient string
// Icon of the legend.
Icon string
} }
const (
LegendIconRect = "rect"
)
// NewLegendOption creates a new legend option by legend text list // NewLegendOption creates a new legend option by legend text list
func NewLegendOption(data []string, position ...string) LegendOption { func NewLegendOption(data []string, position ...string) LegendOption {
opt := LegendOption{ opt := LegendOption{
@ -143,18 +149,6 @@ func (l *legend) Render() (chart.Box, error) {
} }
x = left x = left
for index, text := range opt.Data { for index, text := range opt.Data {
seriesColor := theme.GetSeriesColor(index)
fillColor := seriesColor
if !theme.IsDark() {
fillColor = theme.GetBackgroundColor()
}
style := chart.Style{
StrokeColor: seriesColor,
FillColor: fillColor,
StrokeWidth: 3,
}
style.GetFillAndStrokeOptions().WriteDrawingOptionsToRenderer(r)
textBox := r.MeasureText(text) textBox := r.MeasureText(text)
var renderText func() var renderText func()
if opt.Orient == OrientVertical { if opt.Orient == OrientVertical {
@ -183,12 +177,36 @@ func (l *legend) Render() (chart.Box, error) {
if opt.Align == PositionRight { if opt.Align == PositionRight {
renderText() renderText()
} }
seriesColor := theme.GetSeriesColor(index)
fillColor := seriesColor
if !theme.IsDark() {
fillColor = theme.GetBackgroundColor()
}
style := chart.Style{
StrokeColor: seriesColor,
FillColor: fillColor,
StrokeWidth: 3,
}
if opt.Icon == LegendIconRect {
style.FillColor = seriesColor
style.StrokeWidth = 1
}
style.GetFillAndStrokeOptions().WriteDrawingOptionsToRenderer(r)
if opt.Icon == LegendIconRect {
legendDraw.moveTo(x, y-legendDotHeight)
legendDraw.lineTo(x+legendWidth, y-legendDotHeight)
legendDraw.lineTo(x+legendWidth, y+legendDotHeight)
legendDraw.lineTo(x, y+legendDotHeight)
legendDraw.lineTo(x, y-legendDotHeight)
r.FillStroke()
} else {
legendDraw.moveTo(x, y) legendDraw.moveTo(x, y)
legendDraw.lineTo(x+legendWidth, y) legendDraw.lineTo(x+legendWidth, y)
r.Stroke() r.Stroke()
legendDraw.circle(float64(legendDotHeight), x+legendWidth>>1, y) legendDraw.circle(float64(legendDotHeight), x+legendWidth>>1, y)
r.FillStroke() r.FillStroke()
}
x += legendWidth x += legendWidth
if opt.Align != PositionRight { if opt.Align != PositionRight {