From 5c30ada0e3294880f28ee5dffe99ae96811b5acf Mon Sep 17 00:00:00 2001
From: Daniel Cohen Gindi <danielgindi@gmail.com>
Date: Thu, 17 Mar 2016 14:34:29 +0200
Subject: [PATCH] Styling for highlight circles based on dataset

---
 Charts/Classes/Charts/RadarChartView.swift    | 13 ---
 .../Standard/RadarChartDataSet.swift          | 20 +++++
 .../Data/Interfaces/IRadarChartDataSet.swift  | 18 ++++
 .../Renderers/RadarChartRenderer.swift        | 83 +++++++++++--------
 .../Classes/Demos/RadarChartViewController.m  | 14 +++-
 .../Classes/Data/RealmRadarDataSet.swift      | 20 +++++
 6 files changed, 121 insertions(+), 47 deletions(-)

diff --git a/Charts/Classes/Charts/RadarChartView.swift b/Charts/Classes/Charts/RadarChartView.swift
index 55f0a8e4a5..5864e12fc2 100644
--- a/Charts/Classes/Charts/RadarChartView.swift
+++ b/Charts/Classes/Charts/RadarChartView.swift
@@ -37,19 +37,6 @@ public class RadarChartView: PieRadarChartViewBase
     /// flag indicating if the web lines should be drawn or not
     public var drawWeb = true
     
-    /// flag indicating if highlight circle should be drawn or not
-    public var drawHighlightCircle = true
-    
-    public var circleFillColor = UIColor.whiteColor()
-    
-    public var circleInnerRadius = CGFloat(3.0)
-    
-    public var circleOuterRadius = CGFloat(4.0)
-    
-    public var circleStrokeWidth = CGFloat(2.0)
-    
-    public var circleStrokeAlpha = CGFloat(0.3)
-    
     /// modulus that determines how many labels and web-lines are skipped before the next is drawn
     private var _skipWebLineCount = 0
     
diff --git a/Charts/Classes/Data/Implementations/Standard/RadarChartDataSet.swift b/Charts/Classes/Data/Implementations/Standard/RadarChartDataSet.swift
index d03634e7fc..4a2091aeb5 100644
--- a/Charts/Classes/Data/Implementations/Standard/RadarChartDataSet.swift
+++ b/Charts/Classes/Data/Implementations/Standard/RadarChartDataSet.swift
@@ -37,4 +37,24 @@ public class RadarChartDataSet: LineRadarChartDataSet, IRadarChartDataSet
     // MARK: - Data functions and accessors
     
     // MARK: - Styling functions and accessors
+    
+    /// flag indicating whether highlight circle should be drawn or not
+    /// - default: false
+    public var drawHighlightCircleEnabled: Bool = false
+    
+    public var isDrawHighlightCircleEnabled: Bool { return drawHighlightCircleEnabled }
+    
+    public var highlightCircleFillColor: UIColor? = UIColor.whiteColor()
+    
+    /// The stroke color for highlight circle.
+    /// If `nil`, the the color of the dataset is taken.
+    public var highlightCircleStrokeColor: UIColor?
+    
+    public var highlightCircleStrokeAlpha: CGFloat = 0.3
+    
+    public var highlightCircleInnerRadius: CGFloat = 3.0
+    
+    public var highlightCircleOuterRadius: CGFloat = 4.0
+    
+    public var highlightCircleStrokeWidth: CGFloat = 2.0
 }
\ No newline at end of file
diff --git a/Charts/Classes/Data/Interfaces/IRadarChartDataSet.swift b/Charts/Classes/Data/Interfaces/IRadarChartDataSet.swift
index 892bd1af93..e1d4dd63be 100644
--- a/Charts/Classes/Data/Interfaces/IRadarChartDataSet.swift
+++ b/Charts/Classes/Data/Interfaces/IRadarChartDataSet.swift
@@ -20,4 +20,22 @@ public protocol IRadarChartDataSet: ILineRadarChartDataSet
     
     // MARK: - Styling functions and accessors
     
+    /// flag indicating whether highlight circle should be drawn or not
+    var drawHighlightCircleEnabled: Bool { get set }
+    
+    var isDrawHighlightCircleEnabled: Bool { get }
+    
+    var highlightCircleFillColor: UIColor? { get set }
+    
+    /// The stroke color for highlight circle.
+    /// If `nil`, the the color of the dataset is taken.
+    var highlightCircleStrokeColor: UIColor? { get set }
+    
+    var highlightCircleStrokeAlpha: CGFloat { get set }
+    
+    var highlightCircleInnerRadius: CGFloat { get set }
+    
+    var highlightCircleOuterRadius: CGFloat { get set }
+    
+    var highlightCircleStrokeWidth: CGFloat { get set }
 }
