0

我试图在一个屏幕上显示多个图表,但我无法让它工作。我试图达到一个点,在这个显示器上我有 3 x pieGraphs 和 1 x 散点图。

我尝试过:无法同时显示两个 CorePlot 图表,但无法解决此问题。我使用 Core-Plot 的示例应用程序作为代码设计的基础。当我尝试在我的代码中模拟该问题的解决方案时,我看不出有什么区别。

我能够显示第一个饼图:“matchPieChart”,但不能显示第二个饼图“questionsPieChart”。它看起来甚至不处理第二张图表。

在不考虑其他类似问题解决方案(上图)的情况下,结果是:

没有建议的结果(当前)

右侧的图例是与左侧显示的饼图相关联的图例,我猜它与我尝试显示的第二个饼图相关联。它显示在应该显示第二个饼图的位置。

下面是代码执行,查看代码中的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;
}

我真的很挣扎,希望能得到帮助:-)

4

1 回答 1

1

更改此行

questionsChartView.hostedGraph = matchPieGraph;

questionsChartView.hostedGraph = questionsPieGraph;

self.view此外,检查两个托管视图在其父视图 ( )中的布局方式。确保两者都可见并正确定位。

于 2012-08-26T16:24:36.647 回答