Skip to content

Commit 1a53809

Browse files
committed
Move series and legend association to defaultRender
This also is part of solving #30, this default operation should happen at this common point instead of fillDefaults(). In general fillDefaults() should match what we are doing in our constructor functions added in the prior commit.
1 parent 958571e commit 1a53809

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

chart_option.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package charts
22

33
import (
44
"fmt"
5-
"sort"
65

76
"github.com/golang/freetype/truetype"
87
)
@@ -262,25 +261,6 @@ func (o *ChartOption) fillDefault() error {
262261
if o.Padding.IsZero() {
263262
o.Padding = defaultPadding
264263
}
265-
// association between legend and series name
266-
if len(o.Legend.Data) == 0 {
267-
o.Legend.Data = o.SeriesList.Names()
268-
} else {
269-
seriesCount := len(o.SeriesList)
270-
for index, name := range o.Legend.Data {
271-
if index < seriesCount && len(o.SeriesList[index].Name) == 0 {
272-
o.SeriesList[index].Name = name
273-
}
274-
}
275-
nameIndexDict := map[string]int{}
276-
for index, name := range o.Legend.Data {
277-
nameIndexDict[name] = index
278-
}
279-
// ensure order of series is consistent with legend
280-
sort.Slice(o.SeriesList, func(i, j int) bool {
281-
return nameIndexDict[o.SeriesList[i].Name] < nameIndexDict[o.SeriesList[j].Name]
282-
})
283-
}
284264
return nil
285265
}
286266

