Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4607642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:43:54+00:00 2026-05-22T00:43:54+00:00

I have write following code to show bar plot annotation. Annotations are shown but

  • 0

I have write following code to show bar plot annotation. Annotations are shown but with unexpected alignment/position. Text of CPTextLayer is showing at top-left corner of the frame. I am trying to show it at center. I have used a background image of the CPTextLayer as background color. Please have a look through the code- (symbolTextAnnotation is a CPLayerAnnotation object decleared in the interface)

-(void)barPlot:(CPBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index
{
    CPXYGraph* graph = (CPXYGraph*)plot.graph;
    NSNumber *value = [self numberForPlot:plot field:CPBarPlotFieldBarTip recordIndex:index];

    if ( symbolTextAnnotation ) {
        [graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
        symbolTextAnnotation = nil;
    }

    // Setup a style for the annotation
    CPMutableTextStyle *hitAnnotationTextStyle = [CPMutableTextStyle textStyle];
    hitAnnotationTextStyle.color = [CPColor whiteColor];
    hitAnnotationTextStyle.fontSize = 9.0f;
    hitAnnotationTextStyle.fontName = @"Helvetica-Bold";


    // Determine point of symbol in plot coordinates
    NSNumber *x = [NSNumber numberWithInt:index+1];
    NSNumber *y = [NSNumber numberWithInt:0];

    NSArray *anchorPoint = [NSArray arrayWithObjects:y, x, nil];

    // Add annotation
    // First make a string for the y value
    NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
    [formatter setMaximumFractionDigits:2];
    NSString *yString = [formatter stringFromNumber:value];

    // Now add the annotation to the plot area
    UIImage *annotationBG = [UIImage imageNamed:@"annotation-bg.png"]; 
    CPTextLayer *textLayer = [[[CPTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle] autorelease];

    textLayer.backgroundColor = [UIColor colorWithPatternImage:annotationBG].CGColor;
    textLayer.frame = CGRectMake(0, 0, annotationBG.size.width, annotationBG.size.height);


    symbolTextAnnotation = [[CPPlotSpaceAnnotation alloc] initWithPlotSpace:plot.plotSpace anchorPlotPoint:anchorPoint];
    symbolTextAnnotation.contentLayer = textLayer;
    symbolTextAnnotation.displacement = CGPointMake(-18.0f, 3.0f);

    [graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:43:55+00:00Added an answer on May 22, 2026 at 12:43 am

    CPTextLayer sizes itself to the size of its text. Resizing it after setting the text is causing the drawing problem. You could make another CPLayer, set your background on that, make your CPTextLayer a sublayer, and position it however you want.

    The displacement property works with the contentAnchorPoint property to position the annotation. The contentAnchorPoint is the Core Animation anchor point for your content layer. The displacement is the pixel offset of that point from the anchorPlotPoint of the annotation. For example, if you want your annotation centered at the anchorPlotPoint, set the contentAnchorPoint to CGPointMake(0.5, 0.5) and the displacement to CGPointZero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: MemoryStream foo(){ MemoryStream ms = new MemoryStream(); // write
I have the following code: <DataTemplate> <!--<sdk:DataGridTextColumn Binding={Binding Description} Header=Description Width=205 />--> <TextBlock Text={Binding
The following code is good for uploading text files, but it fails to upload
hi folks I have this following code but it gives me invalid argument error
I have the following code to read and write a single line to a
The following code does not have any errors when run script.py -f filename but
I have written the following code to write a file on my local file
i have used following code in my c# application string verification_url = @http://site.com/posttest.php?; string
I have the following code: private void button1_Click(object sender, EventArgs e) { var answer
I have the following code for encryption and decryption. The problem is that at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.