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
Are you sure your xrange is set correctly. I haven’t debugged the code but its easy to overlook.