質問編集履歴

5

追記

2018/08/05 09:55

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,64 +1,4 @@
1
1
  ```swift
2
-
3
- import UIKit
4
-
5
- import Charts
6
-
7
-
8
-
9
- class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
10
-
11
-
12
-
13
- @IBOutlet weak var weight: UITextField!
14
-
15
- @IBOutlet weak var bodyfat: UITextField!
16
-
17
- @IBOutlet weak var chtChart: LineChartView!
18
-
19
- @IBOutlet weak var chartssegment: UISegmentedControl!
20
-
21
- @IBOutlet weak var mytableView: UITableView!
22
-
23
- var numbers : [Double] = []
24
-
25
- var bfpnumbers : [Double] = []
26
-
27
- var item = String()
28
-
29
-
30
-
31
- var dete = NSDate()
32
-
33
-
34
-
35
- @IBAction func okBtr(_ sender: Any) {
36
-
37
- let input = Double(weight.text!)
38
-
39
- let output = Double(bodyfat.text!)
40
-
41
-
42
-
43
- numbers.append(input!) //here we add the data to the array.
44
-
45
- bfpnumbers.append(output!)
46
-
47
-
48
-
49
- updateGraph()
50
-
51
- item.append("(numbers)" + "(bfpnumbers)")
52
-
53
- mytableView.reloadData()
54
-
55
- weight.text = ""
56
-
57
- bodyfat.text = ""
58
-
59
- }
60
-
61
-
62
2
 
63
3
  func updateGraph(){
64
4
 
@@ -204,188 +144,6 @@
204
144
 
205
145
  }
206
146
 
207
-
208
-
209
-
210
-
211
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
212
-
213
- return numbers.count
214
-
215
- }
216
-
217
-
218
-
219
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
220
-
221
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
222
-
223
-
224
-
225
- let dateFormatter = DateFormatter()
226
-
227
- dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale? // ロケールの設定
228
-
229
- dateFormatter.dateFormat = "yyyy/MM/dd "//:ss" // 日付フォーマットの設定
230
-
231
-
232
-
233
- let dateString = dateFormatter.string(from: dete as Date)
234
-
235
- print(dateString) // -> 2014/06/25 02:13:18*/
236
-
237
-
238
-
239
- let str = String("(numbers)" + "(bfpnumbers)")
240
-
241
-
242
-
243
- cell.textLabel?.text = dateString + ("(numbers[indexPath.row]) " + "¥ " + "(bfpnumbers[indexPath.row])") + " $"//str.description
244
-
245
-
246
-
247
- /*if numbers.count > 2 {
248
-
249
- numbers.remove(at: indexPath.row)
250
-
251
- mytableView.reloadData()
252
-
253
- }*/
254
-
255
-
256
-
257
- print(str)
258
-
259
- return cell
260
-
261
- }
262
-
263
-
264
-
265
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
266
-
267
- return 40
268
-
269
- }
270
-
271
-
272
-
273
- func tableView(_ tableView: UITableView,
274
-
275
- trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
276
-
277
- guard indexPath.row % 2 == 0 else { return UISwipeActionsConfiguration(actions: []) }
278
-
279
-
280
-
281
- let deleteAction = UIContextualAction(style: .destructive, title: "Delete")
282
-
283
- { action, view, completionHandler in
284
-
285
- self.numbers.remove(at: indexPath.row)
286
-
287
- self.bfpnumbers.remove(at: indexPath.row)
288
-
289
- self.mytableView.reloadData()
290
-
291
- completionHandler(true)
292
-
293
- }
294
-
295
-
296
-
297
- let edit = UIContextualAction(style: .normal,title: "edit", handler:
298
-
299
- { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in
300
-
301
- //self.showTextFieldAlert(.titleEdit, index: indexPath.section)
302
-
303
- success(true)
304
-
305
- })
306
-
307
-
308
-
309
- edit.backgroundColor = .blue
310
-
311
- let config = UISwipeActionsConfiguration(actions: [deleteAction,edit])
312
-
313
- config.performsFirstActionWithFullSwipe = false
314
-
315
- return config
316
-
317
- }
318
-
319
-
320
-
321
- override func viewDidLoad() {
322
-
323
- super.viewDidLoad()
324
-
325
-
326
-
327
- mytableView.delegate = self
328
-
329
- mytableView.dataSource = self
330
-
331
- //mytableView.separatorColor = UIColor.clear
332
-
333
- makeKeybord()
334
-
335
- self.weight.keyboardType = UIKeyboardType.decimalPad
336
-
337
- self.bodyfat.keyboardType = UIKeyboardType.decimalPad
338
-
339
-
340
-
341
- }
342
-
343
- //入力画面を閉じる時
344
-
345
- func makeKeybord(){
346
-
347
- // 仮のサイズでツールバー生成
348
-
349
- let kbToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 40))
350
-
351
- kbToolBar.barStyle = UIBarStyle.default // スタイルを設定
352
-
353
-
354
-
355
- kbToolBar.sizeToFit() // 画面幅に合わせてサイズを変更
356
-
357
-
358
-
359
- // スペーサー
360
-
361
- let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: self, action: nil)
362
-
363
-
364
-
365
- // 閉じるボタン
366
-
367
- let commitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(ViewController.commitButtonTapped))
368
-
369
-
370
-
371
- kbToolBar.items = [spacer, commitButton]
372
-
373
- weight.inputAccessoryView = kbToolBar
374
-
375
- bodyfat.inputAccessoryView = kbToolBar
376
-
377
- }
378
-
379
-
380
-
381
- @objc func commitButtonTapped (){
382
-
383
- self.view.endEditing(true)
384
-
385
- }
386
-
387
- }
388
-
389
147
  ```
