title: Drawing linechart in Swift with 1000+ datapoints
body: <p>So I have Custom view an I'm trying to draw line chart there. First I draw xGrid, yGrid and middleline. Here are my functions to do that:</p>
  
  <pre><code>var width: CGFloat = 400
  var height: CGFloat = 200
  var xLines: CGFloat = 20
  var yLines: CGFloat = 10
  let color = NSColor.blackColor()
  
  func drawMiddleLine() {
      let middleHeight = height / 2
      var context = NSGraphicsContext.currentContext()?.CGContext
      CGContextSetStrokeColorWithColor(context, color.CGColor)
      CGContextSetLineWidth(context, 2)
      CGContextMoveToPoint(context, 0, middleHeight)
      CGContextAddLineToPoint(context, width, middleHeight)
      CGContextStrokePath(context)
  }
  
  func drawYGrid() {
      let space = height / yLines
      var context = NSGraphicsContext.currentContext()?.CGContext
      CGContextSetStrokeColorWithColor(context, color.CGColor)
      for index in 0...Int(yLines) {
          CGContextMoveToPoint(context, width, (CGFloat(index) * space))
          CGContextAddLineToPoint(context, 0, (CGFloat(index) * space))
      }
      CGContextStrokePath(context)
  }
  
  func drawXGrid() {
      let space = width / xLines
      var context = NSGraphicsContext.currentContext()?.CGContext
      CGContextSetStrokeColorWithColor(context, color.CGColor)
      for index in 0...Int(xLines) {
          CGContextMoveToPoint(context, (CGFloat(index) * space), self.bounds.height)
          CGContextAddLineToPoint(context, CGFloat(index) * space, 0)
      }
      CGContextStrokePath(context)
  }
  </code></pre>
  
  <p>Now I have basic grid which width is two times height. Now I want to scale my y-axis, so I'm going to scale my data (In this example, I only take max positive number):</p>
  
  <pre><code>func getMaxValue(data: Array&lt;CGFloat&gt;) -&gt;CGFloat {
      let max = maxElement(data)
      return max
  }
  </code></pre>
  
  <p>And now I scale my y-Axis:</p>
  
  <pre><code>func scaleYAxis(data: Array&lt;CGFloat&gt;) -&gt;Array&lt;CGFloat&gt; {
      let maxValue = getMaxValue(data)
      var factor = height / maxValue / 4
      var scaledY = data.map({datum -&gt; CGFloat in
          var newValue = datum * factor
          return newValue
          })
      return scaledY
  }
  </code></pre>
  
  <p>But when I'm drawing my line with too many datapoints, my drawing will get messed up, because there is too many datapoints. It's ok when there is  about 50 datapoints. </p>
  
  <p>For example, I want to get something like this in results: <a href="http://canvasjs.com/editor/?id=http://canvasjs.com/example/gallery/line/100000datapoints/" rel="nofollow">JavaScript linechart with many many datapoints</a></p>
  
  <p>Any ideas how can I manage to get that in Swift?</p>
  
  <p>And my drawing method is something like that:</p>
  
  <pre><code>func drawLine(data: Array&lt;CGFloat&gt;) {
      var path = CGPathCreateMutable()
      var middleHeight = height / 2
      CGPathMoveToPoint(path, nil, 0, middleHeight)
      var scaledY = sclaleYAxis(data)
  
      for index in 0..&lt;data.count {
          var xSpot = data[index]
          var ySpot = middleHeight + scaledY[index]
  
          CGPathAddLineToPoint(path, nil, xSpot, ySpot)
      }
      var layer = CAShapeLayer()
      layer.frame = self.bounds
      layer.path = path
      layer.strokeColor = NSColor.greenColor().CGColor
      layer.fillColor = nil
      layer.lineWidth = 3
      self.layer?.addSublayer(layer)
  
      // I Have lineLayerStore and I delete all lines when it is needed
      lineLayerStore.append(layer)
  }
  </code></pre>