質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

2345閲覧

Swiftによる折れ線グラフ作成によるエラー

PYPP

総合スコア51

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/10/22 07:51

”[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}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

コピペして実行したところえらーにはなりませんでした、、、しかし何も表示されません。

本家のサイトをSwift3に移行したら問題なく表示されたのでそちらを載せておきます。
ViewControllerは問題ないと思います。

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 graphPoints = ["2000/2/3", "2000/3/3", "2000/4/3", "2000/5/3", "2000/6/3", "2000/7/3", "2000/8/3"] 17 graphDatas = [100, 30, 10, -50, 90, 12, 40] 18 19 GraphFrame() 20 MemoriGraphDraw() 21 } 22 23 //グラフを描画するviewの大きさ 24 func GraphFrame() { 25 self.backgroundColor = UIColor(red:0.972, green:0.973, blue:0.972, alpha:1) 26 self.frame = CGRect(x:10 , y: 0, width: checkWidth(), height: checkHeight()) 27 } 28 29 //横目盛・グラフを描画する 30 func MemoriGraphDraw() { 31 32 var count:CGFloat = 0 33 for memori in graphPoints { 34 35 let label = UILabel() 36 label.text = String(memori) 37 label.font = UIFont.systemFont(ofSize: 9) 38 39 //ラベルのサイズを取得 40 let frame = CGSize(width: 250, height: CGFloat.greatestFiniteMagnitude) 41 let rect = label.sizeThatFits(frame) 42 43 //ラベルの位置 44 var lebelX = (count * memoriMargin)-rect.width/2 45 46 //最初のラベル 47 if Int(count) == 0{ 48 lebelX = (count * memoriMargin) 49 } 50 51 //最後のラベル 52 if Int(count+1) == graphPoints.count{ 53 lebelX = (count * memoriMargin)-rect.width 54 } 55 56 label.frame = CGRect(x: lebelX , y: graphHeight, width: rect.width, height: rect.height) 57 self.addSubview(label) 58 59 count += 1 60 } 61 } 62 63 //グラフの線を描画 64 override func draw(_ rect: CGRect) { 65 66 var count:CGFloat = 0 67 let linePath = UIBezierPath() 68 var myCircle = UIBezierPath() 69 70 linePath.lineWidth = lineWidth 71 lineColor.setStroke() 72 73 for datapoint in graphDatas { 74 75 if Int(count+1) < graphDatas.count { 76 77 var nowY: CGFloat = datapoint/yAxisMax * (graphHeight - circleWidth) 78 nowY = graphHeight - nowY 79 80 if(graphDatas.min()!<0){ 81 nowY = (datapoint - graphDatas.min()!)/yAxisMax * (graphHeight - circleWidth) 82 nowY = graphHeight - nowY 83 } 84 85 //次のポイントを計算 86 var nextY: CGFloat = 0 87 nextY = graphDatas[Int(count+1)]/yAxisMax * (graphHeight - circleWidth) 88 nextY = graphHeight - nextY 89 90 if(graphDatas.min()!<0){ 91 nextY = (graphDatas[Int(count+1)] - graphDatas.min()!)/yAxisMax * (graphHeight - circleWidth) 92 nextY = graphHeight - nextY - circleWidth 93 } 94 95 //最初の開始地点を指定 96 var circlePoint:CGPoint = CGPoint() 97 if Int(count) == 0 { 98 linePath.move(to: CGPoint(x: count * memoriMargin + circleWidth, y: nowY)) 99 circlePoint = CGPoint(x: count * memoriMargin + circleWidth, y: nowY) 100 myCircle = UIBezierPath(arcCenter: circlePoint,radius: circleWidth,startAngle: 0.0,endAngle: CGFloat(M_PI*2),clockwise: false) 101 circleColor.setFill() 102 myCircle.fill() 103 myCircle.stroke() 104 } 105 106 //描画ポイントを指定 107 linePath.addLine(to: CGPoint(x: (count+1) * memoriMargin, y: nextY)) 108 109 //円をつくる 110 circlePoint = CGPoint(x: (count+1) * memoriMargin, y: nextY) 111 myCircle = UIBezierPath(arcCenter: circlePoint, 112 // 半径 113 radius: circleWidth, 114 // 初角度 115 startAngle: 0.0, 116 // 最終角度 117 endAngle: CGFloat(M_PI*2), 118 // 反時計回り 119 clockwise: false) 120 circleColor.setFill() 121 myCircle.fill() 122 myCircle.stroke() 123 124 } 125 count += 1 126 127 } 128 129 linePath.stroke() 130 } 131 132 // 保持しているDataの中で最大値と最低値の差を求める 133 var yAxisMax: CGFloat { 134 return graphDatas.max()!-graphDatas.min()! 135 } 136 137 //グラフ横幅を算出 138 func checkWidth() -> CGFloat{ 139 return CGFloat(graphPoints.count-1) * memoriMargin + (circleWidth * 2) 140 } 141 142 //グラフ縦幅を算出 143 func checkHeight() -> CGFloat{ 144 return graphHeight 145 } 146}

投稿2016/10/22 08:30

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

PYPP

2016/10/31 14:38

お礼が遅くなってすみません。回答していただき誠にありがとうございます。表示できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問