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 8264553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:29:56+00:00 2026-06-08T04:29:56+00:00

Despite having spent countless hours examining line of code, and after creating a fully

  • 0

Despite having spent countless hours examining line of code, and after creating a fully functional scatter plot, I cannot get the bars to show up on my bar chart.

I am using Core Plot, I am sure I have imported the core plot library and set up the core plot environment correctly as the scatter plot is fully working.

Could somebody please save me from pulling out my hair and perhaps tell me where I am going wrong? I have a feeling its because my #define BAR_POSITION @”POSITION” and #define BAR_HEIGHT @”HEIGHT” are not getting set/called properly.

Below is a the code i have used for both the header and main files.

Any and all help is appreciated.

Header file:

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@interface BarPlotViewController : UIViewController 
<CPTBarPlotDataSource, CPTBarPlotDelegate> {

}

@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, retain) CPTGraphHostingView *hostingView;
@property (nonatomic, retain) CPTXYGraph *graph;

- (void) generateBarPlot;

@end

Main file:

#import "BarPlotViewController.h"

@implementation BarPlotViewController


#define BAR_POSITION @"POSITION"
#define BAR_HEIGHT @"HEIGHT"
#define COLOR @"COLOR"
#define CATEGORY @"CATEGORY"

#define AXIS_START 0
#define AXIS_END 50

@synthesize data;
@synthesize graph;
@synthesize hostingView;




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    self.data = [NSMutableArray array];

    int bar_heights[] = {20,30,10,40};
    NSLog(@"After heights initialised.");


    int bar_positions[] = {10,20,30,40};
    NSLog(@"After position initialised.");


    UIColor *colors[] = {
        [UIColor redColor],
        [UIColor blueColor],
        [UIColor orangeColor],
        [UIColor purpleColor]};
    NSLog(@"After colors initialised.");


    NSString *categories[] = {@"A", @"B", @"C", @"D"};
    NSLog(@"After categories initialised.");


    for (int i = 0; i < 4 ; i++){
        double position = i*10; //Bars will be 10 pts away from each other
        double height = bar_heights[i];
        //double position = bar_positions[i];

        NSDictionary *bar = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithDouble:position],BAR_POSITION,
                             [NSNumber numberWithDouble:height],BAR_HEIGHT,
                             colors[i],COLOR,
                             categories[i],CATEGORY,
                             nil];
        [self.data addObject:bar];

        NSLog(@"Data entered into bar dictionary.");

        //NSString *positionStringValue = [NSString stringWithFormat:@"%.02f", position];
        //NSString *log = [@"Position " stringByAppendingFormat: positionStringValue];
        //NSLog( log );

        //NSString *colorsStringValue = [NSString stringWithFormat:@"%.02f", position];
        //NSString *log2 = [@"Colors " stringByAppendingFormat: colors[i]];
        //NSLog( log2 );

    }
    [self generateBarPlot];
    NSLog(@"After generate bar plot.");
}
return self;
}



- (void)generateBarPlot
{
//Create host view
self.hostingView = [[CPTGraphHostingView alloc] 
                    initWithFrame:[[UIScreen mainScreen]bounds]];
[self.view addSubview:self.hostingView];

//Create graph and set it as host view's graph
self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostingView.bounds];
//self.graph = [[CPTXYGraph alloc] initWithHostingView:_graphHostingView];

[self.hostingView setHostedGraph:self.graph];
//self.scatterPlot = [[TUTSimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:data];
//[self.scatterPlot initialisePlot];



//set graph padding and theme
self.graph.plotAreaFrame.paddingTop = 20.0f;
self.graph.plotAreaFrame.paddingRight = 20.0f;
self.graph.plotAreaFrame.paddingBottom = 70.0f;
self.graph.plotAreaFrame.paddingLeft = 70.0f;
[self.graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];


// Gets rid of decimal on years
NSNumberFormatter *labelFormatter = [[NSNumberFormatter alloc] init];
labelFormatter.maximumFractionDigits = 0;


//set axes ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:
                    CPTDecimalFromFloat(AXIS_START)
                                                length:CPTDecimalFromFloat((AXIS_END - AXIS_START))];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:
                    CPTDecimalFromFloat(AXIS_START)
                                                length:CPTDecimalFromFloat((AXIS_END - AXIS_START))];



CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
//set axes' title, labels and their text styles
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 14;
textStyle.color = [CPTColor whiteColor];

