1つの画面に複数のグラフを表示しようとしていますが、機能させることができません。このディスプレイに3xpieGraphsと1x散布図があるポイントに到達しようとしています。
私は試しました:2つのCorePlotグラフを同時に表示することはできませんが、これを解決することはできませんでした。コードの設計のベースとして、Core-Plotのサンプルアプリを使用しています。コードでその問題の解決策をエミュレートしようとしても、違いはわかりません。
最初の円グラフ「matchPieChart」は表示できますが、2番目の円グラフ「questionsPieChart」は表示できません。2番目のチャートを処理するようにも見えません。
他の同様の問題解決策(上記)を考慮しない場合、結果は次のようになります。
右側の凡例は、左側に表示されている円に関連付けられている凡例です。これは、表示しようとしている2番目の円に接続されていると思います。2番目の円グラフを表示する場所に表示されます。
コードの実行は次のとおりです。コードのNSLogを参照してください。
iPad_matchPieChartConfiguration
numberOfRecordsForPlot:
numberOfRecordsForPlot:
numberForPlot:
numberForPlot:
numberOfRecordsForPlot:
numberOfRecordsForPlot:
legendTitleForPieChart: matchPieChart
legendTitleForPieChart: matchPieChart
iPad_questionsPieChartConfiguration
legendTitleForPieChart: matchPieChart
legendTitleForPieChart: matchPieChart
legendTitleForPieChart: matchPieChart
legendTitleForPieChart: matchPieChart
dataLabelForPlot:
dataLabelForPlot: matchPieChart
dataLabelForPlot:
dataLabelForPlot: matchPieChart
legendTitleForPieChart: matchPieChart
legendTitleForPieChart: matchPieChart
legendTitleForPieChart: matchPieChart
legendTitleForPieChart: matchPieChart
そして、これが実際のコードです:
-(void)iPad_matchPieChartConfiguration {
NSLog(@"iPad_matchPieChartConfiguration");
// Set titles
playerName_label.text = playerName;
myString = [[NSString alloc]initWithFormat:@"Total number of Games played: %.0f", nrGamesPlayed];
title_Matches_label.text = myString;
//Create host view
matchChartView = [[CPTGraphHostingView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
[self.view addSubview:matchChartView];
matchPieGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
matchChartView.hostedGraph = matchPieGraph;
matchPieGraph.plotAreaFrame.masksToBorder = NO;
matchPieGraph.paddingLeft = 10.0;
matchPieGraph.paddingTop = 120.0;
matchPieGraph.paddingRight = 350.0;
matchPieGraph.paddingBottom = 360.0;
matchPieGraph.axisSet = nil;
//====ADD MATCH PIE CHART====//
matchPiePlot = [[CPTPieChart alloc]init];
matchPiePlot.dataSource = self;
matchPiePlot.pieRadius = 100.0;
matchPiePlot.identifier = @"matchPieChart";
matchPiePlot.startAngle = M_PI_4;
matchPiePlot.sliceDirection = CPTPieDirectionCounterClockwise;
matchPiePlot.borderLineStyle = [CPTLineStyle lineStyle];
matchPiePlot.labelOffset = 5.0;
matchPieGraph.titleTextStyle = whiteText;
[matchPieGraph addPlot:matchPiePlot];
// 1 - Get graph instance
CPTGraph *matchGraph = matchChartView.hostedGraph;
// 2 - Create legend
CPTLegend *theMatchLegend = [CPTLegend legendWithGraph:matchGraph];
// 3 - Configure legen
CPTMutableTextStyle *legendMatchTextStyle = [CPTTextStyle textStyle];
legendMatchTextStyle.fontSize = 12.0f;
legendMatchTextStyle.fontName = @"Noteworthy-Bold";
legendMatchTextStyle.color = [CPTColor whiteColor];
theMatchLegend.numberOfColumns = 2;
theMatchLegend.fill = nil;
theMatchLegend.borderLineStyle = nil;
theMatchLegend.cornerRadius = 5.0;
theMatchLegend.textStyle = legendMatchTextStyle;
// 4 - Add legend to graph
matchGraph.legend = theMatchLegend;
matchGraph.legendAnchor = CPTRectAnchorRight;
matchGraph.legendDisplacement = CGPointMake(-460.0, -10.0);
}
-(void)iPad_questionsPieChartConfiguration {
NSLog(@"iPad_questionsPieChartConfiguration");
// Set titles
playerName_label.text = playerName;
myString = [[NSString alloc]initWithFormat:@"Total number of Questions answered: %.0f", nrQuestionsAnswered];
title_Questions_label.text = myString;
//Create host view
questionsChartView = [[CPTGraphHostingView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
[self.view addSubview:questionsChartView];
questionsPieGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
questionsChartView.hostedGraph = matchPieGraph;
questionsPieGraph.plotAreaFrame.masksToBorder = NO;
questionsPieGraph.paddingLeft = 10.0;
questionsPieGraph.paddingTop = 120.0;
questionsPieGraph.paddingRight = 150.0;
questionsPieGraph.paddingBottom = 360.0;
questionsPieGraph.axisSet = nil;
//====ADD MATCH PIE CHART====//
questionsPiePlot = [[CPTPieChart alloc]init];
questionsPiePlot.dataSource = self;
questionsPiePlot.pieRadius = 100.0;
questionsPiePlot.identifier = @"questionsPieChart";
questionsPiePlot.startAngle = M_PI_4;
questionsPiePlot.sliceDirection = CPTPieDirectionCounterClockwise;
questionsPiePlot.borderLineStyle = [CPTLineStyle lineStyle];
questionsPiePlot.labelOffset = 5.0;
questionsPieGraph.titleTextStyle = whiteText;
[questionsPieGraph addPlot:questionsPiePlot];
// 1 - Get graph instance
CPTGraph *questionsGraph = questionsChartView.hostedGraph;
// 2 - Create legend
CPTLegend *theQuestionsLegend = [CPTLegend legendWithGraph:questionsGraph];
// 3 - Configure legen
CPTMutableTextStyle *legendQuestionsTextStyle = [CPTTextStyle textStyle];
legendQuestionsTextStyle.fontSize = 12.0f;
legendQuestionsTextStyle.fontName = @"Noteworthy-Bold";
legendQuestionsTextStyle.color = [CPTColor whiteColor];
theQuestionsLegend.numberOfColumns = 2;
theQuestionsLegend.fill = nil;
theQuestionsLegend.borderLineStyle = nil;
theQuestionsLegend.cornerRadius = 5.0;
theQuestionsLegend.textStyle = legendQuestionsTextStyle;
// 4 - Add legend to graph
questionsGraph.legend = theQuestionsLegend;
questionsGraph.legendAnchor = CPTRectAnchorRight;
questionsGraph.legendDisplacement = CGPointMake(-160.0, -10.0);
}
- (void)viewDidLoad {
[super viewDidLoad];
playerName_label.text = playerName;
AccessPlayerData *checkPlayerDiffFunction = [AccessPlayerData new];
int diffLevel = [checkPlayerDiffFunction checkPlayersDifficultyLevel:playerName];
switch (diffLevel) {
case 1:
playerDifficulty_label.text = @"Difficuly Level: Children / Easy";
break;
case 2:
playerDifficulty_label.text = @"Difficuly Level : Teenager / Medium";
break;
case 3:
playerDifficulty_label.text = @"Difficuly Level: Adult / Hard";
break;
default:
break;
}
nrGamesPlayed_label.text = [NSString stringWithFormat:@"Total number of games played: %.0f", nrGamesPlayed];
//====IF THIS IS IPAD PROCESS ALL GRAPHS====//
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
double nrOfSingleGames = nrGamesPlayed -(nrGamesWon + nrGamesLost);
double nrOfTotalGames = nrGamesPlayed;
double percentageNrOfSingleGames = (nrOfSingleGames / nrOfTotalGames) * 100;
double percentageNrOfMatchGames = ((nrOfTotalGames - nrOfSingleGames) / nrOfTotalGames) * 100;
NSString *singleGames = [[NSString alloc]initWithFormat:@"%.0f (%.0f%%)", nrOfSingleGames, percentageNrOfSingleGames];
NSString *matchesGames = [[NSString alloc]initWithFormat:@"%.0f (%.0f%%)", (nrOfTotalGames - nrOfSingleGames), percentageNrOfMatchGames];
//=========ADD DATA FOR MATCHES PIE GRAPH==========//
NSMutableArray *contentMatchArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithDouble:nrOfSingleGames], [NSNumber numberWithDouble:(nrOfTotalGames - nrOfSingleGames)], nil];
labelMatchOrder = [[NSArray alloc]initWithObjects:singleGames, matchesGames, nil];
legendMatchOrder = [[NSArray alloc] initWithObjects:@"Single Games", @"Matches", nil];
self.dataForMatchChart = contentMatchArray;
//=========ADD DATA FOR QUESTIONS PIE GRAPH==========//
nrQuestionsWrongAnswers = nrQuestionsAnswered - nrQuestionsCorrectAnswered;
double percentageCorrectAnswers = (nrQuestionsCorrectAnswered / nrQuestionsAnswered) * 100;
double percentageWrongAnswers = (nrQuestionsWrongAnswers / nrQuestionsAnswered) * 100;
NSString *answerCorrect = [[NSString alloc]initWithFormat:@"%.0f (%.0f%%)", nrQuestionsCorrectAnswered, percentageCorrectAnswers];
NSString *answerWrong = [[NSString alloc]initWithFormat:@"%.0f (%.0f%%)", nrQuestionsWrongAnswers, percentageWrongAnswers];
playerName_label.text = playerName;
myString = [[NSString alloc]initWithFormat:@"Total number of questions answered: %.0f",nrQuestionsAnswered];
title_Questions_label.text = myString;
NSMutableArray *contentQuestionsArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithDouble:nrQuestionsWrongAnswers], [NSNumber numberWithDouble:nrQuestionsCorrectAnswered], nil];
labelQuestionsOrder = [[NSArray alloc]initWithObjects:answerWrong, answerCorrect, nil];
legendQuestionsOrder = [[NSArray alloc] initWithObjects:@"Correct answers", @"Wrong answers", nil];
self.dataForQuestionsChart = contentQuestionsArray;
NSLog(@"ContentQuestionsArray: %@", contentQuestionsArray);
//====SET GLOBAL PARAMETERS FOR THE GRAPHs====//
whiteText = [CPTMutableTextStyle textStyle];
whiteText.color = [CPTColor whiteColor];
whiteText.fontSize = 14;
whiteText.fontName = @"Noteworthy-Bold";
[self iPad_matchPieChartConfiguration];
[self iPad_questionsPieChartConfiguration];
}
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index {
if ( [matchPiePlot.identifier isEqual:[NSString stringWithFormat:@"matchPieChart"]] ) {
NSLog(@"legendTitleForPieChart: matchPieChart");
if (index < [legendMatchOrder count]) {
return [legendMatchOrder objectAtIndex:index];
}
}
else if ( [matchPiePlot.identifier isEqual:[NSString stringWithFormat:@"questionsPieChart"]] ) {
NSLog(@"legendTitleForPieChart: questionsPieChart");
}
return @"N/A";
}
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
NSLog(@"numberOfRecordsForPlot:");
if ([plot isKindOfClass:[CPTPieChart class]]) {
return [dataForMatchChart count];
}
else {
return 10; //>>>dummy
}
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSLog(@"numberForPlot:");
NSDecimalNumber *num = nil;
if ([plot isKindOfClass:[CPTPieChart class]]) {
if (index >= [dataForMatchChart count]) {
return nil;
}
if (fieldEnum == CPTPieChartFieldSliceWidth) {
num = [dataForMatchChart objectAtIndex:index];
}
else {
num = (NSDecimalNumber *) [NSDecimalNumber numberWithUnsignedInteger:index];
}
}
return num;
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index {
NSLog(@"dataLabelForPlot:");
CPTTextLayer *label = nil;
if ( [plot.identifier isEqual:[NSString stringWithFormat:@"matchPieChart"]] ) {
NSLog(@"dataLabelForPlot: matchPieChart");
label = [[CPTTextLayer alloc] initWithText:[labelMatchOrder objectAtIndex:index]];
CPTMutableTextStyle *textStyle = [label.textStyle mutableCopy];
matchPiePlot.labelOffset = -80;
textStyle.fontName = @"Noteworthy-Bold";
textStyle.color = [CPTColor whiteColor];
textStyle.fontSize = 16;
label.textStyle = textStyle;
return label;
}
else if ( [plot.identifier isEqual:[NSString stringWithFormat:@"matchPieChart"]] ) {
NSLog(@"dataLabelForPlot: MATCH");
}
return label;
}
//====SET COLOR FOR SLICES IN PIE GRAPH====//
-(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index {
// For index = 0
double red1 = 222.0;
double green1 = 184.0;
double blue1 = 135.0;
float alpha1 = 0.5f;
double red2 = 155.0;
double green2 = 00.0;
double blue2 = 0.0;
float alpha2 = 0.3f;
if (index == 0) {
CPTFill *fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:red1 green:green1 blue:blue1 alpha:alpha1]];
return fill;
}
if (index == 1) {
CPTFill *fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:red2 green:green2 blue:blue2 alpha:alpha2]];
return fill;
}
return 0;
}
私は本当にこれに苦労していて、助けていただければ幸いです:-)