質問編集履歴

1

ソースコードの追加

2015/12/05 14:07

投稿

gurennpaku
gurennpaku

スコア12

test CHANGED
File without changes
test CHANGED
@@ -15,3 +15,225 @@
15
15
 
16
16
 
17
17
  ![![イメージ説明](7fdf08c72274cd24b245bf9bddac00fe.png)](b89e327035153b24bb12e2a891252fa5.png)
18
+
19
+
20
+
21
+ グラフのソースコードを追加します
22
+
23
+ ```Objective-C
24
+
25
+ - (void)viewDidLoad
26
+
27
+ {
28
+
29
+ [super viewDidLoad];
30
+
31
+ self.scatterPlotData = [NSMutableArray array];
32
+
33
+
34
+
35
+ for ( NSUInteger i = 0; i < 11; i++ ) {
36
+
37
+ NSNumber *x = [NSNumber numberWithDouble:i];
38
+
39
+ NSNumber *y = [NSNumber numberWithDouble:(int)(rand() / (double)RAND_MAX * 10)]; // 1〜10の値のランダム値(int)
40
+
41
+ [self.scatterPlotData addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
42
+
43
+ }
44
+
45
+
46
+
47
+ // ホスティングビューを生成
48
+
49
+ CPTGraphHostingView *hostingView =
50
+
51
+ [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
52
+
53
+ // 画面にホスティングビューを追加
54
+
55
+ [self.view addSubview:hostingView];
56
+
57
+
58
+
59
+ // グラフを生成
60
+
61
+ graph = [[CPTXYGraph alloc] initWithFrame:hostingView.bounds];
62
+
63
+ hostingView.hostedGraph = graph;
64
+
65
+
66
+
67
+ // グラフのボーダー設定
68
+
69
+ graph.plotAreaFrame.borderLineStyle = nil;
70
+
71
+ graph.plotAreaFrame.cornerRadius = 0.0f;
72
+
73
+ graph.plotAreaFrame.masksToBorder = NO;
74
+
75
+
76
+
77
+ // パディング
78
+
79
+ graph.paddingLeft = 0.0f;
80
+
81
+ graph.paddingRight = 0.0f;
82
+
83
+ graph.paddingTop = 0.0f;
84
+
85
+ graph.paddingBottom = 0.0f;
86
+
87
+
88
+
89
+ graph.plotAreaFrame.paddingLeft = 60.0f;
90
+
91
+ graph.plotAreaFrame.paddingTop = 60.0f;
92
+
93
+ graph.plotAreaFrame.paddingRight = 20.0f;
94
+
95
+ graph.plotAreaFrame.paddingBottom = 65.0f;
96
+
97
+
98
+
99
+ //プロット間隔の設定
100
+
101
+ CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
102
+
103
+ //Y軸は0〜10の値で設定
104
+
105
+ plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(10)];
106
+
107
+ //X軸は0〜10の値で設定
108
+
109
+ plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(10)];
110
+
111
+
112
+
113
+ // テキストスタイル
114
+
115
+ CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle];
116
+
117
+ textStyle.color = [CPTColor colorWithComponentRed:0.447f green:0.443f blue:0.443f alpha:1.0f];
118
+
119
+ textStyle.fontSize = 13.0f;
120
+
121
+ textStyle.textAlignment = CPTTextAlignmentCenter;
122
+
123
+
124
+
125
+ // ラインスタイル
126
+
127
+ CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
128
+
129
+ lineStyle.lineColor = [CPTColor colorWithComponentRed:0.788f green:0.792f blue:0.792f alpha:1.0f];
130
+
131
+ lineStyle.lineWidth = 2.0f;
132
+
133
+
134
+
135
+ // X軸のメモリ・ラベルなどの設定
136
+
137
+ CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
138
+
139
+ CPTXYAxis *x = axisSet.xAxis;
140
+
141
+ x.axisLineStyle = lineStyle; // X軸の線にラインスタイルを適用
142
+
143
+ x.majorTickLineStyle = lineStyle; // X軸の大きいメモリにラインスタイルを適用
144
+
145
+ x.minorTickLineStyle = lineStyle; // X軸の小さいメモリにラインスタイルを適用
146
+
147
+ x.majorIntervalLength = CPTDecimalFromString(@"2"); // X軸ラベルの表示間隔
148
+
149
+ x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); // X軸のY位置
150
+
151
+ x.title = @"X軸";
152
+
153
+ x.titleTextStyle = textStyle;
154
+
155
+ x.titleLocation = CPTDecimalFromFloat(5.0f);
156
+
157
+ x.titleOffset = 36.0f;
158
+
159
+ // x.minorTickLength = 5.0f; // X軸のメモリの長さ ラベルを設定しているため無効ぽい
160
+
161
+ // x.majorTickLength = 9.0f; // X軸のメモリの長さ ラベルを設定しているため無効ぽい
162
+
163
+ x.labelTextStyle = textStyle;
164
+
165
+
166
+
167
+ // Y軸のメモリ・ラベルなどの設定
168
+
169
+ CPTXYAxis *y = axisSet.yAxis;
170
+
171
+ y.axisLineStyle = lineStyle; // Y軸の線にラインスタイルを適用
172
+
173
+ y.majorTickLineStyle = lineStyle; // Y軸の大きいメモリにラインスタイルを適用
174
+
175
+ y.minorTickLineStyle = lineStyle; // Y軸の小さいメモリにラインスタイルを適用
176
+
177
+ y.majorTickLength = 9.0f; // Y軸の大きいメモリの長さ
178
+
179
+ y.minorTickLength = 5.0f; // Y軸の小さいメモリの長さ
180
+
181
+ y.majorIntervalLength = CPTDecimalFromFloat(1.0f); // Y軸ラベルの表示間隔
182
+
183
+ y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f); // Y軸のX位置
184
+
185
+ y.title = @"Y軸";
186
+
187
+ y.titleTextStyle = textStyle;
188
+
189
+ y.titleRotation = M_PI*2;
190
+
191
+ y.titleLocation = CPTDecimalFromFloat(11.0f);
192
+
193
+ y.titleOffset = 15.0f;
194
+
195
+ lineStyle.lineWidth = 0.5f;
196
+
197
+ y.majorGridLineStyle = lineStyle;
198
+
199
+ y.labelTextStyle = textStyle;
200
+
201
+
202
+
203
+ // 折れ線グラフのインスタンスを生成
204
+
205
+ CPTScatterPlot *scatterPlot = [[CPTScatterPlot alloc] init];
206
+
207
+ scatterPlot.identifier = kData; // 折れ線グラフを識別するために識別子を設定
208
+
209
+ scatterPlot.dataSource = self; // 折れ線グラフのデータソースを設定
210
+
211
+
212
+
213
+ // 折れ線グラフのスタイルを設定
214
+
215
+ CPTMutableLineStyle *graphlineStyle = [scatterPlot.dataLineStyle mutableCopy];
216
+
217
+ graphlineStyle.lineWidth = 3; // 太さ
218
+
219
+ graphlineStyle.lineColor = [CPTColor colorWithComponentRed:0.573f green:0.82f blue:0.831f alpha:0.50f];// 色
220
+
221
+ scatterPlot.dataLineStyle = graphlineStyle;
222
+
223
+
224
+
225
+ // グラフに折れ線グラフを追加
226
+
227
+ [graph addPlot:scatterPlot];
228
+
229
+
230
+
231
+
232
+
233
+ }
234
+
235
+
236
+
237
+ ```
238
+
239
+ ソースコードは少し変えていますが、このような感じです