Skip to content

Commit

Permalink
Merge pull request #643 from zntfdr/master
Browse files Browse the repository at this point in the history
Adapted ChartLegendRenderer class to upcoming Swift 3 changes and improved code readability
  • Loading branch information
danielgindi committed Jan 8, 2016
2 parents 65e3ae9 + 48c28c8 commit 35379a2
Showing 1 changed file with 31 additions and 45 deletions.
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

0 comments on commit 35379a2

Please sign in to comment.