views:

171

answers:

1

I have core-plot graph like this:

graph

How can I add a label to each of these plots. I have seen some references about adding a CPTextLayer to the graph, but could not figure it out how to do it. Is there an example somebody can point me to?

A: 

I was able to add three labels to the top of the graph like this:

CPTextStyle *textStyle1 = [CPTextStyle textStyle];
textStyle1.color    =  [CPColor blueColor];
textStyle1.fontSize  = 14;
CPTextStyle *textStyle2 = [CPTextStyle textStyle];
textStyle2.color    =  [CPColor greenColor];
textStyle2.fontSize  = 14;
CPTextStyle *textStyle3 = [CPTextStyle textStyle];
textStyle3.color    =  [CPColor redColor];
textStyle3.fontSize  = 14;

CPTextLayer *layer1 = [[[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"text1"]] autorelease];
CPTextLayer *layer2 = [[[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"text2"]] autorelease];
CPTextLayer *layer3 = [[[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"text3"]] autorelease];
layer1.bounds = CGRectMake(-75,2,20,10);
layer1.textStyle = textStyle1;
layer2.bounds = CGRectMake(-130,2, 20,10);
layer2.textStyle = textStyle2;
layer3.bounds = CGRectMake(-190,2, 20,10);
layer3.textStyle = textStyle3;

CPLayerHostingView * newView = [[CPLayerHostingView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
newView.hostedLayer = graph;
NSInteger count = [graph.sublayers count];
[graph insertSublayer:layer1 atIndex: count + 1];
[graph insertSublayer:layer2 atIndex: count + 2];
[graph insertSublayer:layer3 atIndex: count + 3];
self.view = newView;
[newView release];
aogan