いつも回答していただきありがとうございます。
さっそく質問なんですが、現在XcodeでObjective-cによるグラフを作成しています。そこでボタンをおしてグラフを表示させることはできたのですが、2回目にボタンを押すと1回目のグラフが残ってしまいます。
2回目に押すときには1回目のグラフは消したいのですがどのようにしたらいいでしょうか。
おねがいします。下記にそのグラフの様子をのせます
グラフのソースコードを追加します
Objective
1- (void)viewDidLoad 2{ 3 [super viewDidLoad]; 4 self.scatterPlotData = [NSMutableArray array]; 5 6 for ( NSUInteger i = 0; i < 11; i++ ) { 7 NSNumber *x = [NSNumber numberWithDouble:i]; 8 NSNumber *y = [NSNumber numberWithDouble:(int)(rand() / (double)RAND_MAX * 10)]; // 1〜10の値のランダム値(int) 9 [self.scatterPlotData addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]]; 10 } 11 12 // ホスティングビューを生成 13 CPTGraphHostingView *hostingView = 14 [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; 15 // 画面にホスティングビューを追加 16 [self.view addSubview:hostingView]; 17 18 // グラフを生成 19 graph = [[CPTXYGraph alloc] initWithFrame:hostingView.bounds]; 20 hostingView.hostedGraph = graph; 21 22 // グラフのボーダー設定 23 graph.plotAreaFrame.borderLineStyle = nil; 24 graph.plotAreaFrame.cornerRadius = 0.0f; 25 graph.plotAreaFrame.masksToBorder = NO; 26 27 // パディング 28 graph.paddingLeft = 0.0f; 29 graph.paddingRight = 0.0f; 30 graph.paddingTop = 0.0f; 31 graph.paddingBottom = 0.0f; 32 33 graph.plotAreaFrame.paddingLeft = 60.0f; 34 graph.plotAreaFrame.paddingTop = 60.0f; 35 graph.plotAreaFrame.paddingRight = 20.0f; 36 graph.plotAreaFrame.paddingBottom = 65.0f; 37 38 //プロット間隔の設定 39 CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 40 //Y軸は0〜10の値で設定 41 plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(10)]; 42 //X軸は0〜10の値で設定 43 plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(10)]; 44 45 // テキストスタイル 46 CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle]; 47 textStyle.color = [CPTColor colorWithComponentRed:0.447f green:0.443f blue:0.443f alpha:1.0f]; 48 textStyle.fontSize = 13.0f; 49 textStyle.textAlignment = CPTTextAlignmentCenter; 50 51 // ラインスタイル 52 CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 53 lineStyle.lineColor = [CPTColor colorWithComponentRed:0.788f green:0.792f blue:0.792f alpha:1.0f]; 54 lineStyle.lineWidth = 2.0f; 55 56 // X軸のメモリ・ラベルなどの設定 57 CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 58 CPTXYAxis *x = axisSet.xAxis; 59 x.axisLineStyle = lineStyle; // X軸の線にラインスタイルを適用 60 x.majorTickLineStyle = lineStyle; // X軸の大きいメモリにラインスタイルを適用 61 x.minorTickLineStyle = lineStyle; // X軸の小さいメモリにラインスタイルを適用 62 x.majorIntervalLength = CPTDecimalFromString(@"2"); // X軸ラベルの表示間隔 63 x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); // X軸のY位置 64 x.title = @"X軸"; 65 x.titleTextStyle = textStyle; 66 x.titleLocation = CPTDecimalFromFloat(5.0f); 67 x.titleOffset = 36.0f; 68 // x.minorTickLength = 5.0f; // X軸のメモリの長さ ラベルを設定しているため無効ぽい 69 // x.majorTickLength = 9.0f; // X軸のメモリの長さ ラベルを設定しているため無効ぽい 70 x.labelTextStyle = textStyle; 71 72 // Y軸のメモリ・ラベルなどの設定 73 CPTXYAxis *y = axisSet.yAxis; 74 y.axisLineStyle = lineStyle; // Y軸の線にラインスタイルを適用 75 y.majorTickLineStyle = lineStyle; // Y軸の大きいメモリにラインスタイルを適用 76 y.minorTickLineStyle = lineStyle; // Y軸の小さいメモリにラインスタイルを適用 77 y.majorTickLength = 9.0f; // Y軸の大きいメモリの長さ 78 y.minorTickLength = 5.0f; // Y軸の小さいメモリの長さ 79 y.majorIntervalLength = CPTDecimalFromFloat(1.0f); // Y軸ラベルの表示間隔 80 y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f); // Y軸のX位置 81 y.title = @"Y軸"; 82 y.titleTextStyle = textStyle; 83 y.titleRotation = M_PI*2; 84 y.titleLocation = CPTDecimalFromFloat(11.0f); 85 y.titleOffset = 15.0f; 86 lineStyle.lineWidth = 0.5f; 87 y.majorGridLineStyle = lineStyle; 88 y.labelTextStyle = textStyle; 89 90 // 折れ線グラフのインスタンスを生成 91 CPTScatterPlot *scatterPlot = [[CPTScatterPlot alloc] init]; 92 scatterPlot.identifier = kData; // 折れ線グラフを識別するために識別子を設定 93 scatterPlot.dataSource = self; // 折れ線グラフのデータソースを設定 94 95 // 折れ線グラフのスタイルを設定 96 CPTMutableLineStyle *graphlineStyle = [scatterPlot.dataLineStyle mutableCopy]; 97 graphlineStyle.lineWidth = 3; // 太さ 98 graphlineStyle.lineColor = [CPTColor colorWithComponentRed:0.573f green:0.82f blue:0.831f alpha:0.50f];// 色 99 scatterPlot.dataLineStyle = graphlineStyle; 100 101 // グラフに折れ線グラフを追加 102 [graph addPlot:scatterPlot]; 103 104 105} 106
ソースコードは少し変えていますが、このような感じです
回答1件
あなたの回答
tips
プレビュー