diff --git a/Charts/Classes/Renderers/RadarChartRenderer.swift b/Charts/Classes/Renderers/RadarChartRenderer.swift
index 75a68dd20b..8eeac624fa 100644
--- a/Charts/Classes/Renderers/RadarChartRenderer.swift
+++ b/Charts/Classes/Renderers/RadarChartRenderer.swift
@@ -343,14 +343,31 @@ public class RadarChartRenderer: LineRadarChartRenderer
             // draw the lines
             drawHighlightLines(context: context, point: _highlightPointBuffer, set: set)
             
-            if (_chart.drawHighlightCircle)
+            if (set.isDrawHighlightCircleEnabled)
             {
                 let p = ChartUtils.getPosition(center: center, dist: CGFloat(y) * factor,
-                    angle: sliceangle * CGFloat(j) + _chart.rotationAngle)
+                    angle: sliceangle * CGFloat(j) + chart.rotationAngle)
                 
                 if (!p.x.isNaN && !p.y.isNaN)
                 {
-                    highlightDataDot(context: context, atPoint: p, innerRadius: _chart.circleInnerRadius, outerRadius: _chart.circleOuterRadius, fillColor: _chart.circleFillColor, strokeColor: set.colorAt(0), strokeWidth: _chart.circleStrokeWidth, strokeAlpha: _chart.circleStrokeAlpha)
+                    var strokeColor = set.highlightCircleStrokeColor
+                    if strokeColor == nil
+                    {
+                        strokeColor = set.colorAt(0)
+                    }
+                    if set.highlightCircleStrokeAlpha < 1.0
+                    {
+                        strokeColor = strokeColor?.colorWithAlphaComponent(set.highlightCircleStrokeAlpha)
+                    }
+                    
+                    drawHighlightCircle(
+                        context: context,
+                        atPoint: p,
+                        innerRadius: set.highlightCircleInnerRadius,
+                        outerRadius: set.highlightCircleOuterRadius,
+                        fillColor: set.highlightCircleFillColor,
+                        strokeColor: strokeColor,
+                        strokeWidth: set.highlightCircleStrokeWidth)
                 }
             }
         }
@@ -358,38 +375,38 @@ public class RadarChartRenderer: LineRadarChartRenderer
         CGContextRestoreGState(context)
     }
     
