在iOS中绘制线图

时间:2016-04-04 07:10:08

标签: ios

我想在附图中实现图形功能。任何人都可以建议它的框架。

现在正在使用以下代码,但无法使用x,y轴绘制线条。  下面是用于绘制线条的示例代码,      lineChart.addLine(data5)//这里data5是一个动态包含x值的数组,y值从零开始是静态的。现在我想让y值也是动态的。

addLine方法将drawline函数调用为:

        func drawLine(lineIndex: Int) {

    var data = self.dataStore[lineIndex]
    let path = UIBezierPath()

    var xValue = self.x.scale(0) + x.axis.inset
    var yValue = self.bounds.height - self.y.scale(data[0]) - y.axis.inset
    path.moveToPoint(CGPoint(x: xValue, y: yValue))
    for index in 1..<data.count {
        xValue = self.x.scale(CGFloat(index)) + x.axis.inset
        yValue = self.bounds.height - self.y.scale(data[index]) - y.axis.inset
        path.addLineToPoint(CGPoint(x: xValue, y: yValue))
    }

    let layer = CAShapeLayer()
    layer.frame = self.bounds
    layer.path = path.CGPath
    layer.strokeColor = colors[lineIndex].CGColor
    layer.fillColor = nil
    layer.lineWidth = lineWidth
    self.layer.addSublayer(layer)

    // animate line drawing
    if animation.enabled {
        let anim = CABasicAnimation(keyPath: "strokeEnd")
        anim.duration = animation.duration
        anim.fromValue = 0
        anim.toValue = 1
        layer.addAnimation(anim, forKey: "strokeEnd")
    }

    // add line layer to store
    lineLayerStore.append(layer)
}

enter image description here

先谢谢。

2 个答案:

答案 0 :(得分:2)

您可以使用Charts。但是,自己绘制这些线条并不难。

答案 1 :(得分:1)

你可以使用UIBezierPath绘制这样的图表。请在该课程中深入了解你会发现很多东西来创建这样的折线图。