I've got a pie chart being generated through Coreplot. It's put a black line all the way around the chart and through all the segments like so:
I generate the chart with the following code:
-(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index
{
CPTFill *areaGradientFill ;
if (index==0)
return areaGradientFill= [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.0f/255.0f green:111.0f/255.0f blue:115.0f/255.0f alpha:1.0f]];
else if (index==1)
return areaGradientFill= [CPTFill fillWithColor:[CPTColor colorWithComponentRed:171.0f/255.0f green:213.0f/255.0f blue:199.0f/255.0f alpha:1.0f]];
else if (index==2)
return areaGradientFill= [CPTFill fillWithColor:[CPTColor yellowColor]];
return areaGradientFill;
}
-(void)constructPieChart:(NSString *)datain;
{
NSArray *strings = [datain componentsSeparatedByString:#","];
dataforcharts = [datain componentsSeparatedByString:#","];
NSLog(#"dataforcharts: %#", dataforcharts);
// Create pieChart from theme
pieGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[pieGraph applyTheme:theme];
pieChartView.hostedGraph = pieGraph;
pieGraph.plotAreaFrame.borderLineStyle = nil;
pieGraph.plotAreaFrame.masksToBorder = NO;
pieGraph.paddingLeft = 20.0;
pieGraph.paddingTop = 20.0;
pieGraph.paddingRight = 20.0;
pieGraph.paddingBottom = 20.0;
pieGraph.axisSet = nil;
// Prepare a radial overlay gradient for shading/gloss
CPTGradient *overlayGradient = [[CPTGradient alloc] init];
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];
// Add pie chart
piePlot = [[CPTPieChart alloc] init];
piePlot.dataSource = self;
piePlot.pieRadius = 100.0;
piePlot.pieInnerRadius = 50.0;
piePlot.identifier = #"Pie Chart 1";
piePlot.startAngle = M_PI_4;
// piePlot.sliceDirection = CPTPieDirectionCounterClockwise;
piePlot.borderLineStyle = [CPTLineStyle lineStyle];
piePlot.labelOffset = -40.0;
piePlot.overlayFill = [CPTFill fillWithGradient:overlayGradient];
[pieGraph addPlot:piePlot];
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.color = [CPTColor blackColor];
textStyle.fontName = #"Helvetica-Bold";
textStyle.fontSize = 16.0f;
pieGraph.title=#"Chart1";
pieGraph.titleTextStyle = textStyle;
pieGraph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
pieGraph.titleDisplacement = CGPointMake(0.0f, -1.0f);
int i = [[strings objectAtIndex:0] intValue];
int f = [[strings objectAtIndex:1] intValue];
NSLog(#"Int1: %i", i);
NSLog(#"Int2: %i", f);
// Add some initial data
NSMutableArray *contentArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithDouble:i], [NSNumber numberWithDouble:f], nil];
self.dataForChart = contentArray;
CPTLegend *theLegend = [CPTLegend legendWithGraph:pieGraph];
theLegend.numberOfColumns = 2;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.cornerRadius = 5.0;
pieGraph.legend = theLegend;
pieGraph.legendAnchor = CPTRectAnchorBottom;
pieGraph.legendDisplacement = CGPointMake(0.0, -0.0);
}
and I'd like to remove the black lines around the chart. I've already tried setting the borderLineStyle to nil but this doesn't seem to have any effect.
Any help is appreciated.
Thanks!
Adam
Use this and debug again, it's working.
piePlot.borderLineStyle = nil;
Related
I have a graph wtih scatter plot and barplot and I have two y axis. I wnt to be able to scroll horizontally without the y axes moves. When I have only one y axis my code works but when I add the second one. The second still moving up and down. Have you some idee?
Here is my code :
-(void) configureAxes:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme{
[super configureAxes:layerHostingView withTheme:theme];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) layerHostingView.hostedGraph.axisSet;
CPTAxis *x = axisSet.xAxis;
CPTAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.labelingPolicy= CPTAxisLabelingPolicyNone;
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:self.selectedExplYear.exploitation.count];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:self.selectedExplYear.exploitation.count];
for (int i = 0; i<self.selectedExplYear.exploitation.count; i++) {
SCExploitation *tech = self.selectedExplYear.exploitation[i];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM"];
NSDate *dateExploitation = [NSDate dateWithTimeIntervalSince1970:tech.exploitationDate.doubleValue/1000];
NSString *mois = [df stringFromDate:dateExploitation];
CPTAxisLabel *label = [[CPTAxisLabel alloc]initWithText:[NSString stringWithFormat:#"%#", mois] textStyle:x.labelTextStyle];
CGFloat location = i;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = x.majorTickLength;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
NSNumberFormatter *Xformatter = [[NSNumberFormatter alloc] init];
[Xformatter setGeneratesDecimalNumbers:NO];
[Xformatter setNumberStyle:NSNumberFormatterDecimalStyle];
y.labelFormatter = Xformatter;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) layerHostingView.hostedGraph.defaultPlotSpace;
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = #"Helvetica-Bold";
axisTitleStyle.fontSize = 14.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = #"Helvetica-Bold";
axisTextStyle.fontSize = 14.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor];
tickLineStyle.lineWidth = 2.0f;
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
x.axisLabels=xLabels;
x.majorTickLocations = xLocations;
axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:5];
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(250)];
CPTXYAxis *y2 = [[CPTXYAxis alloc] init];
y2.coordinate = CPTCoordinateY;
y2.plotSpace = self.scatterGraphPlotSpace;
y2.axisConstraints = [CPTConstraints constraintWithUpperOffset:0];
y2.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
y2.axisLineStyle = axisLineStyle;
y2.majorTickLineStyle = axisLineStyle;
y2.title = #"Km";
y2.majorIntervalLength = CPTDecimalFromFloat((80 - 0) / 5.0f);
y2.labelFormatter = Xformatter;
y2.titleTextStyle = axisTextStyle;
y2.labelTextStyle = axisTextStyle;
y2.tickDirection = CPTSignNone;
y2.tickLabelDirection = CPTSignPositive;
y2.orthogonalCoordinateDecimal = CPTDecimalFromFloat(5.0);
y2.titleOffset = 16.0 * CPTFloat(-2.1);
y2.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(80)];
self.scatterGraphPlotSpace.xRange = plotSpace.xRange;
layerHostingView.hostedGraph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
}
-(void)configurePlots:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme
{
CPTGraph *graph = layerHostingView.hostedGraph;
CPTBarPlot *barPlotTotalDetenteEau = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
barPlotTotalDetenteEau.identifier = DETENTEEAUIDENTIFIER;
CPTMutableLineStyle *lsEauPotable = [[CPTMutableLineStyle alloc]init];
lsEauPotable.lineColor = [CPTColor lightGrayColor];
lsEauPotable.lineWidth = 0.5;
barPlotTotalDetenteEau.lineStyle = lsEauPotable;
barPlotTotalDetenteEau.dataSource =self;
barPlotTotalDetenteEau.delegate = self;
barPlotTotalDetenteEau.zPosition = 90;
barPlotTotalDetenteEau.labelOffset = LABELOFFSET;
barPlotTotalDetenteEau.title = NSLocalizedString(#"exploitation_detente_eau", nil);
barPlotTotalDetenteEau.barWidth = CPTDecimalFromDouble(BARWIDTH);
barPlotTotalDetenteEau.barOffset = CPTDecimalFromDouble(0);
[graph addPlot:barPlotTotalDetenteEau toPlotSpace:graph.defaultPlotSpace];
CPTBarPlot *barPlotTotalControleGaz = [CPTBarPlot tubularBarPlotWithColor:[CPTColor yellowColor] horizontalBars:NO];
barPlotTotalControleGaz.identifier = CONTROLEGAZIDENTIFIER;
CPTMutableLineStyle *lsNouveau = [[CPTMutableLineStyle alloc]init];
lsNouveau.lineColor = [CPTColor lightGrayColor];
lsNouveau.lineWidth = 0.5;
barPlotTotalControleGaz.lineStyle = lsNouveau;
barPlotTotalControleGaz.dataSource =self;
barPlotTotalControleGaz.delegate = self;
barPlotTotalControleGaz.zPosition = 100;
barPlotTotalControleGaz.labelOffset = LABELOFFSET;
barPlotTotalControleGaz.title = NSLocalizedString(#"exploitation_controle_gaz", nil);
barPlotTotalControleGaz.barWidth = CPTDecimalFromDouble(BARWIDTH);
barPlotTotalControleGaz.barOffset = CPTDecimalFromDouble(BARWIDTH+0.01);
[graph addPlot:barPlotTotalControleGaz toPlotSpace:graph.defaultPlotSpace];
self.scatterGraphPlotSpace = [[CPTXYPlotSpace alloc] init];
self.scatterGraphPlotSpace.allowsUserInteraction =YES;
// self.scatterGraphPlotSpace.delegate =self;
CPTScatterPlot *controlerPlot = [[CPTScatterPlot alloc] init];
controlerPlot.dataSource = self;
controlerPlot.delegate = self;
controlerPlot.title = NSLocalizedString(#"km_fuite", nil);
controlerPlot.identifier = KMCONTROLERIDENTIFIER;
controlerPlot.plotSymbolMarginForHitDetection = 15.0f;
CPTColor *effectivePlotColor = [CPTColor redColor];
[graph addPlot:controlerPlot toPlotSpace:self.scatterGraphPlotSpace];
CPTBarPlot *barPlotTotalControleEau = [CPTBarPlot tubularBarPlotWithColor:[CPTColor greenColor] horizontalBars:NO];
barPlotTotalControleEau.identifier = CONTROLEEAUIDENTIFIER;
CPTMutableLineStyle *lsProvisoire = [[CPTMutableLineStyle alloc]init];
lsProvisoire.lineColor = [CPTColor lightGrayColor];
lsProvisoire.lineWidth = 0.5;
barPlotTotalControleEau.lineStyle = lsProvisoire;
barPlotTotalControleEau.dataSource = self;
barPlotTotalControleEau.delegate = self;
barPlotTotalControleEau.zPosition = 100;
barPlotTotalControleEau.labelOffset = LABELOFFSET;
barPlotTotalControleEau.title = NSLocalizedString(#"exploitation_controle_eau", nil);
barPlotTotalControleEau.barWidth = CPTDecimalFromDouble(BARWIDTH);
barPlotTotalControleEau.barOffset = CPTDecimalFromDouble(0);
[graph addPlot:barPlotTotalControleEau toPlotSpace:graph.defaultPlotSpace];
CPTBarPlot *barPlotTotalDetenteGaz = [CPTBarPlot tubularBarPlotWithColor:[CPTColor grayColor] horizontalBars:NO];
barPlotTotalDetenteGaz.identifier = DETENTEGAZIDENTIFIER;
CPTMutableLineStyle *lsAugmentation = [[CPTMutableLineStyle alloc]init];
lsAugmentation.lineColor = [CPTColor lightGrayColor];
lsAugmentation.lineWidth = 0.5;
barPlotTotalDetenteGaz.lineStyle = lsAugmentation;
barPlotTotalDetenteGaz.dataSource =self;
barPlotTotalDetenteGaz.delegate = self;
barPlotTotalDetenteGaz.zPosition = 70;
barPlotTotalDetenteGaz.labelOffset = LABELOFFSET;
barPlotTotalDetenteGaz.title = NSLocalizedString(#"exploitation_detente_gaz", nil);
barPlotTotalDetenteGaz.barWidth = CPTDecimalFromDouble(BARWIDTH);
barPlotTotalDetenteGaz.barOffset = CPTDecimalFromDouble(BARWIDTH+0.01);
[graph addPlot:barPlotTotalDetenteGaz toPlotSpace:graph.defaultPlotSpace];
NSArray *plots = #[barPlotTotalDetenteEau,barPlotTotalControleEau,barPlotTotalControleGaz,barPlotTotalDetenteGaz];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
[plotSpace scaleToFitPlots:plots];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.3f)];
xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f) length:CPTDecimalFromFloat(4.0f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.4f)];
plotSpace.yRange = yRange;
plotSpace.delegate = self;
self.scatterGraphPlotSpace.globalYRange = plotSpace.globalYRange;
CPTScatterPlot *visuelPlot = [[CPTScatterPlot alloc] init];
visuelPlot.dataSource = self;
visuelPlot.delegate = self;
visuelPlot.title = NSLocalizedString(#"km_visuel", nil);
visuelPlot.identifier = KMVISUELIDENTIFIER;
visuelPlot.plotSymbolMarginForHitDetection = 15.0f;
CPTColor *budgeteePlotColor = [CPTColor orangeColor];
CPTMutableLineStyle *budgeteelLineStyle = [CPTMutableLineStyle lineStyle]; // [budgeteePlot.dataLineStyle mutableCopy];
budgeteelLineStyle.lineWidth = 2.5;
budgeteelLineStyle.lineColor = budgeteePlotColor;
visuelPlot.dataLineStyle = budgeteelLineStyle;
CPTPlotSymbol *budgeteeSymbol = [CPTPlotSymbol ellipsePlotSymbol];
budgeteeSymbol.fill = [CPTFill fillWithColor:budgeteePlotColor];
budgeteeSymbol.lineStyle = budgeteelLineStyle;
budgeteeSymbol.size = CGSizeMake(6.0f, 6.0f);
visuelPlot.plotSymbol = budgeteeSymbol;
CPTMutableLineStyle *effectiveLineStyle = [CPTMutableLineStyle lineStyle];
effectiveLineStyle.lineWidth = 2.5;
effectiveLineStyle.lineColor = effectivePlotColor;
controlerPlot.dataLineStyle = effectiveLineStyle;
CPTPlotSymbol *effectiveSymbol = [CPTPlotSymbol ellipsePlotSymbol];
effectiveSymbol.fill = [CPTFill fillWithColor:effectivePlotColor];
effectiveSymbol.lineStyle = effectiveLineStyle;
effectiveSymbol.size = CGSizeMake(6.0f, 6.0f);
controlerPlot.plotSymbol = effectiveSymbol;
[self.scatterGraphPlotSpace scaleToFitPlots:[NSArray arrayWithObjects:controlerPlot, visuelPlot, nil]];
[graph addPlot:visuelPlot toPlotSpace:self.scatterGraphPlotSpace];
CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(250)];
plotSpace.globalYRange = globalYRange;
self.scatterGraphPlotSpace.yRange =[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(80)];
CPTMutablePlotRange *y2Range = [self.scatterGraphPlotSpace.yRange mutableCopy];
[y2Range expandRangeByFactor:CPTDecimalFromCGFloat(1.4f)];
self.scatterGraphPlotSpace.yRange = y2Range;
[layerHostingView.hostedGraph addPlotSpace:self.scatterGraphPlotSpace];
}
-(void)configureGraph:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme
{
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:layerHostingView.bounds];
graph.plotAreaFrame.masksToBorder = NO;
layerHostingView.hostedGraph = graph;
if(self.selectedExplYear.exploitation.count> 3){
graph.defaultPlotSpace.allowsUserInteraction = YES;
}
[graph applyTheme:self.theme];
graph.paddingBottom = 60.0f;
graph.paddingLeft = 30.0f;
graph.paddingTop = 30.0f;
graph.paddingRight = 30.0f;
graph.title = nil;
}
-(CGPoint)plotSpace:(CPTPlotSpace *)space willDisplaceBy:(CGPoint)displacement
{
return CGPointMake(displacement.x, 0);
}
-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate
{
if (space == self.scatterGraphPlotSpace) {
if (coordinate == CPTCoordinateY) {
newRange = self.scatterGraphPlotSpace.yRange;
}
}else{
if (coordinate == CPTCoordinateY) {
newRange = ((CPTXYPlotSpace*)space).yRange;
}
}
return newRange;
}
Thanks a lot
Set the delegate for self.scatterGraphPlotSpace. That line of code is commented out in the code you posted.
In my app I am using core plot. Everything is fine as below
.
On pressing next or prev button, core plot shows values of next or previous 7 days. When I press the button many times say 142 times, graph disappears and core plot only shows x and y labels as shown below..
And on tapping more app crashes. How can I fix my issue? Please help. Also in the debug area it shows
malloc: * mach_vm_map(size=8388608) failed (error code=3)
* error: can't allocate region securely
*** set a breakpoint in malloc_error_break to debug
It also gives bad access at
I don't know where my issue is.My code for generating graph is...
- (void)generateLayout
{
//Create graph from theme
self.graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
self.graph = (CPTXYGraph *)[theme newGraph];
//graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
self.graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:183.0/255.0 green:206.0/255.0 blue:227.0/255.0 alpha:1.0]];
//[graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
self.hostedGraph = self.graph;
self.graph.plotAreaFrame.masksToBorder = NO;
self.graph.paddingLeft = 0.0f;
self.graph.paddingTop = 0.0f;
self.graph.paddingRight = 0.0f;
self.graph.paddingBottom = 0.0f;
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor clearColor];
borderLineStyle.lineWidth = 2.0f;
self.graph.plotAreaFrame.borderLineStyle = borderLineStyle;
self.graph.plotAreaFrame.paddingTop = 10.0;
self.graph.plotAreaFrame.paddingRight = 10.0;
self.graph.plotAreaFrame.paddingBottom = 30.0;
self.graph.plotAreaFrame.paddingLeft = 50.0;
//Add plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.delegate = self;
/////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// Here We Can Set The Range Of Y Axis////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(greaterGlass * sets.count)];
//plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(greaterGlass * sets.count)];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(-1)
length:CPTDecimalFromInt(8)];
//Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 2.75;
majorGridLineStyle.lineColor = [[CPTColor colorWithComponentRed:113.0/255.0 green:91.0/255.0 blue:84.0/255.0 alpha:1.0] colorWithAlphaComponent:1.0];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.50;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
majorGridLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInteger:1], [NSNumber numberWithInteger:5], nil];
//Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
//X axis
CPTXYAxis *x = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromInt(0);
x.majorIntervalLength = CPTDecimalFromInt(1);
x.minorTicksPerInterval = 0;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorGridLineStyle = majorGridLineStyle;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
//X labels
int labelLocations = 0;
//NSMutableArray *customXLabels = [NSMutableArray array];
NSMutableArray *customXLabels = [NSMutableArray arrayWithCapacity:[dates count]];
//for (NSString *day in dates)
for(int couter=0; couter<[dates count]; couter++)
{
NSString *day = [NSString stringWithFormat:#"%#",[dates objectAtIndex:couter]];
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:day textStyle:x.labelTextStyle];
newLabel.tickLocation = [[NSNumber numberWithInt:labelLocations] decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength;
//newLabel.rotation = M_PI / 4;
[customXLabels addObject:newLabel];
labelLocations++;
[newLabel release];
}
x.axisLabels = [NSSet setWithArray:customXLabels];
//Y axis
CPTXYAxis *y = axisSet.yAxis;
CGRect frame = CGRectMake(0, 0, 50, 30);
UILabel *label = [[UILabel alloc] initWithFrame:frame] ;
label.font = [UIFont fontWithName:#"ComicSansMS-Bold" size:12.0];
label.textAlignment = NSTextAlignmentCenter;
label.text = #"Number of Glasses";
y.title = label.text;
y.titleOffset = 50.0f;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
//Create a bar line style
CPTMutableLineStyle *barLineStyle = [[[CPTMutableLineStyle alloc] init] autorelease];
barLineStyle.lineWidth = 1.0;
barLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
//Plot
BOOL firstPlot = YES;
for (NSString *set in [[sets allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)]) {
//UIColor *myColor = [UIColor colorWithRed:87.0/255.0 green:181.0/255.0 blue:229.0/255.0 alpha:1.0];
//CPTColor *mineColor = [CPTColor colorWithComponentRed:160 green:97 blue:5 alpha:1];
CPTBarPlot *plot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
plot.lineStyle = barLineStyle;
CGColorRef color = ((UIColor *)[sets objectForKey:set]).CGColor;
//plot.fill = [CPTFill fillWithColor:[CPTColor colorWithCGColor:color]];
plot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:111.0/255.0 green:164.0/255.0 blue:205.0/255.0 alpha:1.0]];
if (firstPlot) {
plot.barBasesVary = NO;
firstPlot = NO;
} else {
plot.barBasesVary = YES;
}
plot.barWidth = CPTDecimalFromFloat(0.5f);
plot.barsAreHorizontal = NO;
plot.dataSource = self;
plot.identifier = set;
[self.graph addPlot:plot toPlotSpace:plotSpace];
}
}
Can I use two CPTPiechart in view controller ?
I got a problem on dataSource and delegate.
Here is my code: // first piechart
-(IBAction)TG:(id)sender{
pieanother *InterPie = [[pieanother alloc]init];
pieChart = [[CPTPieChart alloc] init];
self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
self.hostView.allowPinchScaling = NO;
self.hostView.frame = CGRectMake(0, 220,320, 240);
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;
// 2 - Set up text style
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.color = [CPTColor grayColor];
textStyle.fontName = #"Helvetica-Bold";
textStyle.fontSize = 16.0f;
// 3 - Configure title
NSString *title = #"Portfolio Prices: May 1, 2012";
graph.title = title;
graph.titleTextStyle = textStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, -12.0f);
// 4 - Set theme
self.selectedTheme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[graph applyTheme:self.selectedTheme];
pieChart.dataSource = InterPie;//
pieChart.delegate = InterPie;
pieChart.pieRadius = (self.hostView1.bounds.size.height * 0.7) / 2;
pieChart.identifier = graph.title;
pieChart.startAngle = M_PI_4;
pieChart.sliceDirection = CPTPieDirectionClockwise;
// 3 - Create gradient
CPTGradient *overlayGradient = [[CPTGradient alloc] init];
overlayGradient.gradientType = CPTGradientTypeRadial;
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.9];
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.4] atPosition:1.0];
pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient];
// 4 - Add chart to graph
[graph addPlot:pieChart];
// 2 - Create legend
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
// 3 - Configure legen
theLegend.numberOfColumns = 1;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.cornerRadius = 5.0;
// 4 - Add legend to graph
graph.legend = theLegend;
graph.legendAnchor = CPTRectAnchorRight;
CGFloat legendPadding = -(self.view.bounds.size.width / 8);
graph.legendDisplacement = CGPointMake(legendPadding, 0.0);
[self.view addSubview: self.hostView];
}
// Second piechart
- (IBAction)OA:(id)sender {
self.anotherView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
self.anotherView.allowPinchScaling = NO;
self.anotherView.frame = CGRectMake(0, 220,320, 240);
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.anotherView.bounds];
self.anotherView.hostedGraph = graph;
graph.paddingLeft = 0.0f;
graph.paddingTop = 0.0f;
graph.paddingRight = 0.0f;
graph.paddingBottom = 0.0f;
graph.axisSet = nil;
// 2 - Set up text style
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.color = [CPTColor grayColor];
textStyle.fontName = #"Helvetica-Bold";
textStyle.fontSize = 16.0f;
// 3 - Configure title
NSString *title = #"Portfolio Prices: May 1, 2012";
graph.title = title;
graph.titleTextStyle = textStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, -12.0f);
// 4 - Set theme
self.selectedTheme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[graph applyTheme:self.selectedTheme];
pieanother *DomPie = [[pieanother alloc]init];
pieChart = [[CPTPieChart alloc] init];
pieChart.dataSource = DomPie;//
pieChart.delegate = DomPie;
pieChart.pieRadius = (self.hostView1.bounds.size.height * 0.7) / 2;
pieChart.identifier = graph.title;
pieChart.startAngle = M_PI_4;
pieChart.sliceDirection = CPTPieDirectionClockwise;
// 3 - Create gradient
CPTGradient *overlayGradient = [[CPTGradient alloc] init];
overlayGradient.gradientType = CPTGradientTypeRadial;
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.9];
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.4] atPosition:1.0];
pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient];
// 4 - Add chart to graph
[graph addPlot:pieChart];
// 2 - Create legend
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
// 3 - Configure legen
theLegend.numberOfColumns = 1;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.cornerRadius = 5.0;
// 4 - Add legend to graph
graph.legend = theLegend;
graph.legendAnchor = CPTRectAnchorRight;
CGFloat legendPadding = -(self.view.bounds.size.width / 8);
graph.legendDisplacement = CGPointMake(legendPadding, 0.0);
[self.view addSubview: self.anotherView];
}
Sometime I can run App without error but other times app just crash. Please advice.
pieChart.dataSource = DomPie; //if i comment pieChart.dataSource I alway run App without error but no data
pieChart.delegate = DomPie;
Use identifier property of CPTPlot object.
CPTPieChart piePlot1 = [[CPTPieChart alloc] init];
piePlot1.identifier = #"Pie Chart 1";
CPTPieChart piePlot2 = [[CPTPieChart alloc] init];
piePlot2.identifier = #"Pie Chart 2";
In datasource method:
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{
if ( [plot.identifier isEqual:#"Pie Chart 1"] ) {
// return for 1st Pie Chart
}
else if ( [plot.identifier isEqual:#"Pie Chart 2"] ) {
// return for 2nd Pie Chart
}
}
I created a core plot graph, have one x-axis, two y-axis, now, I want to have two lines and first use the first y-axis, second use the second y-axis, but it doesn't work. Who can guide me?
- (void)viewDidLoad
{
[super viewDidLoad];
graph = [[CPTXYGraph alloc] initWithFrame:CGRectMake(10, 100, 300, 300)];
// graph.backgroundColor = [CPTColor clearColor].cgColor;
CPTTheme * theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[graph applyTheme:theme];
CPTGraphHostingView * hostingView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(10, 100, 300, 300)];
hostingView.hostedGraph = graph;
[self.view addSubview:hostingView];
[hostingView release];
graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
// Plot area
// graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
graph.plotAreaFrame.paddingTop = 10;
graph.plotAreaFrame.paddingBottom = 50;
graph.plotAreaFrame.paddingLeft = 20.0;
graph.plotAreaFrame.paddingRight = 20.0;
graph.plotAreaFrame.cornerRadius = 10.0;
graph.plotAreaFrame.masksToBorder = NO;
// graph.plotAreaFrame.axisSet.borderLineStyle = [CPTLineStyle lineStyle];
graph.plotAreaFrame.plotArea.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1) length:CPTDecimalFromDouble(7)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(53.0) length:CPTDecimalFromDouble(4)];
// Line styles
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 3.0;
axisLineStyle.lineCap = kCGLineCapRound;
// Label x axis with a fixed interval policy
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.separateLayers = YES;//
x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(53);
x.majorIntervalLength = CPTDecimalFromDouble(1);
x.minorTicksPerInterval = 1;
x.tickDirection = CPTSignNegative;
x.axisLineStyle = axisLineStyle;
x.majorTickLength = 2.0;
x.majorTickLineStyle = axisLineStyle;
// Label y with an automatic labeling policy.
axisLineStyle.lineColor = [CPTColor greenColor];
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.separateLayers = YES;//
y.minorTicksPerInterval = 0;
y.tickDirection = CPTSignNegative;
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(1.0);
y.axisLineStyle = axisLineStyle;
y.majorTickLength = 6.0;
y.majorTickLineStyle = axisLineStyle;
// Label y2 with an equal division labeling policy.
axisLineStyle.lineColor = [CPTColor orangeColor];
CPTXYPlotSpace * plotSpace2 = [[CPTXYPlotSpace alloc] init];
plotSpace2.xRange = plotSpace.xRange;
plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1) length:CPTDecimalFromFloat(5)];
[graph addPlotSpace:plotSpace2];
CPTXYAxis *y2 = [[[CPTXYAxis alloc] init] autorelease];
y2.coordinate = CPTCoordinateY;
y2.plotSpace = plotSpace2;
y2.orthogonalCoordinateDecimal = CPTDecimalFromDouble(8.0);
y2.labelingPolicy = CPTAxisLabelingPolicyAutomatic;//
y2.separateLayers = NO;
y2.preferredNumberOfMajorTicks = 0;
y2.minorTicksPerInterval = 0;
y2.tickDirection = CPTSignPositive;
y2.axisLineStyle = axisLineStyle;
y2.majorTickLength = 6.0;
y2.majorTickLineStyle = axisLineStyle;
y2.minorTickLength = 4.0;
y2.titleOffset = 30.0;
//
CPTMutableLineStyle * lineStyle1 = [[CPTMutableLineStyle lineStyle] retain];
lineStyle1.lineWidth = 3.0f;
lineStyle1.lineColor = [CPTColor blueColor];
dataSourceLinePlot1 = [[CPTScatterPlot alloc] init];
dataSourceLinePlot1.identifier = #"1plot";
CPTPlotSymbol *plotSymbol1 = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol1.size = CGSizeMake(10, 10);
plotSymbol1.fill = [CPTFill fillWithColor:[CPTColor redColor]];
plotSymbol1.lineStyle = lineStyle1;
CPTMutableShadow *shadow1 = [CPTMutableShadow shadow];
shadow1.shadowColor = [CPTColor blueColor];
shadow1.shadowBlurRadius = 10.0;
dataSourceLinePlot1.shadow = shadow1;
dataSourceLinePlot1.plotSymbol = plotSymbol1;
dataSourceLinePlot1.dataLineStyle = lineStyle1;
dataSourceLinePlot1.dataSource = self;
[graph addPlot:dataSourceLinePlot1];
//-----------------------------------
CPTMutableLineStyle * lineStyle2 = [[CPTMutableLineStyle lineStyle] retain];
lineStyle2.lineWidth = 3.0f;
lineStyle2.lineColor = [CPTColor greenColor];
dataSourceLinePlot2 = [[CPTScatterPlot alloc] init];
dataSourceLinePlot2.identifier = #"2plot";
CPTPlotSymbol * plotSymbol2 = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol2.size = CGSizeMake(10, 10);
plotSymbol2.fill = [CPTFill fillWithColor:[CPTColor redColor]];
plotSymbol2.lineStyle = lineStyle2;
CPTMutableShadow *shadow2 = [CPTMutableShadow shadow];
shadow2.shadowColor = [CPTColor greenColor];
shadow2.shadowBlurRadius = 10.0;
dataSourceLinePlot2.shadow = shadow2;
dataSourceLinePlot2.plotSymbol = plotSymbol2;
dataSourceLinePlot2.dataLineStyle = lineStyle2;
dataSourceLinePlot2.dataSource = self;
[graph addPlot:dataSourceLinePlot2];
// Add the y2 axis to the axis set
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
dataArray1 = [[NSMutableArray alloc] init];
dataArray2 = [[NSMutableArray alloc] init];
for(int i=0; i< 7; i++){
float a = (float)(random()%40 + 530) / 10;
[dataArray1 addObject:[NSNumber numberWithFloat:a]];
}
for (int i = 0; i < 7; i++) {
// float a = (float)(random()%40 + 530) / 10;
[dataArray2 addObject:[NSNumber numberWithFloat:4]];
}
NSLog(#"array1 = %#,array2 = %#",dataArray1,dataArray2);
}
there is my delegate:
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
NSLog(#"%d",[dataArray1 count]);
if ([[plot identifier] isEqual:#"1plot"]) {
return [dataArray1 count];
}
return [dataArray2 count];
}
- (NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
if(fieldEnum == CPTScatterPlotFieldY){
if ([[plot identifier] isEqual:#"1plot"]) {
return [dataArray1 objectAtIndex:index];
}else{
return [dataArray2 objectAtIndex:index];
}
}else{
return [NSNumber numberWithFloat:index];
}
}
You are setting the datasource delegate to self before the arrays have any data put into them. You need to either initialize the array earlier or call [graph reloadData];
I have a chart with 2 bar plots (Example: A and B).
For each hour i have 2 bar plots.
When i set the legend, i get swatch and text for every hour and bar
plot.
Example: In my chart have 4 hours then my legend will be like this
A A A A
B B B B
And i only want
A B
Because on every hour, the bars means the same.
How can i do this ??
I have tried everything to make this happen but no success till now....
Bellow is my
// CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
CPTLegend *theLegend = [CPTLegend legendWithPlots:[NSArray arrayWithObjects:[barChart plotAtIndex:0],[barChart plotAtIndex:1], nil]];
theLegend.numberOfRows = 1;
theLegend.numberOfColumns = 2;//[horas count] +1 / 2;
//theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]];
//theLegend.borderLineStyle = barLineStyle;
theLegend.cornerRadius = 10.0;
theLegend.swatchSize = CGSizeMake(15, 15);
//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;
//theLegend.equalColumns = YES;
//theLegend.equalRows = YES;
theLegend.delegate = self;
barChart.legend = theLegend;
-(NSString *)legendTitleForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index{
if ( [barPlot.identifier isEqual:#"Embarque"] ) {
if (index == 0)
{
return #"Embarque";
}else {
return #"";
}
}else {
if (index == 0)
{
return #"Desembarque";
}else {
return #"";
}
}
}
-(BOOL)legend:(CPTLegend *)legend shouldDrawSwatchAtIndex:(NSUInteger)index forPlot:(CPTPlot *)plot inRect:(CGRect)rect inContext:(CGContextRef)context{
if (index == 0) {
return YES;
}else{
return NO;
}
}
Don't implement the -legendTitleForBarPlot:recordIndex: method unless you want a separate label for each bar. Use the title property to set a single legend title for the plot.
The -legend:shouldDrawSwatchAtIndex:forPlot:inRect:inContext: method is only needed if you want to change the default swatch drawing in some way.
-(void) exibirGraficoEmbarqueDescarga {
// Create barChart from theme
barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[barChart applyTheme:theme];
//CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
//hostingView.hostedGraph = barChart;
barChart.delegate = self;
if (chartView == nil)
{
chartView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(-15, 30, 350, 320)];
// chartView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 35, 510, 280)];
[self.view addSubview:chartView];
}
chartView.hostedGraph = barChart;
// Border
barChart.plotAreaFrame.borderLineStyle = nil;
barChart.plotAreaFrame.cornerRadius = 0.0f;
// Paddings
barChart.paddingLeft = 0.0f;
barChart.paddingRight = 0.0f;
barChart.paddingTop = 0.0f;
barChart.paddingBottom = 0.0f;
barChart.plotAreaFrame.paddingLeft = 70.0;
barChart.plotAreaFrame.paddingTop = 20.0;
barChart.plotAreaFrame.paddingRight = 20.0;
barChart.plotAreaFrame.paddingBottom = 80.0;
// Graph title
barChart.title = #"Embarque / Descarga";
CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle];
textStyle.color = [CPTColor grayColor];
textStyle.fontSize = 16.0f;
textStyle.textAlignment = CPTTextAlignmentCenter;
barChart.titleTextStyle = textStyle;
barChart.titleDisplacement = CGPointMake(1.0f, -20.0f);
barChart.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
// Add plot space for horizontal bar charts
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)barChart.defaultPlotSpace;
Apoio *apoio = [[Apoio alloc] init];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat([apoio retornaMaiorDadosGrafico:qtdEmb :qtdDesc])];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(16.0f)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChart.axisSet;
CPTXYAxis *x = axisSet.xAxis;
CPTMutableLineStyle *marcacaoLineStyle = [CPTLineStyle lineStyle];
marcacaoLineStyle.lineColor = [CPTColor lightGrayColor];
marcacaoLineStyle.lineWidth = 1;
x.axisLineStyle = marcacaoLineStyle;
x.majorTickLineStyle = marcacaoLineStyle;
x.minorTickLineStyle = marcacaoLineStyle;
x.majorIntervalLength = CPTDecimalFromString(#"5");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
//x.title = #"Horas";
//x.titleLocation = CPTDecimalFromFloat(15.0f);
//x.titleOffset = 55.0f;
// Define some custom labels for the data elements
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSArray *customTickLocations = posicao;//[NSArray arrayWithObjects:[NSDecimalNumber numberWithFloat:0.7], [NSDecimalNumber numberWithFloat:2.7], [NSDecimalNumber numberWithFloat:4.7], nil];
NSArray *xAxisLabels = horas;//[NSArray arrayWithObjects:#"10", #"11", #"12", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for ( NSNumber *tickLocation in customTickLocations ) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset; //+ x.majorTickLength;
[customLabels addObject:newLabel];
}
x.axisLabels = [NSSet setWithArray:customLabels];
CPTXYAxis *y = axisSet.yAxis;
y.axisLineStyle = marcacaoLineStyle;
y.majorTickLineStyle = marcacaoLineStyle;
y.minorTickLineStyle = marcacaoLineStyle;
y.majorIntervalLength = CPTDecimalFromInt([apoio retornaMaiorDadosGrafico:qtdEmb :qtdDesc]/7); //CPTDecimalFromString(#"5");
y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
y.title = #"Containers";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];
y.labelFormatter = formatter;
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [CPTColor lightGrayColor ];
y.majorGridLineStyle = majorGridLineStyle ;
y.preferredNumberOfMajorTicks = 10;
CPTMutableTextStyle *EixostextStyle = [CPTTextStyle textStyle];
EixostextStyle.fontSize = 12.0f;
y.labelTextStyle = EixostextStyle;
x.labelTextStyle = EixostextStyle;
// Embaque
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO];
barPlot.baseValue = CPTDecimalFromString(#"0");
barPlot.dataSource = self;
barPlot.delegate = self;
barPlot.barOffset = CPTDecimalFromFloat(0.25f);
barPlot.identifier = #"Embarque";
barPlot.title = #"Embarque";
barPlot.labelOffset = 0;
[barChart addPlot:barPlot toPlotSpace:plotSpace];
//Descarga
CPTBarPlot *barPlot2 = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
barPlot2.baseValue = CPTDecimalFromString(#"0");
barPlot2.dataSource = self;
barPlot2.delegate = self;
//barPlot2.barOffset = CPTDecimalFromFloat(25f);
barPlot2.identifier = #"Descarga";
barPlot2.title = #"Descarga";
barPlot2.labelOffset = 0;
[barChart addPlot:barPlot2 toPlotSpace:plotSpace];
CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
theLegend.numberOfRows = 1;
theLegend.numberOfColumns = 2;
theLegend.cornerRadius = 10.0;
theLegend.swatchSize = CGSizeMake(15, 15);
//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;
//theLegend.equalColumns = YES;
//theLegend.equalRows = YES;
barChart.legend = theLegend;
}
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{
return [horas count];}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSDecimalNumber *num = nil;
if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
switch ( fieldEnum ) {
case CPTBarPlotFieldBarLocation:
if ( [plot.identifier isEqual:#"Embarque"] ) {
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index*2];
} else{
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:(index*2+1)];
}
break;
case CPTBarPlotFieldBarTip:
if ( [plot.identifier isEqual:#"Embarque"] ) {
num = [qtdEmb objectAtIndex:index];
} else{
num = [qtdDesc objectAtIndex:index];
}
break;
}
}
return num;
}
-(CPTFill *)barFillForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index
{
if ( [barPlot.identifier isEqual:#"Embarque"] ) {
return [CPTFill fillWithColor:[CPTColor colorWithComponentRed:(175/255.0 ) green:(238/255.0) blue:(238/255.0) alpha:1.0]];
}
if ( [barPlot.identifier isEqual:#"Descarga"] ) {
return [CPTFill fillWithColor:[CPTColor colorWithComponentRed:(255./255.0 ) green:(228.0/255.0) blue:(181.0/255.0) alpha:1.0]];
}
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
int valor;
if ( [plot.identifier isEqual:#"Embarque"] )
{
valor = [qtdEmb objectAtIndex:index];
}else{
valor = [qtdDesc objectAtIndex:index];
}
CPTTextLayer *newLayer = [[CPTTextLayer alloc] initWithText:[[NSString alloc] initWithFormat:#"%#",valor]];
CPTMutableTextStyle *estiloTexto = [[CPTMutableTextStyle alloc] init];
estiloTexto.color = [CPTColor blackColor];
estiloTexto.fontSize = 10;
newLayer.textStyle = estiloTexto;
return newLayer;
}