Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapted ChartLegendRenderer class to upcoming Swift 3 changes and improved code readability #643

Merged
merged 6 commits into from
Jan 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 31 additions & 45 deletions Charts/Classes/Renderers/ChartLegendRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ChartLegendRenderer: ChartRendererBase
var colors = [UIColor?]()

// loop for building up the colors and labels used in the legend
for (var i = 0, count = data.dataSetCount; i < count; i++)
for i in 0..<data.dataSetCount
{
let dataSet = data.getDataSetByIndex(i)!

Expand All @@ -48,7 +48,7 @@ public class ChartLegendRenderer: ChartRendererBase
let bds = dataSet as! BarChartDataSet
var sLabels = bds.stackLabels

for (var j = 0; j < clrs.count && j < bds.stackSize; j++)
for j in 0..<min(clrs.count, bds.stackSize)
{
labels.append(sLabels[j % sLabels.count])
colors.append(clrs[j])
Expand All @@ -66,7 +66,7 @@ public class ChartLegendRenderer: ChartRendererBase
var xVals = data.xVals
let pds = dataSet as! PieChartDataSet

for (var j = 0; j < clrs.count && j < entryCount && j < xVals.count; j++)
for j in 0..<min(clrs.count, entryCount, xVals.count)
{
labels.append(xVals[j])
colors.append(clrs[j])
Expand All @@ -82,7 +82,7 @@ public class ChartLegendRenderer: ChartRendererBase
else
{ // all others

for (var j = 0; j < clrs.count && j < entryCount; j++)
for j in 0..<min(clrs.count, entryCount)
{
// if multiple colors are set for a DataSet, group them
if (j < clrs.count - 1 && j < entryCount - 1)
Expand Down Expand Up @@ -137,12 +137,13 @@ public class ChartLegendRenderer: ChartRendererBase

switch (legendPosition)
{
case .BelowChartLeft: fallthrough
case .BelowChartRight: fallthrough
case .BelowChartCenter: fallthrough
case .AboveChartLeft: fallthrough
case .AboveChartRight: fallthrough
case .AboveChartCenter:
case
.BelowChartLeft,
.BelowChartRight,
.BelowChartCenter,
.AboveChartLeft,
.AboveChartRight,
.AboveChartCenter:

let contentWidth: CGFloat = viewPortHandler.contentWidth

Expand Down Expand Up @@ -191,7 +192,8 @@ public class ChartLegendRenderer: ChartRendererBase

var lineIndex: Int = 0

for (var i = 0, count = labels.count; i < count; i++)

for i in 0..<labels.count
{
if (i < calculatedLabelBreakPoints.count && calculatedLabelBreakPoints[i])
{
Expand Down Expand Up @@ -250,15 +252,14 @@ public class ChartLegendRenderer: ChartRendererBase
}
}

break

case .PiechartCenter: fallthrough
case .RightOfChart: fallthrough
case .RightOfChartCenter: fallthrough
case .RightOfChartInside: fallthrough
case .LeftOfChart: fallthrough
case .LeftOfChartCenter: fallthrough
case .LeftOfChartInside:
case
.PiechartCenter,
.RightOfChart,
.RightOfChartCenter,
.RightOfChartInside,
.LeftOfChart,
.LeftOfChartCenter,
.LeftOfChartInside:

// contains the stacked legend size in pixels
var stack = CGFloat(0.0)
Expand Down Expand Up @@ -293,24 +294,18 @@ public class ChartLegendRenderer: ChartRendererBase
}
}

if (legendPosition == .RightOfChart ||
legendPosition == .LeftOfChart)
switch legendPosition
{
case .RightOfChart, .LeftOfChart:
posY = viewPortHandler.contentTop + yoffset
}
else if (legendPosition == .RightOfChartCenter ||
legendPosition == .LeftOfChartCenter)
{
case .RightOfChartCenter, .LeftOfChartCenter:
posY = viewPortHandler.chartHeight / 2.0 - _legend.neededHeight / 2.0
}
else /*if (legend.position == .RightOfChartInside ||
legend.position == .LeftOfChartInside)*/
{
default: // case .RightOfChartInside, .LeftOfChartInside
posY = viewPortHandler.contentTop + yoffset
}
}

for (var i = 0; i < labels.count; i++)
for i in 0..<labels.count
{
let drawingForm = colors[i] != nil
var x = posX
Expand Down Expand Up @@ -371,7 +366,6 @@ public class ChartLegendRenderer: ChartRendererBase
}
}

break
}
}

Expand All @@ -380,42 +374,34 @@ public class ChartLegendRenderer: ChartRendererBase
/// Draws the Legend-form at the given position with the color at the given index.
internal func drawForm(context context: CGContext, x: CGFloat, y: CGFloat, colorIndex: Int, legend: ChartLegend)
{
let formColor = legend.colors[colorIndex]

if (formColor === nil || formColor == UIColor.clearColor())
{
guard let formColor = legend.colors[colorIndex] where formColor != UIColor.clearColor() else {
return
}

let formsize = legend.formSize

CGContextSaveGState(context)
defer { CGContextRestoreGState(context) }

switch (legend.form)
{
case .Circle:
CGContextSetFillColorWithColor(context, formColor!.CGColor)
CGContextSetFillColorWithColor(context, formColor.CGColor)
CGContextFillEllipseInRect(context, CGRect(x: x, y: y - formsize / 2.0, width: formsize, height: formsize))
break
case .Square:
CGContextSetFillColorWithColor(context, formColor!.CGColor)
CGContextSetFillColorWithColor(context, formColor.CGColor)
CGContextFillRect(context, CGRect(x: x, y: y - formsize / 2.0, width: formsize, height: formsize))
break
case .Line:

CGContextSetLineWidth(context, legend.formLineWidth)
CGContextSetStrokeColorWithColor(context, formColor!.CGColor)
CGContextSetStrokeColorWithColor(context, formColor.CGColor)

_formLineSegmentsBuffer[0].x = x
_formLineSegmentsBuffer[0].y = y
_formLineSegmentsBuffer[1].x = x + formsize
_formLineSegmentsBuffer[1].y = y
CGContextStrokeLineSegments(context, _formLineSegmentsBuffer, 2)

break
}

CGContextRestoreGState(context)
}

/// Draws the provided label at the given position.
Expand Down