Skip to content

Commit

Permalink
Slight performance improvement for linear line chart (issue #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Apr 23, 2015
1 parent 2816bce commit ffeb9dc
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions Charts/Classes/Renderers/LineChartRenderer.swift
Expand Up @@ -272,6 +272,11 @@ public class LineChartRenderer: ChartDataRendererBase
// more than 1 color
if (dataSet.colors.count > 1)
{
if (_lineSegments.count != 2)
{
_lineSegments = [CGPoint](count: 2, repeatedValue: CGPoint());
}

for (var j = minx, count = Int(ceil(CGFloat(maxx - minx) * phaseX + CGFloat(minx))); j < count; j++)
{
if (count > 1 && j == count - 1)
Expand Down Expand Up @@ -318,28 +323,30 @@ public class LineChartRenderer: ChartDataRendererBase
else
{ // only one color per dataset

var point = CGPoint();
point.x = CGFloat(entries[minx].xIndex);
point.y = CGFloat(entries[minx].value) * phaseY;
point = CGPointApplyAffineTransform(point, valueToPixelMatrix)
var e1: ChartDataEntry!;
var e2: ChartDataEntry!;

if (_lineSegments.count != max((entries.count - 1) * 2, 2))
{
_lineSegments = [CGPoint](count: max((entries.count - 1) * 2, 2), repeatedValue: CGPoint());
}

CGContextBeginPath(context);
CGContextMoveToPoint(context, point.x, point.y);
e1 = entries[minx];

// create a new path
for (var x = minx + 1, count = Int(ceil(CGFloat(maxx - minx) * phaseX + CGFloat(minx))); x < count; x++)
var count = Int(ceil(CGFloat(maxx - minx) * phaseX + CGFloat(minx)));

for (var x = count > 1 ? minx + 1 : minx, j = 0; x < count; x++)
{
var e = entries[x];

point.x = CGFloat(e.xIndex);
point.y = CGFloat(e.value) * phaseY;
point = CGPointApplyAffineTransform(point, valueToPixelMatrix)
e1 = entries[x == 0 ? 0 : (x - 1)];
e2 = entries[x];

CGContextAddLineToPoint(context, point.x, point.y);
_lineSegments[j++] = CGPointApplyAffineTransform(CGPoint(x: CGFloat(e1.xIndex), y: CGFloat(e1.value) * phaseY), valueToPixelMatrix);
_lineSegments[j++] = CGPointApplyAffineTransform(CGPoint(x: CGFloat(e2.xIndex), y: CGFloat(e2.value) * phaseY), valueToPixelMatrix);
}

var size = max((count - minx - 1) * 2, 2)
CGContextSetStrokeColorWithColor(context, dataSet.colorAt(0).CGColor);
CGContextStrokePath(context);
CGContextStrokeLineSegments(context, _lineSegments, size);
}

CGContextRestoreGState(context);
Expand Down

0 comments on commit ffeb9dc

Please sign in to comment.