Created
April 14, 2010 05:15
-
-
Save justingraves/365476 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)drawRect:(CPRect)aRect | |
{ | |
if(needsLayout) | |
{ | |
[self layoutSubviews]; | |
needsLayout = NO; | |
} | |
[super drawRect:aRect]; | |
var start = [[CPDate alloc] init]; | |
var context = [[CPGraphicsContext currentContext] graphicsPort]; | |
// Gradient background | |
if(!overlayMessage) | |
{ | |
CGContextSaveGState(context); | |
var myStartPoint = CPPointMake(0,0); | |
var myEndPoint = CPPointMake(0,chartBounds.size.height); | |
CGContextClipToRect(context, chartBounds); | |
CGContextDrawLinearGradient(context, bgGradient, myStartPoint, myEndPoint, 0); | |
CGContextRestoreGState(context); | |
} | |
// Draw separator lines | |
CGContextSetLineWidth(context, 1.0); | |
[[CPColor colorWithCalibratedRed:0.5 green:0.5 blue:0.5 alpha:0.2] setStroke]; | |
for(var h = 0.25; h < 0.99; h += 0.25) | |
[CPBezierPath strokeLineFromPoint:CPPointMake(Math.round(pointPadding.x), Math.round(chartBounds.size.height * h)) toPoint:CPPointMake(Math.round(chartBounds.size.width - pointPadding.x), Math.round(chartBounds.size.height * h))]; | |
for(var w = 0.2; w < 0.99; w += 0.2) | |
[CPBezierPath strokeLineFromPoint:CPPointMake(Math.round((w * chartBounds.size.width) + chartBounds.origin.x), Math.round(chartBounds.origin.y + chartBounds.size.height - pointPadding.y)) | |
toPoint:CPPointMake(Math.round((w * chartBounds.size.width) + chartBounds.origin.x), Math.round(chartBounds.origin.y + pointPadding.y))]; | |
// Shadow | |
//CGContextSetShadowWithColor(context, 3.0, 3.0, [CPColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1]); | |
// Lines for data | |
var colorIndex = 0; | |
var trueMarkerSize = Math.max(dataMarkerSize, lineThickness); | |
for(var a=0; a<numberOfLines; a++) | |
{ | |
if(!lineStatus[a]) | |
{ | |
colorIndex++; | |
if(colorIndex >= [SR_LINE_CHART_DEFAULT_LINE_COLORS count]) | |
colorIndex = 0; | |
continue; | |
} | |
//[[CPColor colorWithCalibratedRed:0.4 green:0.4 blue:1.0 alpha:1.0] setStroke]; | |
[SR_LINE_CHART_DEFAULT_LINE_COLORS[colorIndex] setStroke]; | |
var white = [CPColor whiteColor]; | |
if(lineThickness > 0.1) | |
{ | |
CGContextSaveGState(context); | |
CGContextBeginPath(context); | |
CGContextSetLineWidth(context, lineThickness); | |
var usedPoints = 0; | |
var finalPointIndex = [pointsTranslated[a] count]-1; | |
var lastX = 0; | |
for(var i=0; i<=finalPointIndex; i++) | |
{ | |
if(i < finalPointIndex) | |
{ | |
if(pointsTranslated[a][i+1].x - horizontalOffset < 0) | |
continue; | |
} | |
if(lastX > chartBounds.size.width) | |
continue; | |
var baseX = pointsTranslated[a][i].x - horizontalOffset; | |
//if(usedPoints) | |
CGContextAddLineToPoint(context, baseX, pointsTranslated[a][i].y); | |
//else | |
CGContextMoveToPoint(context, baseX, pointsTranslated[a][i].y); | |
usedPoints++; | |
lastX = baseX; | |
} | |
if(usedPoints) | |
CGContextStrokePath(context); | |
CGContextRestoreGState(context); | |
} | |
// Draw circle markers on points | |
if(trueMarkerSize >= 3.0) | |
{ | |
var tms2 = trueMarkerSize/2; | |
[SR_LINE_CHART_DEFAULT_STROKE_COLORS[colorIndex] setStroke]; | |
if(dataMarkerSize >= 1) | |
[white setFill];//[SR_LINE_CHART_DEFAULT_DOT_COLORS[colorIndex] setFill]; | |
else | |
[SR_LINE_CHART_DEFAULT_LINE_COLORS[colorIndex] setFill]; | |
CGContextSetLineWidth(context, dataMarkerStroke); | |
for(var i=0; i<[pointsTranslated[a] count]; i++) | |
{ | |
var baseX = pointsTranslated[a][i].x - horizontalOffset; | |
if(baseX + trueMarkerSize < 0) | |
continue; | |
else if(baseX - trueMarkerSize > chartBounds.size.width) | |
continue; | |
var baseY = pointsTranslated[a][i].y; | |
var aRect = CPRectMake(baseX-tms2, baseY-tms2, trueMarkerSize, trueMarkerSize); | |
CGContextBeginPath(context); | |
CGContextAddEllipseInRect(context, aRect); | |
CGContextFillPath(context); | |
if(dataMarkerStroke > 0.1) | |
CGContextStrokePath(context); | |
} | |
} | |
colorIndex++; | |
if(colorIndex >= [SR_LINE_CHART_DEFAULT_LINE_COLORS count]) | |
colorIndex = 0; | |
} | |
if(overlayMessage) | |
{ | |
var overlay = [CPColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.25]; | |
[overlay setFill]; | |
[CPBezierPath fillRect:chartBounds]; | |
} | |
// Border | |
var dataBorderWidth = 2; | |
var dataBorderHalf = dataBorderWidth/2; | |
var border = CPRectMake(chartBounds.origin.x + dataBorderHalf, chartBounds.origin.y + dataBorderHalf, chartBounds.size.width - dataBorderWidth, chartBounds.size.height - dataBorderWidth); | |
[[CPColor colorWithCalibratedRed:0.7 green:0.7 blue:0.7 alpha:1.0] setStroke]; | |
CGContextSetLineWidth(context, dataBorderWidth); | |
[CPBezierPath strokeRect:border]; | |
var end = [[CPDate alloc] init]; | |
totalRenderTime += [end timeIntervalSinceDate:start]; | |
totalRenderFrames++; | |
//if(totalRenderFrames % 100 == 0) | |
// alert("Average render time: " + (totalRenderTime/totalRenderFrames) + "\nFPS: " + (totalRenderFrames/totalRenderTime)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment