diff --git a/examples/charts/main.go b/examples/charts/main.go index bac7106..fddbe6d 100644 --- a/examples/charts/main.go +++ b/examples/charts/main.go @@ -235,10 +235,13 @@ func indexHandler(w http.ResponseWriter, req *http.Request) { "Sat", "Sun", }), - Legend: charts.NewLegendOption([]string{ - "Rainfall", - "Evaporation", - }), + Legend: charts.LegendOption{ + Data: []string{ + "Rainfall", + "Evaporation", + }, + Icon: charts.LegendIconRect, + }, SeriesList: []charts.Series{ charts.NewSeriesFromValues([]float64{ 120, diff --git a/go.mod b/go.mod index ff855cc..610af22 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // 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 ) diff --git a/go.sum b/go.sum index 56d77f9..d88f473 100644 --- a/go.sum +++ b/go.sum @@ -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/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-20220321031419-a8550c1d254a h1:LnH9RNcpPv5Kzi15lXg42lYMPUf0x8CuPv1YnvBWZAg= -golang.org/x/image v0.0.0-20220321031419-a8550c1d254a/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= +golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 h1:LRtI4W37N+KFebI/qV0OFiLUv4GLOWeEW5hn/KEJvxE= +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.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/legend.go b/legend.go index 7eb33b3..df72757 100644 --- a/legend.go +++ b/legend.go @@ -48,8 +48,14 @@ type LegendOption struct { Align string // The layout orientation of legend, it can be horizontal or vertical, default is horizontal. Orient string + // Icon of the legend. + Icon string } +const ( + LegendIconRect = "rect" +) + // NewLegendOption creates a new legend option by legend text list func NewLegendOption(data []string, position ...string) LegendOption { opt := LegendOption{ @@ -143,18 +149,6 @@ func (l *legend) Render() (chart.Box, error) { } x = left 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) var renderText func() if opt.Orient == OrientVertical { @@ -183,12 +177,36 @@ func (l *legend) Render() (chart.Box, error) { if opt.Align == PositionRight { 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) - legendDraw.moveTo(x, y) - legendDraw.lineTo(x+legendWidth, y) - r.Stroke() - legendDraw.circle(float64(legendDotHeight), x+legendWidth>>1, y) - r.FillStroke() + 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.lineTo(x+legendWidth, y) + r.Stroke() + legendDraw.circle(float64(legendDotHeight), x+legendWidth>>1, y) + r.FillStroke() + } x += legendWidth if opt.Align != PositionRight {