how to remove the shadow for the label added to pie plot using core plot libraru in iphone sdk - objective-c

I had added shadow for pie plot but I'm getting shadow for the label data also. I want to remove that shadow. Please help me.
Thanks in advance.
piePlot.dataSource = self;
piePlot.pieRadius = 65.0;
piePlot.pieInnerRadius = 35.0;
piePlot.shadowColor = [[UIColor blackColor]CGColor];
piePlot.shadowRadius = 3.0;
piePlot.shadowOffset = CGSizeMake(8,-3);
piePlot.shadowOpacity = 1.0;
piePlot.identifier = #"Current Year Credits By Type";
piePlot.startAngle = M_PI_4;
piePlot.sliceDirection = CPTPieDirectionClockwise;
piePlot.borderLineStyle = [CPTLineStyle lineStyle];
piePlot.sliceLabelOffset = 10.0;
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
CPTTextLayer *newLayer = nil;
static CPTMutableTextStyle *whiteText = nil;
if ( !whiteText )
{
whiteText = [[CPTMutableTextStyle alloc] init];
whiteText.color = [CPTColor blackColor];
}
if ( [plot isKindOfClass:[CPTPieChart class]] )
{
NSString *str = [NSString stringWithFormat:#"%#",[pieChartData1 objectAtIndex:index]];
newLayer = [[[CPTTextLayer alloc] initWithText:str style:whiteText] autorelease];
}
return newLayer;
}

Use Core Plot's CPTShadow class instead of the CALayer shadow properties.
CPTMutableShadow *blackShadow = [CPTMutableShadow shadow];
blackShadow.shadowOffset = CGSizeMake(8,-3);
blackShadow.shadowColor = [CPTColor blackColor];
piePlot.shadow = blackShadow;

Related

Core Plot Pie Chart not full when animating

I have an inconsistent behaviour when animating Pie Chart:
- Full animation
- animation with a little gap at the end.
below is my codes and results!
image link
animation with gap
http://s604.photobucket.com/user/Jcljk/media/IMG_5865_zps83ac73a6.png.html
Full animation
http://s604.photobucket.com/user/Jcljk/media/IMG_5867_zps7beff336.png.html
I appreciate your help.. thank you
-(void)configureGraph {
///********************** Pie Chart* ***************//////
// 1 - Create and initialize graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
self.hostView.hostedGraph = graph;
graph.paddingLeft = 0.0f;
graph.paddingTop = 0.0f;
graph.paddingRight = 0.0f;
graph.paddingBottom = 0.0f;
graph.axisSet = nil;
graph.plotAreaFrame.borderLineStyle = nil;
// 2 - Set up text style
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.color = [CPTColor grayColor];
textStyle.fontName = #"Helvetica-Bold";
textStyle.fontSize = 16.0f;
self.selectedTheme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[graph applyTheme:self.selectedTheme];
///********************** End of Pie Chart* ***************//////
}
-(void)configureChart {
// 1 - Get reference to graph
CPTGraph *graph = self.hostView.hostedGraph;
// 2 - Create chart
CPTPieChart *piePlot = [[CPTPieChart alloc] init];
piePlot.dataSource = self;
piePlot.delegate = self;
piePlot.pieRadius = (self.hostView.bounds.size.height * 0.7) / 2;
piePlot.identifier = graph.title;
piePlot.startAngle = 0;
piePlot.endAngle = M_PI*2;//2 pi is 1 circle
piePlot.sliceDirection = CPTPieDirectionClockwise;
piePlot.identifier = #"Pie Chart";
graph.plotAreaFrame.borderLineStyle = nil;
[CPTAnimation animate:piePlot
property:#"startAngle"
from:0
to:M_PI*2
duration:0.5];
[graph addPlot:piePlot];
}

altering trajectory using UIKit Dynamics ios7

