From 2fedfc6e50e49ed69dddf9e299c9fef88d4f1203 Mon Sep 17 00:00:00 2001 From: Alexei Date: Tue, 12 Feb 2019 13:49:58 -0500 Subject: [PATCH] Multiple colors for valueline (Fixes #3480) (#3709) * Multiple colors for valueline (Fixes #3480) This change adds a flag matchValueLineColorToPieSlice to PieChartDataSet and IPieChartDataSet protocol. When enabled, valuelines will have the same color as slices they attached to. matchValueLineColorToPieSlice is set to false by default, so colors won't be changed in old projects that use Charts. * Changed variable name from matchValueLineColorToPieSlice to valueLineAutoColor * Changed variable name from valueLineAutoColor to useValueColorForLine * Changed variable name from valueLineAutoColor to useValueColorForLine * fix code style fix code style * Changed check for useValueColorForLine with suggested * fix code style * Added name DrawLine: to do{} section --- .../Standard/PieChartDataSet.swift | 3 +++ .../Data/Interfaces/IPieChartDataSet.swift | 3 +++ Source/Charts/Renderers/PieChartRenderer.swift | 17 ++++++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift index 857a8ea69b..069ebf9789 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift @@ -84,6 +84,9 @@ open class PieChartDataSet: ChartDataSet, IPieChartDataSet /// When valuePosition is OutsideSlice, indicates line color open var valueLineColor: NSUIColor? = NSUIColor.black + /// When valuePosition is OutsideSlice and enabled, line will have the same color as the slice + open var useValueColorForLine: Bool = false + /// When valuePosition is OutsideSlice, indicates line width open var valueLineWidth: CGFloat = 1.0 diff --git a/Source/Charts/Data/Interfaces/IPieChartDataSet.swift b/Source/Charts/Data/Interfaces/IPieChartDataSet.swift index 872a031170..b85249a292 100644 --- a/Source/Charts/Data/Interfaces/IPieChartDataSet.swift +++ b/Source/Charts/Data/Interfaces/IPieChartDataSet.swift @@ -38,6 +38,9 @@ public protocol IPieChartDataSet: IChartDataSet /// When valuePosition is OutsideSlice, indicates line color var valueLineColor: NSUIColor? { get set } + /// When valuePosition is OutsideSlice and enabled, line will have the same color as the slice + var useValueColorForLine: Bool { get set } + /// When valuePosition is OutsideSlice, indicates line width var valueLineWidth: CGFloat { get set } diff --git a/Source/Charts/Renderers/PieChartRenderer.swift b/Source/Charts/Renderers/PieChartRenderer.swift index c1ceb7c1fe..e50106c351 100644 --- a/Source/Charts/Renderers/PieChartRenderer.swift +++ b/Source/Charts/Renderers/PieChartRenderer.swift @@ -438,9 +438,20 @@ open class PieChartRenderer: DataRenderer labelPoint = CGPoint(x: pt2.x + 5, y: pt2.y - lineHeight) } - if dataSet.valueLineColor != nil + DrawLine: do { - context.setStrokeColor(dataSet.valueLineColor!.cgColor) + if dataSet.useValueColorForLine + { + context.setStrokeColor(dataSet.color(atIndex: j).cgColor) + } + else if let valueLineColor = dataSet.valueLineColor + { + context.setStrokeColor(valueLineColor.cgColor) + } + else + { + return + } context.setLineWidth(dataSet.valueLineWidth) context.move(to: CGPoint(x: pt0.x, y: pt0.y)) @@ -449,7 +460,7 @@ open class PieChartRenderer: DataRenderer context.drawPath(using: CGPathDrawingMode.stroke) } - + if drawXOutside && drawYOutside { ChartUtils.drawText(