Skip to content

Commit

Permalink
Styling for highlight circles based on dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Mar 17, 2016
1 parent 4b86e9c commit 5c30ada
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 47 deletions.
13 changes: 0 additions & 13 deletions Charts/Classes/Charts/RadarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions Charts/Classes/Data/Interfaces/IRadarChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
83 changes: 50 additions & 33 deletions Charts/Classes/Renderers/RadarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,53 +343,70 @@ 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)
}
}
}

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)
}
Expand Down
14 changes: 13 additions & 1 deletion ChartsDemo/Classes/Demos/RadarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -153,7 +154,7 @@ - (void)optionTapped:(NSString *)key
_chartView.rotationEnabled = !_chartView.isRotationEnabled;
return;
}

if ([key isEqualToString:@"toggleFill"])
{
for (RadarChartDataSet *set in _chartView.data.dataSets)
Expand All @@ -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];
Expand Down
20 changes: 20 additions & 0 deletions ChartsRealm/Classes/Data/RealmRadarDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5c30ada

Please sign in to comment.