2

我已经使用核心绘图框架实现了一个饼图,我需要知道如何在每个切片内显示文本。

这是.m源代码:

@implementation ViewController

@synthesize graph,pieData;

// Implement loadView to create a view hierarchy programmatically
// without using a nib.
- (void)loadView {
    CPTGraphHostingView * newView = [[CPTGraphHostingView alloc]initWithFrame:
                                     [[UIScreen mainScreen] applicationFrame]];
    self.view = newView;
    [newView release];
}

// Implement viewDidLoad to do additional setup after loading the view, 
// typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds];   
    CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
    hostingView.hostedGraph = graph;

    CPTPieChart *pieChart = [[CPTPieChart alloc] init];
    pieChart.dataSource = self;
    pieChart.delegate = self;
    pieChart.pieRadius = 100.0;
    pieChart.identifier = @"PieChart1";
    pieChart.startAngle = M_PI_4;
    //pieChart.sliceDirection = CPTPieDirectionCounterClockwise;
    pieChart.sliceDirection = CPTPieDirectionClockwise;

    self.pieData=  [NSMutableArray arrayWithObjects:
                    [NSNumber numberWithDouble:90.0], 
                    [NSNumber numberWithDouble:20.0],
                    [NSNumber numberWithDouble:30.0],
                    [NSNumber numberWithDouble:40.0],
                    [NSNumber numberWithDouble:50.0], 
                    [NSNumber numberWithDouble:60.0], 
                    nil];

    CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
    [graph applyTheme:theme];

    [graph addPlot:pieChart];
    [pieChart release];
}


-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return [self.pieData count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot
 field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    return [self.pieData objectAtIndex:index];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
    [pieData release];
    [graph release];
    [super dealloc];
}

- (void)pieChart:(CPTPieChart *)plot 
sliceWasSelectedAtRecordIndex:(NSUInteger)index {
    NSLog(@"Slices are selected: index = %d", index);
}
@end
4

1 回答 1

0

使用该labelOffset物业。正值会将标签定位在绘图之外,负值会将它们定位在内部。

于 2012-04-04T23:31:08.203 回答