axisSet.xAxis.title = @"A";
axisSet.yAxis.title = @"B";
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.yAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 30.0f;
axisSet.yAxis.titleOffset = 40.0f;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.yAxis.labelOffset = 3.0f;

//set axes' line styles and interval ticks
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 3.0f;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(5.0f);
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(5.0f);
axisSet.xAxis.majorTickLength = 10.0f;
axisSet.yAxis.majorTickLength = 10.0f;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.xAxis.minorTickLength = .0f;
axisSet.yAxis.minorTickLength = .0f;
axisSet.xAxis.labelFormatter = labelFormatter;
axisSet.yAxis.labelFormatter = labelFormatter;



// Create bar plot and add it to the graph
CPTBarPlot *plot = [[CPTBarPlot alloc] init] ;
plot.dataSource = self;
plot.delegate = self;
plot.barWidth = [[NSDecimalNumber decimalNumberWithString:@"10.0"]
                 decimalValue];
plot.barOffset = [[NSDecimalNumber decimalNumberWithString:@"10.0"]
                  decimalValue];
plot.barCornerRadius = 5.0;


// Remove bar outlines
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor greenColor];
plot.lineStyle = borderLineStyle;


// Identifiers are handy if you want multiple plots in one graph
plot.identifier = @"chocoplot";
[self.graph addPlot:plot];
}



-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
if ( [plot.identifier isEqual:@"chocoplot"] ) 
{
    return [self.data count];
}

return 0;
}



-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
if ( [plot.identifier isEqual:@"chocoplot"] )
{
    NSDictionary *bar = [self.data objectAtIndex:index];

    if(fieldEnum == CPTBarPlotFieldBarLocation) {
        //return [bar valueForKey:BAR_POSITION];
        NSLog(@"bar position before");
        return [NSNumber numberWithDouble:30];
        NSLog(@"bar position after");
    }
    else if(fieldEnum ==CPTBarPlotFieldBarTip) {
        //return [bar valueForKey:BAR_HEIGHT];
        NSLog(@"bar height before");
        return [NSNumber numberWithDouble:20];
        NSLog(@"bar height before");
    }
}
NSLog(@"numberForPlot return before");
return [NSNumber numberWithFloat:0];
NSLog(@"numberForPlot return after");
}


-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
if ( [plot.identifier isEqual: @"chocoplot"] )
{
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.fontName = @"Helvetica";
    textStyle.fontSize = 14;
    textStyle.color = [CPTColor whiteColor];

    NSDictionary *bar = [self.data objectAtIndex:index];
    CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", [bar valueForKey:@"CATEGORY"]]];
    label.textStyle =textStyle;

    return label;
}

CPTTextLayer *defaultLabel = [[CPTTextLayer alloc] initWithText:[NSString stringWithString:@"Label"]];
return defaultLabel;

}



-(CPTFill *)barFillForBarPlot:(CPTBarPlot *)barPlot
              recordIndex:(NSUInteger)index
{
if ( [barPlot.identifier isEqual:@"chocoplot"] )
{
    NSDictionary *bar = [self.data objectAtIndex:index];
    CPTGradient *gradient = [CPTGradient gradientWithBeginningColor:[CPTColor whiteColor]
                                                        endingColor:[bar valueForKey:@"COLOR"]
                                                  beginningPosition:0.0 endingPosition:0.3 ];
    [gradient setGradientType:CPTGradientTypeAxial];
    [gradient setAngle:320.0]; 

    CPTFill *fill = [CPTFill fillWithGradient:gradient];

    return fill;

}
return [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.0 green:1.0 blue:1.0 alpha:1.0]];

}

@end
  • 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-06-08T04:30:01+00:00Added an answer on June 8, 2026 at 4:30 am

    Are you sure your xrange is set correctly. I haven’t debugged the code but its easy to overlook.

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

Sidebar

Related Questions

I've got the DTD for OFX 1.03 (their latest version despite having developed and
Despite me working with C# ( Windows Forms ) for years, I'm having a
Despite being a Project Euler program, the following code doesn't actually concern it much.
I'm asking this question despite having read similar but not exactly what I want
I'm stuck with a code. I admit I´m not an expert programmer, but despite
I am getting segmentation fault despite having a signal handler for SIGSEGV . The
Despite having very little Linux experience, I'm too enticed by VPS (and too sick
Unfortunately, despite having tried to learn regex at least one time a year for
I'm feeling a bit lost with my question about HTML5 code generation, and despite
Despite having studied Domain Driven Design for a long time now there are still

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.