开发者

Core plot - pie chart not showing enough segments

In numberOfRecordsForPlot:... I have this code:

int count = [[optsDict objectForKey:@"Values"] count];
NSLog(@"pie count: %i", count);
return count;

It outputs pie count: 4 thus (unless I'm misinterpreting) there should be 4 segments to the pie chart.

开发者_运维百科

Then in numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index I have this:

double percentage = ((float)counter/(float)total)*100;
num = (NSDecimalNumber *)[NSDecimalNumber numberWithInt:(int)percentage];

NSLog(@"index: %i, percentage: %f", index, percentage);
NSLog(@"%@", num);

return num;

This outputs:

index: 0, percentage: 9.090909
9
index: 1, percentage: 9.090909
9
index: 2, percentage: 63.636364
63
index: 3, percentage: 18.181818
18

So I am assuming (reasonably) that the function is getting run once for each segment and its returning a figure for all of them!

However the chart looks like this:

Core plot - pie chart not showing enough segments

Theres not really much else I can tell you. Thats all I know, I don't know much about core plot!

If there is ANY help you can give me I'd be very appreciative!

Thank you


I play with your values and I think I found the problem in your code.

Try to return in -(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index object of class NSNumber

num = (NSNumber *)[NSNumber numberWithInt:(int)percentage];
return num;

I was testing this in sample that is included in CorePlot framework. Firstly I tried to init contentArray array with NSNumbers:

NSMutableArray *contentArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithInt:9.0909099], [NSNumber numberWithInt:9.0909099], [NSNumber numberWithInt:63.63636463], [NSNumber numberWithInt:18.181818], nil];

And everything works perfect. Then I've tried to init array as in your code with NSDecimalNumbers:

NSMutableArray *contentArray = [NSMutableArray arrayWithObjects:[NSDecimalNumber numberWithInt:9.0909099], [NSDecimalNumber numberWithInt:9.0909099], [NSDecimalNumber numberWithInt:63.63636463], [NSDecimalNumber numberWithInt:18.181818], nil];

And I've received exactly your double colored chart.

Hope this will help you =)


Do you implement

- (CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index

which is in the CPTPieChartDataSource protocol? Since there are no borderlines between sections, it might be drawing several with the same color. Make sure you're returning a different color CPTFill for each index.


Thomas i have done this pia chart using titanium framework.You have to use java script for display pia chart


It looks like you missed first and second slices. numberOfRecordsForPlot can return 4, but in numberForPlot some code returns nil.. I created a new Pie Chart with your values, and all works nice. See code below.

You can also try to see data labels for every slice in the -(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index delegate method. If you really have 4 slices then 2 of them will placed at the same position. Code below is as well.

Core plot - pie chart not showing enough segments

-(void) viewDidLoad{
    NSMutableArray *contentArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithInt:9], [NSNumber numberWithInt:9], [NSNumber numberWithInt:63], [NSNumber numberWithInt:18], nil];
    self.dataForChart = contentArray;

    // Add pie chart
    CPPieChart *piePlot = [[CPPieChart alloc] init];
    piePlot.dataSource = self;
    piePlot.pieRadius = 100.0;
    piePlot.identifier = @"Pie Chart 1";
    piePlot.startAngle = M_PI;
    piePlot.sliceDirection = CPPieDirectionClockwise;
    piePlot.delegate = self;
    [pieChart addPlot:piePlot];
    [piePlot release];
}

#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{
    return [self.dataForChart count];
}

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    if ( index >= [self.dataForChart count] ) return nil;

    if ( fieldEnum == CPPieChartFieldSliceWidth ) {
        return [self.dataForChart objectAtIndex:index];
    }
    else {
        return [NSNumber numberWithInt:index];
    }
}

-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index {
    CPTextLayer *label = [[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"%lu", index]];
    label.textStyle.color = [CPColor lightGrayColor];
    return [label autorelease];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