”[Swift] オリジナルでシンプルな折れ線グラフをつくる(http://qiita.com/iritec/items/ced95fba982714d25b13)”を参考にして、折れ線グラフを表示させるアプリケーションを作成しようとしています。
しかし、Swift3に移行したことからなのか、”EXC_BAD_INSTRUCTION ”というエラーが出てしまいました。どのようにしたら解消できるのかわかりません。説明不足で誠に申し訳ありません、お答えいただけたら幸いです。
【ViewController.swift】
Swift
1import UIKit 2 3class ViewController: UIViewController { 4 @IBOutlet weak var scview: UIScrollView! 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 let graphview = Graph() 8 scview.addSubview(graphview) 9 graphview.drawLineGraph() 10 scview.contentSize = CGSize(width:graphview.checkWidth()+20, height:graphview.checkHeight()) 11 } 12}
【Graph.swift】
Swift
1import UIKit 2 3class Graph: UIView { 4 5 var lineWidth:CGFloat = 3.0 //グラフ線の太さ 6 var lineColor:UIColor = UIColor(red:0.088, green:0.501, blue:0.979, alpha:1) //グラフ線の色 7 var circleWidth:CGFloat = 4.0 //円の半径 8 var circleColor:UIColor = UIColor(red:0.088, green:0.501, blue:0.979, alpha:1) //円の色 9 10 var memoriMargin: CGFloat = 70 //横目盛の感覚 11 var graphHeight: CGFloat = 300 //グラフの高さ 12 var graphPoints: [String] = [] 13 var graphDatas: [CGFloat] = [] 14 15 func drawLineGraph() 16 { 17 graphPoints = ["2000/2/3", "2000/3/3", "2000/4/3", "2000/5/3", "2000/6/3", "2000/7/3", "2000/8/3"] 18 graphDatas = [100, 30, 10, -50, 90, 12, 40] 19 20 GraphFrame() 21 MemoriGraphDraw() 22 } 23 24 //グラフを描画するviewの大きさ 25 func GraphFrame(){ 26 self.backgroundColor = UIColor(red:0.972, green:0.973, blue:0.972, alpha:1) 27 // self.frame = CGRectMake(10 , 0, checkWidth(), checkHeight()) 28 let frame = CGRect(origin: CGPoint(x: 10,y :0), size: CGSize(width: checkWidth(), height: checkHeight())) 29 } 30 31 //横目盛・グラフを描画する 32 func MemoriGraphDraw() { 33 34 var count:CGFloat = 0 35 for memori in graphPoints { 36 37 let label = UILabel() 38 label.text = String(memori) 39 label.font = UIFont.systemFont(ofSize: 9) 40 41 //ラベルのサイズを取得 42 // let frame = CGSizeMake(250, CGFloat.greatestFiniteMagnitude) 43 let frame = CGSize(width: 250, height: CGFloat.greatestFiniteMagnitude) 44 let rect = label.sizeThatFits(frame) 45 46 //ラベルの位置 47 var lebelX = (count * memoriMargin)-rect.width/2 48 49 //最初のラベル 50 if Int(count) == 0{ 51 lebelX = (count * memoriMargin) 52 } 53 54 55 //最後のラベル 56 if Int(count+1) == graphPoints.count{ 57 lebelX = (count * memoriMargin)-rect.width 58 } 59 60 // label.frame = CGRectMake(lebelX , graphHeight, rect.width, rect.height) 61 _ = CGRect(x: lebelX, y: graphHeight, width: rect.width, height: rect.height) 62 self.addSubview(label) 63 64 count += 1 65 } 66 } 67 68 //グラフの線を描画 69 override func draw(_ rect: CGRect) { 70 71 var count:CGFloat = 0 72 let linePath = UIBezierPath() 73 var myCircle = UIBezierPath() 74 75 linePath.lineWidth = lineWidth 76 lineColor.setStroke() 77 78 for datapoint in graphDatas { 79 80 if Int(count+1) < graphDatas.count { 81 82 var nowY: CGFloat = datapoint/yAxisMax * (graphHeight - circleWidth) 83 nowY = graphHeight - nowY 84 85 if(graphDatas.min()!<0){ 86 nowY = (datapoint - graphDatas.min()!)/yAxisMax * (graphHeight - circleWidth) 87 nowY = graphHeight - nowY 88 } 89 90 //次のポイントを計算 91 var nextY: CGFloat = 0 92 nextY = graphDatas[Int(count+1)]/yAxisMax * (graphHeight - circleWidth) 93 nextY = graphHeight - nextY 94 95 if(graphDatas.min()!<0){ 96 nextY = (graphDatas[Int(count+1)] - graphDatas.min()!)/yAxisMax * (graphHeight - circleWidth) 97 nextY = graphHeight - nextY - circleWidth 98 } 99 100 //最初の開始地点を指定 101 var circlePoint:CGPoint = CGPoint() 102 if Int(count) == 0 { 103 linePath.move(to: CGPoint(x: count * memoriMargin + circleWidth, y: nowY)) 104 circlePoint = CGPoint(x: count * memoriMargin + circleWidth, y: nowY) 105 myCircle = UIBezierPath(arcCenter: circlePoint,radius: circleWidth,startAngle: 0.0,endAngle: CGFloat(M_PI*2),clockwise: false) 106 circleColor.setFill() 107 myCircle.fill() 108 myCircle.stroke() 109 } 110 111 //描画ポイントを指定 112 linePath.addLine(to: CGPoint(x: (count+1) * memoriMargin, y: nextY)) 113 114 //円をつくる 115 circlePoint = CGPoint(x: (count+1) * memoriMargin, y: nextY) 116 myCircle = UIBezierPath(arcCenter: circlePoint, 117 // 半径 118 radius: circleWidth, 119 // 初角度 120 startAngle: 0.0, 121 // 最終角度 122 endAngle: CGFloat(M_PI*2), 123 // 反時計回り 124 clockwise: false) 125 circleColor.setFill() 126 myCircle.fill() 127 myCircle.stroke() 128 129 } 130 131 count += 1 132 133 } 134 135 linePath.stroke() 136 137 138 } 139 140 // 保持しているDataの中で最大値と最低値の差を求める 141 var yAxisMax: CGFloat { 142 return graphDatas.max()!-graphDatas.min()! 143 } 144 145 //グラフ横幅を算出 146 func checkWidth() -> CGFloat{ 147 return CGFloat(graphPoints.count-1) * memoriMargin + (circleWidth * 2) 148 } 149 150 //グラフ縦幅を算出 151 func checkHeight() -> CGFloat{ 152 return graphHeight 153 } 154}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/31 14:38