chart_option_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func TestPieRender(t *testing.T) {
228228
require.NoError(t, err)
229229
data, err := p.Bytes()
230230
require.NoError(t, err)
231-
assertEqualSVG(t, "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 600 400\"><path d=\"M 0 0\nL 600 0\nL 600 400\nL 0 400\nL 0 0\" style=\"stroke:none;fill:white\"/><path d=\"M 20 29\nL 50 29\" style=\"stroke-width:3;stroke:rgb(84,112,198);fill:none\"/><circle cx=\"35\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(84,112,198);fill:rgb(84,112,198)\"/><text x=\"52\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Search Engine</text><path d=\"M 20 49\nL 50 49\" style=\"stroke-width:3;stroke:rgb(145,204,117);fill:none\"/><circle cx=\"35\" cy=\"49\" r=\"5\" style=\"stroke-width:3;stroke:rgb(145,204,117);fill:rgb(145,204,117)\"/><text x=\"52\" y=\"55\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Direct</text><path d=\"M 20 69\nL 50 69\" style=\"stroke-width:3;stroke:rgb(250,200,88);fill:none\"/><circle cx=\"35\" cy=\"69\" r=\"5\" style=\"stroke-width:3;stroke:rgb(250,200,88);fill:rgb(250,200,88)\"/><text x=\"52\" y=\"75\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Email</text><path d=\"M 20 89\nL 50 89\" style=\"stroke-width:3;stroke:rgb(238,102,102);fill:none\"/><circle cx=\"35\" cy=\"89\" r=\"5\" style=\"stroke-width:3;stroke:rgb(238,102,102);fill:rgb(238,102,102)\"/><text x=\"52\" y=\"95\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Union Ads</text><path d=\"M 20 109\nL 50 109\" style=\"stroke-width:3;stroke:rgb(115,192,222);fill:none\"/><circle cx=\"35\" cy=\"109\" r=\"5\" style=\"stroke-width:3;stroke:rgb(115,192,222);fill:rgb(115,192,222)\"/><text x=\"52\" y=\"115\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Video Ads</text><text x=\"222\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Rainfall vs Evaporation</text><text x=\"266\" y=\"50\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Fake Data</text><path d=\"M 300 207\nL 300 93\nA 114 114 119.89 0 1 398 263\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(84,112,198);fill:rgb(84,112,198)\"/><path d=\"M 398 150\nL 411 143\nM 411 143\nL 426 143\" style=\"stroke-width:1;stroke:rgb(84,112,198);fill:none\"/><text x=\"429\" y=\"148\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Search Engine: 33.3%</text><path d=\"M 300 207\nL 398 263\nA 114 114 84.08 0 1 254 311\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(145,204,117);fill:rgb(145,204,117)\"/><path d=\"M 335 315\nL 340 329\nM 340 329\nL 355 329\" style=\"stroke-width:1;stroke:rgb(145,204,117);fill:none\"/><text x=\"358\" y=\"334\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Direct: 23.35%</text><path d=\"M 300 207\nL 254 311\nA 114 114 66.35 0 1 187 207\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(250,200,88);fill:rgb(250,200,88)\"/><path d=\"M 205 268\nL 192 276\nM 192 276\nL 177 276\" style=\"stroke-width:1;stroke:rgb(250,200,88);fill:none\"/><text x=\"93\" y=\"281\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Email: 18.43%</text><path d=\"M 300 207\nL 187 207\nA 114 114 55.37 0 1 236 113\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(238,102,102);fill:rgb(238,102,102)\"/><path d=\"M 200 154\nL 187 147\nM 187 147\nL 172 147\" style=\"stroke-width:1;stroke:rgb(238,102,102);fill:none\"/><text x=\"61\" y=\"152\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Union Ads: 15.37%</text><path d=\"M 300 207\nL 236 113\nA 114 114 34.32 0 1 300 93\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(115,192,222);fill:rgb(115,192,222)\"/><path d=\"M 267 99\nL 262 84\nM 262 84\nL 247 84\" style=\"stroke-width:1;stroke:rgb(115,192,222);fill:none\"/><text x=\"144\" y=\"89\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Video Ads: 9.53%</text></svg>", data)
231+
assertEqualSVG(t, "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 600 400\"><path d=\"M 0 0\nL 600 0\nL 600 400\nL 0 400\nL 0 0\" style=\"stroke:none;fill:white\"/><path d=\"M 20 29\nL 50 29\" style=\"stroke-width:3;stroke:rgb(84,112,198);fill:none\"/><circle cx=\"35\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(84,112,198);fill:rgb(84,112,198)\"/><text x=\"52\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Search Engine</text><path d=\"M 20 49\nL 50 49\" style=\"stroke-width:3;stroke:rgb(145,204,117);fill:none\"/><circle cx=\"35\" cy=\"49\" r=\"5\" style=\"stroke-width:3;stroke:rgb(145,204,117);fill:rgb(145,204,117)\"/><text x=\"52\" y=\"55\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Direct</text><path d=\"M 20 69\nL 50 69\" style=\"stroke-width:3;stroke:rgb(250,200,88);fill:none\"/><circle cx=\"35\" cy=\"69\" r=\"5\" style=\"stroke-width:3;stroke:rgb(250,200,88);fill:rgb(250,200,88)\"/><text x=\"52\" y=\"75\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Email</text><path d=\"M 20 89\nL 50 89\" style=\"stroke-width:3;stroke:rgb(238,102,102);fill:none\"/><circle cx=\"35\" cy=\"89\" r=\"5\" style=\"stroke-width:3;stroke:rgb(238,102,102);fill:rgb(238,102,102)\"/><text x=\"52\" y=\"95\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Union Ads</text><path d=\"M 20 109\nL 50 109\" style=\"stroke-width:3;stroke:rgb(115,192,222);fill:none\"/><circle cx=\"35\" cy=\"109\" r=\"5\" style=\"stroke-width:3;stroke:rgb(115,192,222);fill:rgb(115,192,222)\"/><text x=\"52\" y=\"115\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Video Ads</text><text x=\"222\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Rainfall vs Evaporation</text><text x=\"266\" y=\"50\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Fake Data</text><path d=\"M 300 207\nL 300 93\nA 114 114 119.89 0 1 398 263\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(84,112,198);fill:rgb(84,112,198)\"/><path d=\"M 398 150\nL 411 143\nM 411 143\nL 426 143\" style=\"stroke-width:1;stroke:rgb(84,112,198);fill:none\"/><text x=\"429\" y=\"148\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">: 33.3%</text><path d=\"M 300 207\nL 398 263\nA 114 114 84.08 0 1 254 311\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(145,204,117);fill:rgb(145,204,117)\"/><path d=\"M 335 315\nL 340 329\nM 340 329\nL 355 329\" style=\"stroke-width:1;stroke:rgb(145,204,117);fill:none\"/><text x=\"358\" y=\"334\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">: 23.35%</text><path d=\"M 300 207\nL 254 311\nA 114 114 66.35 0 1 187 207\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(250,200,88);fill:rgb(250,200,88)\"/><path d=\"M 205 268\nL 192 276\nM 192 276\nL 177 276\" style=\"stroke-width:1;stroke:rgb(250,200,88);fill:none\"/><text x=\"125\" y=\"281\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">: 18.43%</text><path d=\"M 300 207\nL 187 207\nA 114 114 55.37 0 1 236 113\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(238,102,102);fill:rgb(238,102,102)\"/><path d=\"M 200 154\nL 187 147\nM 187 147\nL 172 147\" style=\"stroke-width:1;stroke:rgb(238,102,102);fill:none\"/><text x=\"120\" y=\"152\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">: 15.37%</text><path d=\"M 300 207\nL 236 113\nA 114 114 34.32 0 1 300 93\nL 300 207\nZ\" style=\"stroke-width:1;stroke:rgb(115,192,222);fill:rgb(115,192,222)\"/><path d=\"M 267 99\nL 262 84\nM 262 84\nL 247 84\" style=\"stroke-width:1;stroke:rgb(115,192,222);fill:none\"/><text x=\"202\" y=\"89\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">: 9.53%</text></svg>", data)
232232
}
233233

234234
func TestRadarRender(t *testing.T) {
@@ -279,7 +279,7 @@ func TestFunnelRender(t *testing.T) {
279279
require.NoError(t, err)
280280
data, err := p.Bytes()
281281
require.NoError(t, err)
282-
assertEqualSVG(t, "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 600 400\"><path d=\"M 0 0\nL 600 0\nL 600 400\nL 0 400\nL 0 0\" style=\"stroke:none;fill:white\"/><path d=\"M 87 29\nL 117 29\" style=\"stroke-width:3;stroke:rgb(84,112,198);fill:none\"/><circle cx=\"102\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(84,112,198);fill:rgb(84,112,198)\"/><text x=\"119\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Show</text><path d=\"M 177 29\nL 207 29\" style=\"stroke-width:3;stroke:rgb(145,204,117);fill:none\"/><circle cx=\"192\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(145,204,117);fill:rgb(145,204,117)\"/><text x=\"209\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Click</text><path d=\"M 263 29\nL 293 29\" style=\"stroke-width:3;stroke:rgb(250,200,88);fill:none\"/><circle cx=\"278\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(250,200,88);fill:rgb(250,200,88)\"/><text x=\"295\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Visit</text><path d=\"M 346 29\nL 376 29\" style=\"stroke-width:3;stroke:rgb(238,102,102);fill:none\"/><circle cx=\"361\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(238,102,102);fill:rgb(238,102,102)\"/><text x=\"378\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Inquiry</text><path d=\"M 445 29\nL 475 29\" style=\"stroke-width:3;stroke:rgb(115,192,222);fill:none\"/><circle cx=\"460\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(115,192,222);fill:rgb(115,192,222)\"/><text x=\"477\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Order</text><text x=\"20\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Funnel</text><path d=\"M 20 55\nL 580 55\nL 524 112\nL 76 112\nL 20 55\" style=\"stroke:none;fill:rgb(84,112,198)\"/><text x=\"264\" y=\"83\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Show(100%)</text><path d=\"M 76 114\nL 524 114\nL 468 171\nL 132 171\nL 76 114\" style=\"stroke:none;fill:rgb(145,204,117)\"/><text x=\"269\" y=\"142\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Click(80%)</text><path d=\"M 132 173\nL 468 173\nL 412 230\nL 188 230\nL 132 173\" style=\"stroke:none;fill:rgb(250,200,88)\"/><text x=\"271\" y=\"201\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Visit(60%)</text><path d=\"M 188 232\nL 412 232\nL 356 289\nL 244 289\nL 188 232\" style=\"stroke:none;fill:rgb(238,102,102)\"/><text x=\"264\" y=\"260\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Inquiry(40%)</text><path d=\"M 244 291\nL 356 291\nL 300 348\nL 300 348\nL 244 291\" style=\"stroke:none;fill:rgb(115,192,222)\"/><text x=\"268\" y=\"319\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Order(20%)</text></svg>", data)
282+
assertEqualSVG(t, "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 600 400\"><path d=\"M 0 0\nL 600 0\nL 600 400\nL 0 400\nL 0 0\" style=\"stroke:none;fill:white\"/><path d=\"M 87 29\nL 117 29\" style=\"stroke-width:3;stroke:rgb(84,112,198);fill:none\"/><circle cx=\"102\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(84,112,198);fill:rgb(84,112,198)\"/><text x=\"119\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Show</text><path d=\"M 177 29\nL 207 29\" style=\"stroke-width:3;stroke:rgb(145,204,117);fill:none\"/><circle cx=\"192\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(145,204,117);fill:rgb(145,204,117)\"/><text x=\"209\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Click</text><path d=\"M 263 29\nL 293 29\" style=\"stroke-width:3;stroke:rgb(250,200,88);fill:none\"/><circle cx=\"278\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(250,200,88);fill:rgb(250,200,88)\"/><text x=\"295\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Visit</text><path d=\"M 346 29\nL 376 29\" style=\"stroke-width:3;stroke:rgb(238,102,102);fill:none\"/><circle cx=\"361\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(238,102,102);fill:rgb(238,102,102)\"/><text x=\"378\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Inquiry</text><path d=\"M 445 29\nL 475 29\" style=\"stroke-width:3;stroke:rgb(115,192,222);fill:none\"/><circle cx=\"460\" cy=\"29\" r=\"5\" style=\"stroke-width:3;stroke:rgb(115,192,222);fill:rgb(115,192,222)\"/><text x=\"477\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Order</text><text x=\"20\" y=\"35\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Funnel</text><path d=\"M 20 55\nL 580 55\nL 524 112\nL 76 112\nL 20 55\" style=\"stroke:none;fill:rgb(84,112,198)\"/><text x=\"280\" y=\"83\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(100%)</text><path d=\"M 76 114\nL 524 114\nL 468 171\nL 132 171\nL 76 114\" style=\"stroke:none;fill:rgb(145,204,117)\"/><text x=\"284\" y=\"142\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(80%)</text><path d=\"M 132 173\nL 468 173\nL 412 230\nL 188 230\nL 132 173\" style=\"stroke:none;fill:rgb(250,200,88)\"/><text x=\"284\" y=\"201\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(60%)</text><path d=\"M 188 232\nL 412 232\nL 356 289\nL 244 289\nL 188 232\" style=\"stroke:none;fill:rgb(238,102,102)\"/><text x=\"284\" y=\"260\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(40%)</text><path d=\"M 244 291\nL 356 291\nL 300 348\nL 300 348\nL 244 291\" style=\"stroke:none;fill:rgb(115,192,222)\"/><text x=\"284\" y=\"319\" style=\"stroke:none;fill:rgb(70,70,70);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(20%)</text></svg>", data)
283283
}
284284

285285
func TestChildRender(t *testing.T) {

charts.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package charts
33
import (
44
"errors"
55
"math"
6+
"sort"
67

78
"github.com/go-analyze/charts/chartdraw"
89
)
@@ -97,18 +98,37 @@ type defaultRenderResult struct {
9798
}
9899

99100
func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, error) {
100-
fillThemeDefaults(getPreferredTheme(opt.theme), &opt.title, &opt.legend, &opt.xAxis)
101+
fillThemeDefaults(getPreferredTheme(opt.theme, p.theme), &opt.title, &opt.legend, &opt.xAxis)
101102

102103
seriesList := opt.seriesList
103104
seriesList.init()
104105
if !opt.backgroundIsFilled {
105106
p.SetBackground(p.Width(), p.Height(), opt.theme.GetBackgroundColor())
106107
}
107-
108108
if !opt.padding.IsZero() {
109109
p = p.Child(PainterPaddingOption(opt.padding))
110110
}
111111

112+
// association between legend and series name
113+
if len(opt.legend.Data) == 0 {
114+
opt.legend.Data = opt.seriesList.Names()
115+
} else {
116+
seriesCount := len(opt.seriesList)
117+
for index, name := range opt.legend.Data {
118+
if index < seriesCount && len(opt.seriesList[index].Name) == 0 {
119+
opt.seriesList[index].Name = name
120+
}
121+
}
122+
nameIndexDict := map[string]int{}
123+
for index, name := range opt.legend.Data {
124+
nameIndexDict[name] = index
125+
}
126+
// ensure order of series is consistent with legend
127+
sort.Slice(opt.seriesList, func(i, j int) bool {
128+
return nameIndexDict[opt.seriesList[i].Name] < nameIndexDict[opt.seriesList[j].Name]
129+
})
130+
}
131+
112132
const legendTitlePadding = 15
113133
legendTopSpacing := 0
114134
legendResult, err := newLegendPainter(p, opt.legend).Render()

0 commit comments

Comments
 (0)