Skip to content

Commit 375786c

Browse files
authored
Merge pull request #145 from willdale/bugfix/stop-extraline-legends-from-duplicating
Ensure that extra line legends can't be added multiple times.
2 parents 4a39af9 + f2d922b commit 375786c

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

Sources/SwiftUICharts/SharedLineAndBar/ViewModifiers/ExtraLine.swift

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,55 @@ internal struct ExtraLine<T>: ViewModifier where T: CTLineBarChartDataProtocol {
6767
if self.chartData.extraLineData.style.lineColour.colourType == .colour,
6868
let colour = self.chartData.extraLineData.style.lineColour.colour
6969
{
70-
chartData.legends.append(LegendData(id: self.chartData.extraLineData.id,
71-
legend: self.chartData.extraLineData.legendTitle,
72-
colour: ColourStyle(colour: colour),
73-
strokeStyle: self.chartData.extraLineData.style.strokeStyle,
74-
prioity: 3,
75-
chartType: .line))
70+
if !chartData.legends.contains(where: {
71+
$0.legend == chartData.extraLineData.legendTitle &&
72+
$0.colour == ColourStyle(colour: colour) &&
73+
$0.prioity == 3 &&
74+
$0.chartType == .line
75+
}) {
76+
chartData.legends.append(LegendData(id: self.chartData.extraLineData.id,
77+
legend: self.chartData.extraLineData.legendTitle,
78+
colour: ColourStyle(colour: colour),
79+
strokeStyle: self.chartData.extraLineData.style.strokeStyle,
80+
prioity: 3,
81+
chartType: .line))
82+
}
7683
} else if self.chartData.extraLineData.style.lineColour.colourType == .gradientColour,
7784
let colours = self.chartData.extraLineData.style.lineColour.colours
7885
{
79-
chartData.legends.append(LegendData(id: self.chartData.extraLineData.id,
80-
legend: self.chartData.extraLineData.legendTitle,
81-
colour: ColourStyle(colours: colours,
82-
startPoint: .leading,
83-
endPoint: .trailing),
84-
strokeStyle: self.chartData.extraLineData.style.strokeStyle,
85-
prioity: 3,
86-
chartType: .line))
86+
if !chartData.legends.contains(where: {
87+
$0.legend == chartData.extraLineData.legendTitle &&
88+
$0.colour == ColourStyle(colours: colours, startPoint: .leading, endPoint: .trailing) &&
89+
$0.prioity == 3 &&
90+
$0.chartType == .line
91+
}) {
92+
chartData.legends.append(LegendData(id: self.chartData.extraLineData.id,
93+
legend: self.chartData.extraLineData.legendTitle,
94+
colour: ColourStyle(colours: colours,
95+
startPoint: .leading,
96+
endPoint: .trailing),
97+
strokeStyle: self.chartData.extraLineData.style.strokeStyle,
98+
prioity: 3,
99+
chartType: .line))
100+
}
87101
} else if self.chartData.extraLineData.style.lineColour.colourType == .gradientStops,
88102
let stops = self.chartData.extraLineData.style.lineColour.stops
89103
{
90-
chartData.legends.append(LegendData(id: self.chartData.extraLineData.id,
91-
legend: self.chartData.extraLineData.legendTitle,
92-
colour: ColourStyle(stops: stops,
93-
startPoint: .leading,
94-
endPoint: .trailing),
95-
strokeStyle: self.chartData.extraLineData.style.strokeStyle,
96-
prioity: 3,
97-
chartType: .line))
104+
if !chartData.legends.contains(where: {
105+
$0.legend == chartData.extraLineData.legendTitle &&
106+
$0.colour == ColourStyle(stops: stops, startPoint: .leading, endPoint: .trailing) &&
107+
$0.prioity == 3 &&
108+
$0.chartType == .line
109+
}) {
110+
chartData.legends.append(LegendData(id: self.chartData.extraLineData.id,
111+
legend: self.chartData.extraLineData.legendTitle,
112+
colour: ColourStyle(stops: stops,
113+
startPoint: .leading,
114+
endPoint: .trailing),
115+
strokeStyle: self.chartData.extraLineData.style.strokeStyle,
116+
prioity: 3,
117+
chartType: .line))
118+
}
98119
}
99120
}
100121
}

0 commit comments

Comments
 (0)