ios - 图表的数据 swift

标签 ios arrays swift uiview graph

我已经用 swift 开发应用程序几个月了,但我不知道如何将数据传递到图表。所以基本上我拥有的是一个 View Controller ,里面有一个数组,以及一个包含图表的 UIView。我想要做的是 UIView 中的图形绘制 ViewController 中的数组。所以我想要的是将 View Controller 中的数组 dataMoneyTracker1 中的数据传递到 UIView 中的数组 graphPoints,或者我可以从 uiview 访问 View Controller 中的数组。

感谢您的回答!

这是我的代码:

查看 Controller

import UIKit

class GonDetail2ViewController: UIViewController {
var dataMoneyTracker1 = [Int]()
@IBOutlet var View1: UIView!

}

UI View

import UIKit

class GonGraphUIView: UIView {


    var graphPoints = [Int]()

    override func drawRect(rect: CGRect) {
        let width = rect.width
        let height = rect.height

        let margin:CGFloat = 20.0
        let columnXPoint = { (column:Int) -> CGFloat in
            //Calculate gap between points
            let spacer = (width - margin*2 - 4) /
                CGFloat((self.graphPoints.count - 1))
            var x:CGFloat = CGFloat(column) * spacer
            x += margin + 2
            return x



    }

        let topBorder:CGFloat = 60
        let bottomBorder:CGFloat = 50
        let graphHeight = height - topBorder - bottomBorder
        let maxValue = graphPoints.maxElement()
        let columnYPoint = { (graphPoint:Int) -> CGFloat in
            var y:CGFloat = CGFloat(graphPoint) /
                CGFloat(maxValue!) * graphHeight
            y = graphHeight + topBorder - y // Flip the graph
            return y
        }

        UIColor.whiteColor().setFill()
        UIColor.whiteColor().setStroke()

        //set up the points line
        let graphPath = UIBezierPath()
        //go to start of line
        graphPath.moveToPoint(CGPoint(x:columnXPoint(0),
            y:columnYPoint(graphPoints[0])))

        //add points for each item in the graphPoints array
        //at the correct (x, y) for the point
        for i in 1..<graphPoints.count {
            let nextPoint = CGPoint(x:columnXPoint(i),
                y:columnYPoint(graphPoints[i]))
            graphPath.addLineToPoint(nextPoint)
        }

        graphPath.stroke()

        graphPath.lineWidth = 2.0
        graphPath.stroke()

        //Draw the circles on top of graph stroke
        for i in 0..<graphPoints.count {
            var point = CGPoint(x:columnXPoint(i), y:columnYPoint(graphPoints[i]))
            point.x -= 5.0/2
            point.y -= 5.0/2

            let circle = UIBezierPath(ovalInRect:
                CGRect(origin: point,
                    size: CGSize(width: 5.0, height: 5.0)))
            circle.fill()
        }

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
}

最佳答案

可以使用此变量重新定义 UIViewController:

@IBOutlet var View1: GonGraphUIView!

GonGraphUIView

var graphPoints:[Int]?

然后在drawRect

if let graphPoints = graphPoints {
    // draw the graph
} else { do nothing }

所以 UIViewController 可以说

View1.graphPoints = dataMoneyTracker1
View1.setneedsDisplay()

关于ios - 图表的数据 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35252175/

相关文章:

ios - 不同的图像名称

java - 更新 ArrayList 中 Array 的数组元素

javascript - 如何循环根据 div 中每个字母的位置修改文本的替换函数?

swift - 如何在 Swift 中从 UIPicker 填充文本字段

swift - 使用实时取景时如何更改 Xcode Playground Simulator 的大小?

ios - 无法在 Apple Developer Program 中添加设备

ios - 从 NSDictionary Swift 设置枚举

ios - XCode 9 在标签本地化评论版上崩溃

javascript - 在 react-native 中迭代 JSON 数组

swift - 为什么不在 Swift 中直接使用静态变量?