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

  • SEARCH
  • Home
  • 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 6197523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:50:09+00:00 2026-05-24T03:50:09+00:00

I am new to CPBarPlot and totally clueless how add my own bars into

  • 0

I am new to CPBarPlot and totally clueless how add my own bars into the bar chart. I have an array of integers i want to display for the bars but to no avail. I have tried the following below. Some one plz help… plzzzzz.

- (void)constructBarChart
{
    // Create barChart from theme
    barChart = [[CPXYGraph alloc] initWithFrame:CGRectZero];
    CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
    [barChart applyTheme:theme];
    barChartView.hostedGraph = barChart;
    barChart.plotAreaFrame.masksToBorder = NO;

barChart.paddingLeft = 70.0;
barChart.paddingTop = 20.0;
barChart.paddingRight = 20.0;
barChart.paddingBottom = 80.0;

// Add plot space for horizontal bar charts
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)barChart.defaultPlotSpace;
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(300.0f)];
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(16.0f)];

CPXYAxisSet *axisSet = (CPXYAxisSet *)barChart.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.majorIntervalLength = CPDecimalFromString(@"5");
x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
x.title = @"Age Group";
x.titleLocation = CPDecimalFromFloat(7.5f);
x.titleOffset = 55.0f;

// Define some custom labels for the data elements
x.labelRotation = M_PI/4;
x.labelingPolicy = CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"< 40", @"40 - 50", @"50-60", @"> 60", @"Label E", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = x.labelOffset + x.majorTickLength;
    newLabel.rotation = M_PI/4;
    [customLabels addObject:newLabel];
    [newLabel release];
}

x.axisLabels =  [NSSet setWithArray:customLabels];

_barChartData = [[NSMutableArray alloc]initWithObjects:[NSDecimalNumber numberWithInt:30], [NSDecimalNumber numberWithInt:30],[NSDecimalNumber numberWithInt:40],[NSDecimalNumber numberWithInt:50],[NSDecimalNumber numberWithInt:100],nil];

CPXYAxis *y = axisSet.yAxis;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
y.majorIntervalLength = CPDecimalFromString(@"50");
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
y.title = @"Number of Individuals";
y.titleOffset = 45.0f;
y.titleLocation = CPDecimalFromFloat(150.0f);


// First bar plot
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor darkGrayColor] horizontalBars:NO];
//barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor blueColor] horizontalBars:NO];
//barPlot.dataSource = self;
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.barOffset = CPDecimalFromFloat(0.25f);
barPlot.barCornerRadius = 2.0f;
barPlot.identifier = @"Bar Plot 2";
barPlot.delegate = self;
[barChart addPlot:barPlot toPlotSpace:plotSpace];
}

‘

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

    if ( fieldEnum == CPPieChartFieldSliceWidth ) {
        num = [self.dataForChart objectAtIndex:index];
    }
    else {
        num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
    }
}
else if( [plot isKindOfClass:[CPBarPlot class]] ) {
    switch ( fieldEnum ) {
        case CPBarPlotFieldBarLocation:
            //num = [NSNumber numberWithUnsignedInteger:index];
            num = (NSNumber *)[NSNumber numberWithUnsignedInteger:[[_barChartData objectAtIndex:index] unsignedIntegerValue]];
            break;
    }   
}

return num;
}
  • 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-24T03:50:10+00:00Added an answer on May 24, 2026 at 3:50 am

    You need to set the plot datasource:

    barPlot.dataSource = self;
    

    Change the bar plot section of -numberForPlot:field:recordIndex: to:

    switch ( fieldEnum ) {
        case CPTBarPlotFieldBarLocation:
            num = [NSNumber numberWithUnsignedInteger:index];
            break;
        case CPTBarPlotFieldBarTip:
            num = [_barChartData objectAtIndex:index];
            break;
    }
    

    The constants in the switch statement depend on which version of Core Plot you’re using. I used the latest field constants. Earlier versions used a prefix of “CP” instead of “CPT”. Very old versions used CPBarPlotFieldBarLength instead of CPTBarPlotFieldBarTip.

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

Sidebar

Related Questions

New to Tsung and Erlan and have run into an issue I haven't been
new on ruby and using windows xp and rails 3, i want to send
New to PHP and MySQL, have heard amazing things about this website from Leo
New to Regex. I want to validate to this format: Any character allowed, except
New to java here. I'm trying to split a string into variables in an
New to pandas python. I have a dataframe (df) with two columns of cusips.
New here and have a question for anyone experienced with HTML5 video and/or the
(New to Objective-C, but well versed in C/C++). Presently I have an Objective-C class
** New EDIT ** so what I'm trying to do is this. I want
(New to ASP.NET here.) I have a user control which should check for a

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.