图表数据迅速

编程入门 行业动态 更新时间:2024-10-13 22:20:45
本文介绍了图表数据迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

几个月来,我一直在快速开发应用程序,但我不知道如何将数据传递到图表.因此,基本上,我所拥有的是一个内部带有数组的视图控制器以及一个包含图表的UIView.我想做的是UIView中的图形在ViewController中绘制数组.所以我想要的是将视图控制器中的数组dataMoneyTracker1中的数据传递到UIView中的数组graphPoints,或者我可以从uiview中访问视图控制器中的数组.

I've been developing apps in swift, for a few months, and I can't figure out how to pass data to a chart. So basically what I have is a view controller with an array inside, and a UIView, that contains the chart. What I want to do is that the graph in the UIView paints the array in the ViewController. So what I want is that the data from the array dataMoneyTracker1 in the view controller is passed to the array graphPoints in the UIView, or that I can access from the uiview to the array in the view controller.

感谢您的回答!

这是我的代码:

视图控制器

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

UIVIEW

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:

The UIViewController can be redefined with this variable:

@IBOutlet var View1: GonGraphUIView!

和 GonGraphUIView

var graphPoints:[Int]?

,然后在 drawRect

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

所以UIViewController可以说

so UIViewController can say

View1.graphPoints = dataMoneyTracker1 View1.setneedsDisplay()

更多推荐

图表数据迅速

本文发布于:2023-11-26 20:37:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图表   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!