-    internal func highlightDataDot(context context:CGContext, atPoint point:CGPoint, innerRadius:CGFloat, outerRadius:CGFloat, fillColor:UIColor, strokeColor:UIColor, strokeWidth: CGFloat, strokeAlpha: CGFloat) {
-        // draw inner filled dot
-        let innerPath = CGPathCreateMutable()
-        
-        CGPathAddArc(innerPath, nil, CGFloat(point.x), CGFloat(point.y), innerRadius, CGFloat(0), CGFloat(M_PI*2), true)
-        CGPathCloseSubpath(innerPath)
-        
-        CGContextSaveGState(context)
-        
-        CGContextSetFillColorWithColor(context, fillColor.CGColor)
-        CGContextSetAlpha(context, CGFloat(1.0))
-        
-        CGContextBeginPath(context)
-        CGContextAddPath(context, innerPath)
-        CGContextFillPath(context)
-        
-        CGContextRestoreGState(context)
-        
-        // draw outer stroke dot
-        let outerPath = CGPathCreateMutable()
-        CGPathAddArc(outerPath, nil, CGFloat(point.x), CGFloat(point.y), outerRadius, CGFloat(0), CGFloat(M_PI*2), true)
-        CGPathCloseSubpath(outerPath)
-        
+    internal func drawHighlightCircle(
+        context context: CGContext,
+        atPoint point: CGPoint,
+        innerRadius: CGFloat,
+        outerRadius: CGFloat,
+        fillColor: UIColor?,
+        strokeColor: UIColor?,
+        strokeWidth: CGFloat)
+    {
         CGContextSaveGState(context)
         
-        CGContextSetStrokeColorWithColor(context, strokeColor.CGColor)
-        CGContextSetLineWidth(context, strokeWidth)
-        CGContextSetAlpha(context, strokeAlpha)
-        
-        CGContextBeginPath(context)
-        CGContextAddPath(context, outerPath)
-        CGContextStrokePath(context)
+        if let fillColor = fillColor
+        {
+            CGContextBeginPath(context)
+            CGContextAddEllipseInRect(context, CGRectMake(point.x - outerRadius, point.y - outerRadius, outerRadius * 2.0, outerRadius * 2.0))
+            if innerRadius > 0.0
+            {
+                CGContextAddEllipseInRect(context, CGRectMake(point.x - innerRadius, point.y - innerRadius, innerRadius * 2.0, innerRadius * 2.0))
+            }
+            
+            CGContextSetFillColorWithColor(context, fillColor.CGColor)
+            CGContextEOFillPath(context)
+        }
+            
+        if let strokeColor = strokeColor
+        {
+            CGContextBeginPath(context)
+            CGContextAddEllipseInRect(context, CGRectMake(point.x - outerRadius, point.y - outerRadius, outerRadius * 2.0, outerRadius * 2.0))
+            CGContextSetStrokeColorWithColor(context, strokeColor.CGColor)
+            CGContextSetLineWidth(context, strokeWidth)
+            CGContextStrokePath(context)
+        }
         
         CGContextRestoreGState(context)
     }
diff --git a/ChartsDemo/Classes/Demos/RadarChartViewController.m b/ChartsDemo/Classes/Demos/RadarChartViewController.m
index f2fea32113..73bcc0ffa2 100644
--- a/ChartsDemo/Classes/Demos/RadarChartViewController.m
+++ b/ChartsDemo/Classes/Demos/RadarChartViewController.m
@@ -35,6 +35,7 @@ - (void)viewDidLoad
                      @{@"key": @"toggleYLabels", @"label": @"Toggle Y-Values"},
                      @{@"key": @"toggleRotate", @"label": @"Toggle Rotate"},
                      @{@"key": @"toggleFill", @"label": @"Toggle Fill"},
+                     @{@"key": @"toggleHighlightCircle", @"label": @"Toggle highlight circle"},
                      @{@"key": @"animateX", @"label": @"Animate X"},
                      @{@"key": @"animateY", @"label": @"Animate Y"},
                      @{@"key": @"animateXY", @"label": @"Animate XY"},
@@ -153,7 +154,7 @@ - (void)optionTapped:(NSString *)key
         _chartView.rotationEnabled = !_chartView.isRotationEnabled;
         return;
     }
-
+    
     if ([key isEqualToString:@"toggleFill"])
     {
         for (RadarChartDataSet *set in _chartView.data.dataSets)
@@ -165,6 +166,17 @@ - (void)optionTapped:(NSString *)key
         return;
     }
     
+    if ([key isEqualToString:@"toggleHighlightCircle"])
+    {
+        for (RadarChartDataSet *set in _chartView.data.dataSets)
+        {
+            set.drawHighlightCircleEnabled = !set.drawHighlightCircleEnabled;
+        }
+        
+        [_chartView setNeedsDisplay];
+        return;
+    }
+    
     if ([key isEqualToString:@"animateX"])
     {
         [_chartView animateWithXAxisDuration:1.4];
diff --git a/ChartsRealm/Classes/Data/RealmRadarDataSet.swift b/ChartsRealm/Classes/Data/RealmRadarDataSet.swift
index 69d6f0cd20..fbbf233bcb 100644
--- a/ChartsRealm/Classes/Data/RealmRadarDataSet.swift
+++ b/ChartsRealm/Classes/Data/RealmRadarDataSet.swift
@@ -28,4 +28,24 @@ public class RealmRadarDataSet: RealmLineRadarDataSet, IRadarChartDataSet
     // MARK: - Data functions and accessors
     
     // MARK: - Styling functions and accessors
+    
+    /// flag indicating whether highlight circle should be drawn or not
+    /// - default: false
+    public var drawHighlightCircleEnabled: Bool = false
+    
+    public var isDrawHighlightCircleEnabled: Bool { return drawHighlightCircleEnabled }
+    
+    public var highlightCircleFillColor: UIColor? = UIColor.whiteColor()
+    
+    /// The stroke color for highlight circle.
+    /// If `nil`, the the color of the dataset is taken.
+    public var highlightCircleStrokeColor: UIColor?
+    
+    public var highlightCircleStrokeAlpha: CGFloat = 0.3
+    
+    public var highlightCircleInnerRadius: CGFloat = 3.0
+    
+    public var highlightCircleOuterRadius: CGFloat = 4.0
+    
+    public var highlightCircleStrokeWidth: CGFloat = 2.0
 }
\ No newline at end of file