390
148
 
391
149
 
@@ -403,11 +161,3 @@
403
161
  chtChart.xAxis = dateString
404
162
 
405
163
  ```
406
-
407
-
408
-
409
- ![横軸完成イメージ](128c35f032007922f8b69f640c9f925f.png)
410
-
411
-
412
-
413
- [リポジトリ](https://github.com/haruka22/swift-line-charts)

4

追記

2018/08/05 09:55

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,21 +1,5 @@
1
1
  ```swift
2
2
 
3
- //
4
-
5
- // ViewController.swift
6
-
7
- // ChartsApp
8
-
9
- //
10
-
11
- // Created by 西川継延 on 2018/07/24.
12
-
13
- // Copyright © 2018年 kei. All rights reserved.
14
-
15
- //
16
-
17
-
18
-
19
3
  import UIKit
20
4
 
21
5
  import Charts
@@ -423,3 +407,7 @@
423
407
 
424
408
 
425
409
  ![横軸完成イメージ](128c35f032007922f8b69f640c9f925f.png)
410
+
411
+
412
+
413
+ [リポジトリ](https://github.com/haruka22/swift-line-charts)

3

コードの追記

2018/08/04 14:30

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,10 +1,82 @@
1
1
  ```swift
2
2
 
3
+ //
4
+
5
+ // ViewController.swift
6
+
7
+ // ChartsApp
8
+
9
+ //
10
+
11
+ // Created by 西川継延 on 2018/07/24.
12
+
13
+ // Copyright © 2018年 kei. All rights reserved.
14
+
15
+ //
16
+
17
+
18
+
19
+ import UIKit
20
+
21
+ import Charts
22
+
23
+
24
+
25
+ class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
26
+
27
+
28
+
29
+ @IBOutlet weak var weight: UITextField!
30
+
31
+ @IBOutlet weak var bodyfat: UITextField!
32
+
33
+ @IBOutlet weak var chtChart: LineChartView!
34
+
35
+ @IBOutlet weak var chartssegment: UISegmentedControl!
36
+
37
+ @IBOutlet weak var mytableView: UITableView!
38
+
39
+ var numbers : [Double] = []
40
+
41
+ var bfpnumbers : [Double] = []
42
+
43
+ var item = String()
44
+
45
+
46
+
3
- var dete = NSDate()
47
+ var dete = NSDate()
48
+
49
+
50
+
4
-
51
+ @IBAction func okBtr(_ sender: Any) {
52
+
5
-
53
+ let input = Double(weight.text!)
54
+
6
-
55
+ let output = Double(bodyfat.text!)
56
+
57
+
58
+
59
+ numbers.append(input!) //here we add the data to the array.
60
+
61
+ bfpnumbers.append(output!)
62
+
63
+
64
+
65
+ updateGraph()
66
+
67
+ item.append("(numbers)" + "(bfpnumbers)")
68
+
69
+ mytableView.reloadData()
70
+
71
+ weight.text = ""
72
+
73
+ bodyfat.text = ""
74
+
75
+ }
76
+
77
+
78
+
7
- func updateGraph(){
79
+ func updateGraph(){
8
80
 
9
81
  var lineChartEntry = [ChartDataEntry]()//ここが折れ線グラフのキー
10
82
 
@@ -18,11 +90,21 @@
18
90
 
19
91
  let value = ChartDataEntry(x: Double(i), y: numbers[i])
20
92
 
93
+
94
+
95
+ //Y軸の右側の値について
96
+
97
+ //chtChart.rightAxis.enabled = true
98
+
99
+ //chtChart.leftAxis.drawGridLinesEnabled = false
100
+
21
101
 
22
102
 
23
103
  lineChartEntry.append(value)
24
104
 
25
-
105
+
106
+
107
+
26
108
 
27
109
  }
28
110
 
@@ -36,6 +118,14 @@
36
118
 
37
119
 
38
120
 
121
+ //Y軸の右側の値について
122
+
123
+ //chtChart.rightAxis.enabled = true
124
+
125
+ //chtChart.leftAxis.drawGridLinesEnabled = false
126
+
127
+
128
+
39
129
  lineChartEntry2.append(value2)
40
130
 
41
131
 
@@ -62,63 +152,255 @@
62
152
 
63
153
 
64
154
 
155
+ line1.circleRadius = 6
156
+
157
+ line1.circleColors = [UIColor.red]
158
+
159
+
160
+
161
+ line2.circleRadius = 5
162
+
163
+ line2.circleColors = [UIColor.blue]
164
+
165
+
166
+
167
+
168
+
169
+ let xAxis = chtChart.xAxis
170
+
171
+ xAxis.labelPosition = .bottom
172
+
173
+ xAxis.labelCount = numbers.count
174
+
175
+ xAxis.drawLabelsEnabled = true
176
+
177
+ xAxis.drawLimitLinesBehindDataEnabled = true
178
+
179
+ xAxis.avoidFirstLastClippingEnabled = true
180
+
181
+
182
+
183
+
184
+
185
+ //横軸を非表示
186
+
187
+ //chtChart.xAxis.enabled = true
188
+
189
+
190
+
191
+ chtChart.leftAxis.axisMaximum = 150 //y左軸最大値
192
+
193
+ chtChart.leftAxis.axisMinimum = 30 //y左軸最小値
194
+
195
+
196
+
197
+ chtChart.rightAxis.axisMaximum = 50 //y左軸最大値
198
+
199
+ chtChart.rightAxis.axisMinimum = 0 //y左軸最小値
200
+
201
+
202
+
203
+ chtChart.rightAxis.drawGridLinesEnabled = false
204
+
205
+
206
+
207
+ let data = LineChartData()
208
+
209
+ data.addDataSet(line1)
210
+
211
+ data.addDataSet(line2)
212
+
213
+
214
+
215
+ chtChart.data = data
216
+
217
+ chtChart.chartDescription?.text = "My awesome chart"
218
+
219
+
220
+
221
+ }
222
+
223
+
224
+
225
+
226
+
227
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
228
+
229
+ return numbers.count
230
+
231
+ }
232
+
233
+
234
+
235
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
236
+
237
+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
238
+
239
+
240
+
65
241
  let dateFormatter = DateFormatter()
66
242
 
67
243
  dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale? // ロケールの設定
68
244
 
69
- dateFormatter.dateFormat = "MM/dd"//:ss" // 日付フォーマットの設定
245
+ dateFormatter.dateFormat = "yyyy/MM/dd "//:ss" // 日付フォーマットの設定
70
246
 
71
247
 
72
248
 
73
249
  let dateString = dateFormatter.string(from: dete as Date)
74
250
 
75
- print(dateString) // -> 04/21
76
-
77
-
78
-
79
-
80
-
81
- //横軸を非表示 //エラー文
82
-
83
- chtChart.xAxis = dateString //Cannot assign to property: 'xAxis' is a get-only property
84
-
85
-
86
-
87
-
88
-
89
- chtChart.leftAxis.axisMaximum = 150 //y左軸最大値
90
-
91
- chtChart.leftAxis.axisMinimum = 30 //y左軸最小値
92
-
93
-
94
-
95
- chtChart.rightAxis.axisMaximum = 50 //y左軸最大値
96
-
97
- chtChart.rightAxis.axisMinimum = 0 //y左軸最小値
98
-
99
-
100
-
101
-
102
-
103
- chtChart.rightAxis.drawGridLinesEnabled = false
104
-
105
-
106
-
107
- let data = LineChartData()
108
-
109
- data.addDataSet(line1)
110
-
111
- data.addDataSet(line2)
112
-
113
-
114
-
115
- chtChart.data = data
116
-
117
- chtChart.chartDescription?.text = "My awesome chart"
118
-
119
-
120
-
121
- }
251
+ print(dateString) // -> 2014/06/25 02:13:18*/
252
+
253
+
254
+
255
+ let str = String("(numbers)" + "(bfpnumbers)")
256
+
257
+
258
+
259
+ cell.textLabel?.text = dateString + ("(numbers[indexPath.row]) " + "¥ " + "(bfpnumbers[indexPath.row])") + " $"//str.description
260
+
261
+
262
+
263
+ /*if numbers.count > 2 {
264
+
265
+ numbers.remove(at: indexPath.row)
266
+
267
+ mytableView.reloadData()
268
+
269
+ }*/
270
+
271
+
272
+
273
+ print(str)
274
+
275
+ return cell
276
+
277
+ }
278
+
279
+
280
+
281
+ func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
282
+
283
+ return 40
284
+
285
+ }
286
+
287
+
288
+
289
+ func tableView(_ tableView: UITableView,
290
+
291
+ trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
292
+
293
+ guard indexPath.row % 2 == 0 else { return UISwipeActionsConfiguration(actions: []) }
294
+
295
+
296
+
297
+ let deleteAction = UIContextualAction(style: .destructive, title: "Delete")
298
+
299
+ { action, view, completionHandler in
300
+
301
+ self.numbers.remove(at: indexPath.row)
302
+
303
+ self.bfpnumbers.remove(at: indexPath.row)
304
+
305
+ self.mytableView.reloadData()
306
+
307
+ completionHandler(true)
308
+
309
+ }
310
+
311
+
312
+
313
+ let edit = UIContextualAction(style: .normal,title: "edit", handler:
314
+
315
+ { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in
316
+
317
+ //self.showTextFieldAlert(.titleEdit, index: indexPath.section)
318
+
319
+ success(true)
320
+
321
+ })
322
+
323
+
324
+
325
+ edit.backgroundColor = .blue
326
+
327
+ let config = UISwipeActionsConfiguration(actions: [deleteAction,edit])
328
+
329
+ config.performsFirstActionWithFullSwipe = false
330
+
331
+ return config
332
+
333
+ }
334
+
335
+
336
+
337
+ override func viewDidLoad() {
338
+
339
+ super.viewDidLoad()
340
+
341
+
342
+
343
+ mytableView.delegate = self
344
+
345
+ mytableView.dataSource = self
346
+
347
+ //mytableView.separatorColor = UIColor.clear
348
+
349
+ makeKeybord()
350
+
351
+ self.weight.keyboardType = UIKeyboardType.decimalPad
352
+
353
+ self.bodyfat.keyboardType = UIKeyboardType.decimalPad
354
+
355
+
356
+
357
+ }
358
+
359
+ //入力画面を閉じる時
360
+
361
+ func makeKeybord(){
362
+
363
+ // 仮のサイズでツールバー生成
364
+
365
+ let kbToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 40))
366
+
367
+ kbToolBar.barStyle = UIBarStyle.default // スタイルを設定
368
+
369
+
370
+
371
+ kbToolBar.sizeToFit() // 画面幅に合わせてサイズを変更
372
+
373
+
374
+
375
+ // スペーサー
376
+
377
+ let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: self, action: nil)
378
+
379
+
380
+
381
+ // 閉じるボタン
382
+
383
+ let commitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(ViewController.commitButtonTapped))
384
+
385
+
386
+
387
+ kbToolBar.items = [spacer, commitButton]
388
+
389
+ weight.inputAccessoryView = kbToolBar
390
+
391
+ bodyfat.inputAccessoryView = kbToolBar
392
+
393
+ }
394
+
395
+
396
+
397
+ @objc func commitButtonTapped (){
398
+
399
+ self.view.endEditing(true)
400
+
401
+ }
402
+
403
+ }
122
404
 
123
405
  ```
124
406
 

2

画像の変更

2018/08/04 14:25

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -140,4 +140,4 @@
140
140
 
141
141
 
142
142
 
143
- ![横軸イメージ](e95bb069762d5bd8fe8d082badb0ab44.png)
143
+ ![横軸完成イメージ](128c35f032007922f8b69f640c9f925f.png)

1

画像の追記

2018/08/04 14:09

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -137,3 +137,7 @@
137
137
  chtChart.xAxis = dateString
138
138
 
139
139
  ```
140
+
141
+
142
+
143
+ ![横軸のイメージ](e95bb069762d5bd8fe8d082badb0ab44.png)