While trying to replicate the game “Breakout” using ios7’s UIKit Dynamics, I have not been able to figure out how to alter an items trajectory.
In the game, the trajectory of the ball is determined based on where it strikes the paddle. What method/s should I call to alter regular trajectory calculations.
What I have so far is copied below. Thanks for your help.
viewDidLoad.m
- (void)viewDidLoad
{
[super viewDidLoad];
unstruckBlocks = [NSMutableArray array];
[self drawBlocks];
gameHasBegun = NO;
// Set ball image
NSString *ballImagePath = [[NSBundle mainBundle] pathForResource:#"ball" ofType:#"png" inDirectory:#"pongAssets"];
ballView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:ballImagePath]];
[ballView setOpaque:NO];
// Set paddle image
NSString *paddleImagePath = [[NSBundle mainBundle] pathForResource:#"paddle" ofType:#"png" inDirectory:#"pongAssets"];
paddleView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:paddleImagePath]];
[paddleView setOpaque:NO];
dynamicAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
paddleDynamicBehavior = [[UIDynamicItemBehavior alloc] initWithItems:#[paddleView]];
ballDynamicBehavior = [[UIDynamicItemBehavior alloc] initWithItems:#[ballView]];
blockDynamicBehavior = [[UIDynamicItemBehavior alloc] initWithItems:unstruckBlocks];
pushBehavior = [[UIPushBehavior alloc] initWithItems:#[ballView] mode:UIPushBehaviorModeInstantaneous];
collisionBehavior = [[UICollisionBehavior alloc] initWithItems:unstruckBlocks];
collisionBehavior.collisionDelegate = self;
[collisionBehavior addItem:paddleView];
[collisionBehavior addItem:ballView];
paddleDynamicBehavior.allowsRotation = NO;
paddleDynamicBehavior.density = 100000000;
paddleDynamicBehavior.elasticity = 1.0;
[dynamicAnimator addBehavior:paddleDynamicBehavior];
ballDynamicBehavior.allowsRotation = NO;
ballDynamicBehavior.elasticity = 1.0;
ballDynamicBehavior.friction = 0.0;
ballDynamicBehavior.resistance = 0.0;
[dynamicAnimator addBehavior:ballDynamicBehavior];
blockDynamicBehavior.allowsRotation = NO;
blockDynamicBehavior.elasticity = 1.0;
blockDynamicBehavior.friction = 0.0;
blockDynamicBehavior.resistance = 0.0;
blockDynamicBehavior.density = 100000000;
[dynamicAnimator addBehavior:blockDynamicBehavior];
pushBehavior.pushDirection = CGVectorMake(0.3, 0.9);
pushBehavior.magnitude = 0.5;
pushBehavior.active = NO;
[dynamicAnimator addBehavior:pushBehavior];
collisionBehavior.collisionMode = UICollisionBehaviorModeEverything;
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[dynamicAnimator addBehavior:collisionBehavior];
}
I think u should modify your code as
pushBehavior.active = Yes;
it might helps you.

add an animation to bar plot does not take effect visually

I am a newer to core plot. And recently I decide to add bar growth animation to my plot. so I followed the instruction searched here to implement the animation. Now I come up with the problem: the animation seemed not to take effect.The delegate: didStopAnimate:(BOOL) returns no matter how long I set the duration of the animation with flag false.
One more thing to state: in the graph view, I put a scrollview under the real graph hosting view. does that affect Core animation to work?
the plot appeared after a navigation from the table view to itself. here is some of my codes:
if ([plot_type isEqualToString:#"bar"]) {
CPTBarPlot *plot = [[CPTBarPlot alloc] init];
//id
plot.identifier = [d objectForKey:#"title"];
plot.title = [d objectForKey:#"title"];
plot.lineStyle = barLineStyle;
plot.barCornerRadius = 1.2;
//set color
CPTGradient *gradient = [CPTGradient gradientWithBeginningColor:[CPTColor colorWithCGColor:color_begin] endingColor:[CPTColor colorWithCGColor:color_end]];
gradient.angle = 90.0f;
CPTFill *fill = [CPTFill fillWithGradient:gradient];
plot.fill = fill;
//bar width
double width = [[d objectForKey:#"width"] doubleValue]==0.0?0.6:[[d objectForKey:#"width"] doubleValue];
plot.barWidth = CPTDecimalFromFloat(width);
plot.barsAreHorizontal = NO;
//need manually stacked
if (need_stack && !is_first) {
plot.barBasesVary = YES;
} else {
plot.barBasesVary = NO;
}
//data source
plot.dataSource = self;
//delegate
plot.delegate = self;
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:#"transform"];
[anim setDuration:2.0f];
anim.toValue = [NSNumber numberWithFloat:1];
anim.fromValue = [NSNumber numberWithFloat:0.0f];
anim.removedOnCompletion = NO;
anim.delegate = self;
anim.fillMode = kCAFillModeForwards;
[plot addAnimation:anim forKey:(NSString *)plot.identifier];
[graph addPlot:plot toPlotSpace:plotSpace];
}
Thanks for your helps in advance.
The transform property of a CALayer is CATransform3D struct, not a scalar value. You should wrap it using the NSValue method +valueWithCATransform3D:.

Cocoa Touch CorePlot scatter plot graph not showing

I've been using CorePlot to draw a pie chart before which works fine. I would now like to visualise data in a scatter plot beneath my already existing pie chart. To that end I have added a new view with InterfaceBuilder, identical to the one already hosting my pie chart. I've set the data source and the delegate to self and the corresponding delegate methods are called correctly. The seemingly only thing that is not working is the actual display of the graph in the hosting view.
Here's what it looks like. Top: the working pie chart; bottom: what should be my scatter plot:
The only visible thing of the scatter plot is the small rectangle.
Here's the corresponding code:
- (id)init
{
// ...
sp1 = [[MyPlotClass alloc] initWithTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
// the following returns an NSArray. Everything working as expected here.
pieChartData = [self getEMDTurnoverSums];
[pieChartData retain];
sp2 = [[MyPlotClass alloc] initWithTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
// dito getEMDTurnoverSums
scatterPlotData = [self getEMDTurnoverData];
[scatterPlotData retain];
return self;
}
- (void)viewDidLoad
{
/*
* ------------------------
* Pie Chart
* ------------------------
*/
graph1 = [sp1 getPieChartGraphForView:graphView1];
CPTPieChart *pieChart = [sp1 initPieChartWithIdentifier:#"emd" andLineWidth:1.0f];
pieChart.dataSource = self;
pieChart.delegate = self;
CPTLegend *legend = [CPTLegend legendWithGraph:graph1];
legend.numberOfColumns = 1;
legend.numberOfRows = 3;
legend.cornerRadius = 5.0;
CPTMutableTextStyle *textStyle = [[[CPTMutableTextStyle alloc] init] autorelease];
textStyle.fontSize = 15.0;
legend.textStyle = textStyle;
graph1.legend = legend;
graph1.legendAnchor = CPTRectAnchorLeft;
graph1.legendDisplacement = CGPointMake(30.0, 30.0);
if (self.interfaceOrientation == UIDeviceOrientationLandscapeLeft ||
self.interfaceOrientation == UIDeviceOrientationLandscapeRight)
{
graphView1.frame = CGRectMake(5.0f, 500.0f, 750.0f, 510.0f);
}
[graph1 addPlot:pieChart];
[pieChart release];
/*
* ------------------------
* Scatter Plot
* ------------------------
*/
graph2 = [sp2 getScatterPlotGraphForView:graphView2];
CPTScatterPlot *scatterPlot = [sp2 initScatterPlotWithIdentifier:#"scatterPlot" andLineWidth:5.0f andColor:[CPTColor blackColor]];
scatterPlot.dataSource = self;
scatterPlot.delegate = self;
CPTLegend *scatterPlotLegend = [CPTLegend legendWithGraph:graph2];
scatterPlotLegend.numberOfColumns = 2;
scatterPlotLegend.numberOfRows = 3;
scatterPlotLegend.cornerRadius = 5.0;
// see PieChart
scatterPlotLegend.textStyle = textStyle;
graph2.legend = scatterPlotLegend;
graph2.legendAnchor = CPTRectAnchorLeft;
graph2.legendDisplacement = CGPointMake(30.0, 30.0);
if (self.interfaceOrientation == UIDeviceOrientationLandscapeLeft ||
self.interfaceOrientation == UIDeviceOrientationLandscapeRight)
{
graphView2.frame = CGRectMake(5.0f, 240.0f, 750.0f, 270.0f);
}
[graph2 addPlot:scatterPlot];
[scatterPlot release];
/*
* ------------------------
* END PLOTS
* ------------------------
*/
[super viewDidLoad];
}
The delegate methods:
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
if ([(NSString *)plot.identifier isEqualToString:#"emd"]) {
NSLog(#"numberOfRecordsForPlot: emd: %i", [pieChartData count]);
return [pieChartData count];
} else if ([(NSString *)plot.identifier isEqualToString:#"scatterPlot"]) {
NSLog(#"numberOfRecordsForPlot: scatterPlot: %i", [scatterPlotData count]);
NSLog(#"numberOfRecordsForPlot: scatterPlot: %#", scatterPlotData);
return [scatterPlotData count];
}
return 0;
}
- (NSNumber *)numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index
{
if ([(NSString *)plot.identifier isEqualToString:#"emd"]) {
return [pieChartData objectAtIndex:index];
} else if ([(NSString *)plot.identifier isEqualToString:#"scatterPlot"]) {
NSDictionary *fieldMapping = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
#"e_turnover",
#"m_turnover",
#"d_turnover",
#"na_turnover",
#"sum_turnover",
nil]
forKeys:[NSArray arrayWithObjects:
#"0",
#"1",
#"2",
#"3",
#"4",
nil]];
NSLog(#"scatterPlotData: %#", scatterPlotData);
NSLog(#"fieldMapping : %#", fieldMapping);
NSNumber *returnValue = [(NSDictionary *)[scatterPlotData objectAtIndex:index] objectForKey:
[fieldMapping objectForKey:
[NSString stringWithFormat:#"%i", fieldEnum]]];
NSLog(#"returnValue: %#", returnValue);
return returnValue;
}
return 0;
}
- (NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart
recordIndex:(NSUInteger)index
{
switch (index) {
case 0:
return [NSString stringWithFormat:#"Easy Lines - %# €", [self standardFormatNumber:(NSNumber *)[pieChartData objectAtIndex:0] asDecimal:NO]];
case 1:
return [NSString stringWithFormat:#"Medium Lines - %# €", [self standardFormatNumber:(NSNumber *)[pieChartData objectAtIndex:1] asDecimal:NO]];
case 2:
return [NSString stringWithFormat:#"Difficult Lines - %# €", [self standardFormatNumber:(NSNumber *)[pieChartData objectAtIndex:2] asDecimal:NO]];
default:
break;
}
return #"";
}
- (CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart
recordIndex:(NSUInteger)index
{
switch (index) {
case 0:
return [CPTFill fillWithColor:[CPTColor greenColor]];
case 1:
return [CPTFill fillWithColor:[CPTColor yellowColor]];
case 2:
return [CPTFill fillWithColor:[CPTColor redColor]];
default:
break;
}
return [CPTFill fillWithColor:[CPTColor blackColor]];
}
Here's the code from MyPlotClass.m:
#import "MyPlotClass.h"
#implementation MyPlotClass
#synthesize paddingLeft;
#synthesize paddingTop;
#synthesize paddingRight;
#synthesize paddingBottom;
#synthesize plotRangeXFrom;
#synthesize plotRangeXLength;
#synthesize plotRangeYFrom;
#synthesize plotRangeYLength;
#synthesize plotSpace;
#synthesize axisLineWidth;
#synthesize axisLineColor;
#synthesize minorTickLengthX;
#synthesize majorTickLengthX;
#synthesize minorTickLengthY;
#synthesize majorTickLengthY;
#synthesize majorIntervalLengthX;
#synthesize majorIntervalLengthY;
#synthesize labelOffsetX;
#synthesize labelOffsetY;
#synthesize minorTicksPerIntervalX;
#synthesize minorTicksPerIntervalY;
#synthesize pieChartRadius;
#synthesize pieChartStartAngle;
#synthesize pieChartSliceDirection;
- (id)init
{
return [self initWithTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
}
- (MyPlotClass *)initWithTheme:(CPTTheme *)theme
{
graph = [[[CPTXYGraph alloc] initWithFrame:CGRectZero] retain];
[graph applyTheme:theme];
paddingLeft = 20.0;
paddingTop = 20.0;
paddingRight = 20.0;
paddingBottom = 20.0;
plotRangeXFrom = 0.0;
plotRangeXLength = 0.0;
plotRangeYFrom = 0.0;
plotRangeYLength = 0.0;
axisLineWidth = 0.0f;
axisLineColor = [CPTColor blackColor];
minorTickLengthX = 0.0f;
majorTickLengthX = 0.0f;
minorTickLengthY = 0.0f;
majorTickLengthY = 0.0f;
majorIntervalLengthX = 5.0f;
majorIntervalLengthY = 5.0f;
labelOffsetX = 3.0f;
labelOffsetY = 3.0f;
minorTicksPerIntervalX = 0;
minorTicksPerIntervalY = 0;
pieChartRadius = 100.0;
pieChartStartAngle = M_PI;
pieChartSliceDirection = CPTPieDirectionClockwise;
return self;
}
- (CPTXYGraph *)getScatterPlotGraphForView:(UIView *)view
{
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)view;
hostingView.hostedGraph = graph;
graph.paddingLeft = paddingLeft;
graph.paddingTop = paddingTop;
graph.paddingRight = paddingRight;
graph.paddingBottom = paddingBottom;
plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(plotRangeXFrom)
length:CPTDecimalFromFloat(plotRangeXLength)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(plotRangeYFrom)
length:CPTDecimalFromFloat(plotRangeYLength)];
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineColor = axisLineColor;
axisLineStyle.lineWidth = axisLineWidth;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
axisSet.xAxis.majorIntervalLength = [[NSNumber numberWithFloat:majorIntervalLengthX] decimalValue];
axisSet.xAxis.minorTicksPerInterval = minorTicksPerIntervalX;
axisSet.xAxis.majorTickLineStyle = axisLineStyle;
axisSet.xAxis.minorTickLineStyle = axisLineStyle;
axisSet.xAxis.axisLineStyle = axisLineStyle;
axisSet.xAxis.minorTickLength = minorTickLengthX;
axisSet.xAxis.majorTickLength = majorTickLengthX;
axisSet.xAxis.labelOffset = labelOffsetX;
axisSet.yAxis.majorIntervalLength = [[NSNumber numberWithInt:majorIntervalLengthY] decimalValue];
axisSet.yAxis.minorTicksPerInterval = minorTicksPerIntervalY;
axisSet.yAxis.majorTickLineStyle = axisLineStyle;
axisSet.yAxis.minorTickLineStyle = axisLineStyle;
axisSet.yAxis.axisLineStyle = axisLineStyle;
axisSet.yAxis.minorTickLength = minorTickLengthY;
axisSet.yAxis.majorTickLength = majorTickLengthY;
axisSet.yAxis.labelOffset = labelOffsetY;
return graph;
}
- (CPTXYGraph *)getPieChartGraphForView:(UIView *)view
{
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)view;
hostingView.hostedGraph = graph;
graph.paddingLeft = paddingLeft;
graph.paddingTop = paddingTop;
graph.paddingRight = paddingRight;
graph.paddingBottom = paddingBottom;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineColor = axisLineColor;
axisLineStyle.lineWidth = axisLineWidth;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
axisSet.xAxis.majorIntervalLength = [[NSNumber numberWithFloat:majorIntervalLengthX] decimalValue];
axisSet.xAxis.minorTicksPerInterval = minorTicksPerIntervalX;
axisSet.xAxis.majorTickLineStyle = axisLineStyle;
axisSet.xAxis.minorTickLineStyle = axisLineStyle;
axisSet.xAxis.axisLineStyle = axisLineStyle;
axisSet.xAxis.minorTickLength = minorTickLengthX;
axisSet.xAxis.majorTickLength = majorTickLengthX;
axisSet.xAxis.labelOffset = labelOffsetX;
axisSet.yAxis.majorIntervalLength = [[NSNumber numberWithInt:majorIntervalLengthY] decimalValue];
axisSet.yAxis.minorTicksPerInterval = minorTicksPerIntervalY;
axisSet.yAxis.majorTickLineStyle = axisLineStyle;
axisSet.yAxis.minorTickLineStyle = axisLineStyle;
axisSet.yAxis.axisLineStyle = axisLineStyle;
axisSet.yAxis.minorTickLength = minorTickLengthY;
axisSet.yAxis.majorTickLength = majorTickLengthY;
axisSet.yAxis.labelOffset = labelOffsetY;
return graph;
}
- (CPTScatterPlot *)initScatterPlotWithIdentifier:(NSString *)identifier
{
return [self initScatterPlotWithIdentifier:identifier andLineWidth:3.0f andColor:[CPTColor blackColor]];
}
- (CPTScatterPlot *)initScatterPlotWithIdentifier:(NSString *)identifier
andLineWidth:(CGFloat)lineWidth
andColor:(CPTColor *)lineColor
{
CPTScatterPlot *scatterPlot = [[[CPTScatterPlot alloc]
initWithFrame:graph.defaultPlotSpace.accessibilityFrame]
autorelease];
scatterPlot.identifier = identifier;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = lineWidth;
lineStyle.lineColor = lineColor;
scatterPlot.dataLineStyle = lineStyle;
[graph addPlot:scatterPlot];
CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
scatterPlot.plotSymbol = greenCirclePlotSymbol;
return scatterPlot;
}
- (CPTPieChart *)initPieChartWithIdentifier:(NSString *)identifier
andLineWidth:(CGFloat)lineWidth
{
CPTPieChart *pieChart = [[[CPTPieChart alloc]
initWithFrame:graph.defaultPlotSpace.accessibilityFrame]
autorelease];
pieChart.identifier = identifier;
pieChart.pieRadius = pieChartRadius;
pieChart.startAngle = pieChartStartAngle;
pieChart.sliceDirection = pieChartSliceDirection;
// Prepare a radial overlay gradient for shading/gloss
CPTGradient *overlayGradient = [[[CPTGradient alloc] init] autorelease];
overlayGradient.gradientType = CPTGradientTypeRadial;
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.0];
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.3] atPosition:0.9];
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.7] atPosition:1.0];
pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = lineWidth;
pieChart.borderLineStyle = lineStyle;
[graph addPlot:pieChart];
return pieChart;
}
- (void)dealloc
{
[super dealloc];
[graph release];
[plotSpace release];
}
#end
I've spent hours on this and haven't yet been able to pin down the problem. The data is fine, the delegate methods get called and return correct values.
TIA!
A few observations:
In -initScatterPlotWithIdentifier:andLineWidth:andColor:, the scatter plot is added to graph. Shouldn't that be graph2?
In -numberForPlot:field:recordIndex:, you have to check the field parameter—a switch statement works well. This method will be called twice for each index, once with field == CPTScatterPlotFieldX and once with field == CPTScatterPlotFieldY. You're returning the same value for both fields, so even if you could see the plot, it would be a diagonal line.
axisLineWidth = 0.0f; You'll need to increase this for the scatter plot so the axis lines show up.
#Eric Skroch:
Thanks for your comment!
No, -initScatterPlotWithIdentifier:andLineWidth:andColor: is a method in MyPlotClass. The member graph there has nothing to do with the variable in the implementing class.
I actually do that in the return statement. I think however that I sacrificed readability here for wanting to be elegant. The following code is more obvious now:
NSDictionary *curDict = (NSDictionary *)[scatterPlotData objectAtIndex:index];
switch (fieldEnum) {
case 0:
return [curDict objectForKey:#"e_turnover"];
case 1:
return [curDict objectForKey:#"m_turnover"];
case 2:
return [curDict objectForKey:#"d_turnover"];
case 3:
return [curDict objectForKey:#"na_turnover"];
case 4:
return [curDict objectForKey:#"sum_turnover"];
default:
break;
}
BTW: Sorry that I put this comment in a separate answer but the comment field won't let me enter multi line code blocks or comments that exceed a certain amount of characters.

How to set chart width with Core Plot?

I have a nib file with File's Owner set to this view and it only has one View that has a custom class CPTGraphHostingView set. It is connected to this view via an outlet. This view loads, but what it displays is a very tiny bar graph. And I am really unsure why. Also, anything that is added to the view in IB turns up upside down.
At which point is the chart getting a width set to it? I assume that using self.view.bounds would just make the chart use up all available screen space.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:#"PollResultView" bundle:nil];
self.title = #"Results";
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
CPTXYPlotSpace *barPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(100)];
barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(30)];
[graph addPlotSpace:barPlotSpace];
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 1.0f;
majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.75f];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 1.0f;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.25f];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromInteger(10);
x.minorTicksPerInterval = 9;
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.labelOffset = 10.0f;
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f)
length:CPTDecimalFromFloat(10.0f)];
x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)
length:CPTDecimalFromFloat(100.0f)];
x.title = #"X Axis";
x.titleOffset = 30.0f;
x.titleLocation = CPTDecimalFromFloat(5.0f);
x.plotSpace = barPlotSpace;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromInteger(10);
y.minorTicksPerInterval = 9;
y.orthogonalCoordinateDecimal = CPTDecimalFromInteger(-0.5);
y.preferredNumberOfMajorTicks = 8;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.labelOffset = 10.0f;
y.labelRotation = M_PI/2;
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)
length:CPTDecimalFromFloat(100.0f)];
y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f)
length:CPTDecimalFromFloat(10.0f)];
y.title = #"Y Axis";
y.titleOffset = 30.0f;
y.titleLocation = CPTDecimalFromInteger(55);
y.plotSpace = barPlotSpace;
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil];
CPTMutableLineStyle *barLineStyle = [[[CPTMutableLineStyle alloc] init] autorelease];
barLineStyle.lineWidth = 1.0f;
barLineStyle.lineColor = [CPTColor whiteColor];
CPTBarPlot *barPlot = [[[CPTBarPlot alloc] init] autorelease];
barPlot.lineStyle = barLineStyle;
barPlot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.0f
green:0.0f
blue:0.5f
alpha:0.5f]];
barPlot.barBasesVary = YES;
barPlot.barWidth = CPTDecimalFromFloat(0.5f); // bar is 50% of the available space
barPlot.barCornerRadius = 10.0f;
barPlot.barsAreHorizontal = NO;
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
barPlot.barLabelTextStyle = whiteTextStyle;
// barPlot.delegate = self;
barPlot.dataSource = self;
barPlot.identifier = #"Bar Plot 1";
[graph addPlot:barPlot toPlotSpace:barPlotSpace];
CPTBarPlot *barPlot2 = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor]
horizontalBars:NO];
barPlot2.lineStyle = barLineStyle;
barPlot2.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.0f
green:1.0f
blue:0.5f
alpha:0.5f]];
barPlot2.barBasesVary = YES;
barPlot2.barWidth = CPTDecimalFromFloat(1.0f);
barPlot2.barCornerRadius = 2.0f;
barPlot2.barsAreHorizontal = NO;
//barPlot2.delegate = self;
barPlot2.dataSource = self;
barPlot2.identifier = #"Bar Plot 2";
[graph addPlot:barPlot2 toPlotSpace:barPlotSpace];
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
theLegend.numberOfRows = 2;
theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]];
theLegend.borderLineStyle = barLineStyle;
theLegend.cornerRadius = 10.0;
theLegend.swatchSize = CGSizeMake(20.0, 20.0);
whiteTextStyle.fontSize = 16.0;
theLegend.textStyle = whiteTextStyle;
theLegend.rowMargin = 10.0;
theLegend.paddingLeft = 12.0;
theLegend.paddingTop = 12.0;
theLegend.paddingRight = 12.0;
theLegend.paddingBottom = 12.0;
NSArray *plotPoint = [NSArray arrayWithObjects:[NSNumber numberWithInteger:0], [NSNumber numberWithInteger:95], nil];
CPTPlotSpaceAnnotation *legendAnnotation = [[[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:barPlotSpace anchorPlotPoint:plotPoint] autorelease];
legendAnnotation.contentLayer = theLegend;
legendAnnotation.contentAnchorPoint = CGPointMake(0.0f, 1.0f);
[graph.plotAreaFrame.plotArea addAnnotation:legendAnnotation];
graphHostingView.hostedGraph = graph;
}
The hosting view will make the graph fill it's bounds automatically. It doesn't matter what you use when you initialize it as long as the hosting view is sized to its container correctly. There are several things going on here:
You're using white gridlines on a white background. They're hard to see that way. :-) Change the line colors or set a fill on the graph that's a different color.
Copy/paste error on the y-axis:
x.axisLineStyle = nil; x.majorTickLineStyle = nil; x.minorTickLineStyle = nil;
should be
y.axisLineStyle = nil; y.majorTickLineStyle = nil; y.minorTickLineStyle = nil;
There's no need to set the visible range for your axes--you're not drawing them anyway.
There's no need to set the gridLinesRange either--the default is the plot range which is what you're using.
With barBasesVary, make sure your datasource also provides data for the CPTBarPlotFieldBarBase field.
When using a plot space annotation, the anchor point for the legend has to be within the plot space ranges (currently x: [0, 100] and y: [0, 30]) or it won't be visible.
You need to set the padding on graph.plotAreaFrame to make the axis labels and titles visible. You can also adjust the padding on graph to set the overall margin outside the plot area frame.
Eric
You're setting it to a very small graph with these commands:
barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(100)];
barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(30)];
100 pixels wide and 30 pixels high is very small, for the actual plot area.
My code uses graph = [CPTXYGraph alloc] initWithFrame:CGRectZero]; for the initial setup, and it works fine. I think there's some code in the implementation that sets the size to the containing view when that happens.
joe