Search code examples
swiftnsstringcgcontextquartz-core

Debugging code:: How to colors lines in CGContext?


The following code has an problem, for some reason the radial lines are being drawn black when they should be drawn in UIColor.yellowColor()

import UIKit
import QuartzCore


let π:CGFloat = CGFloat(M_PI)

class CircularPlotView:UIView {

  var lineWidth:CGFloat = 1.5 { didSet { setNeedsDisplay() } }
  var edgeColor:UIColor = UIColor.yellowColor() { didSet { setNeedsDisplay() } }
  var fillColor:UIColor = UIColor.orangeColor() { didSet { setNeedsDisplay() } }
  var fieldColor:UIColor = UIColor.yellowColor(){ didSet { setNeedsDisplay() } }
  var lineColor:UIColor = UIColor.blueColor(){ didSet { setNeedsDisplay() } }
  var scale: CGFloat = 0.90 { didSet { setNeedsDisplay() } }
  //var plotCenter:CGPoint { return convertPoint(center, fromView: superview) }
  var plotCenter:CGPoint {return CGPoint(x: 190,y: 316) }
  var plotRadius:CGFloat {return min(bounds.size.width, bounds.size.height) / 8 * scale }
  var diameterX:CGFloat = 100.0
  var radiusX:CGFloat {return diameterX * 0.5}
  var scaleFactorDB:CGFloat = 5.0
  var offsetDB:CGFloat = 0.0

  //data to plot
  var dataString:[String] = ["cat","dog","pizza"]
  var dataDB:[Float] = [11.0,9.0,3.5]

  var fontSize:CGFloat = 17.0

  override func drawRect(rect: CGRect) {

    let context = UIGraphicsGetCurrentContext()

    let fieldFont = UIFont.systemFontOfSize(fontSize)

    let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.alloc()
    paragraphStyle.lineHeightMultiple = 1
    paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping

    let attributes:Dictionary = [
      NSParagraphStyleAttributeName : paragraphStyle,
      NSForegroundColorAttributeName: fieldColor,
      NSFontAttributeName: fieldFont
    ]


    var theta:CGFloat = 1.5 * π //CGFloat(M_PI)
    var radius:CGFloat = 0.0
    //var s:NSString = "ERROR FIX DATABASE" //if shown error in DB

    let startTime = CFAbsoluteTimeGetCurrent()

    //MARK:== draw strings ==
    //with enough separation so they don't clobber each other
    var myAttributedString:NSMutableAttributedString!

    for (var i=0; i < dataDB.count; i++)
    {
      //s = NSString(string: dataString[i])
      myAttributedString = NSMutableAttributedString (string: dataString[i], attributes:attributes)

      theta =  1.97 * π * ( CGFloat(i) / (CGFloat(dataDB.count) ) )
      radius =  ( radiusX + 10.0 +  offsetDB + scaleFactorDB*CGFloat(dataDB.reduce(dataDB[0], { max($0, $1) })) )

      CGContextSaveGState(context)
      CGContextTranslateCTM(context,plotCenter.x ,plotCenter.y )
      CGContextRotateCTM(context, theta)
     CGContextSetStrokeColorWithColor(context,UIColor.yellowColor().CGColor)
      CGContextMoveToPoint(context,0.0,0.0)
      CGContextAddLineToPoint(context, radiusX + scaleFactorDB*CGFloat(dataDB[i]) + offsetDB, 0.0 )
      myAttributedString.drawAtPoint(CGPoint(x: radius , y: 0 ))
      CGContextRestoreGState(context)
    }

    let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
    println("Time elapsed loop through lines and strings: \(timeElapsed) s")

    CGContextStrokePath(context)

    //MARK:== draw oval, fill, edge ==
    let sunPath = UIBezierPath(ovalInRect: CGRectMake(
      plotCenter.x - radiusX,
      plotCenter.y - radiusX,
      diameterX, diameterX)
    )

    sunPath.lineWidth = lineWidth

    edgeColor.set()
    sunPath.stroke()

    fillColor.setFill()
    sunPath.fill()

  }

Solution

  • Was

      CGContextSaveGState(context)
      CGContextTranslateCTM(context,plotCenter.x ,plotCenter.y )
      CGContextRotateCTM(context, theta)
      CGContextMoveToPoint(context,0.0,0.0)
      CGContextAddLineToPoint(context, radiusX + scaleFactorDB*CGFloat(dataDB[i]) + offsetDB, 0.0 )
      myAttributedString.drawAtPoint(CGPoint(x: radius , y: 0 ))
      CGContextRestoreGState(context)
    

    corrected

      CGContextSaveGState(context)
      CGContextTranslateCTM(context,plotCenter.x ,plotCenter.y )
      CGContextRotateCTM(context, theta)
      CGContextSetStrokeColorWithColor(context,lineColor.CGColor)
      CGContextMoveToPoint(context,0.0,0.0)
      CGContextAddLineToPoint(context, radiusX + scaleFactorDB*CGFloat(dataDB[i]) + offsetDB, 0.0 )
      CGContextStrokePath(context)
      myAttributedString.drawAtPoint(CGPoint(x: radius , y: 0 ))
      CGContextRestoreGState(context)