Custom axis labels won't display on left y and right y axis - core-plot

i have a custom label and tick location script that works fine for my left y axis. As soon as i try to include a right y axis, the labels will only display on either left or right. The tick locations for Major and Minor display on both sides. if i comment out the code for y2.labels, the labels are on the right, include the line of code and only on the left? Think there is something simple i am missing, just can't find it.
edit:
Interesting when i comment out "y3.labelingPolicy = CPTAxisLabelingPolicyNone;" the left hand y axis looses its custom letter labels and moves to decimal, the right hand y axis just gets numbers.
CPTXYAxis *y2 = axisSet2.yAxis;
y2.axisLineStyle = graphLineStyle;
y2.labelingPolicy = CPTAxisLabelingPolicyNone;
NSArray *y2MajorLocation = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:0], [NSDecimalNumber numberWithInt:2], [NSDecimalNumber numberWithInt:4],
[NSDecimalNumber numberWithInt:6], [NSDecimalNumber numberWithInt:8], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:12],
[NSDecimalNumber numberWithInt:14], [NSDecimalNumber numberWithInt:16], nil];
NSArray *y2MinorLocation = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:3], [NSDecimalNumber numberWithInt:5],
[NSDecimalNumber numberWithInt:7], [NSDecimalNumber numberWithInt:9], [NSDecimalNumber numberWithInt:11], [NSDecimalNumber numberWithInt:13],
[NSDecimalNumber numberWithInt:15], nil];
NSArray *y2Label = [NSArray arrayWithObjects:#"", #"S", #"SW", #"W", #"NW", #"N", #"NE", #"E", #"SE", nil];
CPTMutableTextStyle *y2LabelStyle = [CPTMutableTextStyle textStyle];
y2LabelStyle.color = [[CPTColor whiteColor] colorWithAlphaComponent:1];
y2LabelStyle.fontName = #"Helvetica-Bold";
y2LabelStyle.fontSize = 9.0f;
NSUInteger y2labelLocation = 0;
NSMutableArray *y2customLabels = [NSMutableArray arrayWithCapacity:[y2Label count]];
NSMutableArray *y3customLabels = [NSMutableArray arrayWithCapacity:[y2Label count]];
for (NSNumber *tickLocation in y2MajorLocation) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [y2Label objectAtIndex:y2labelLocation++] textStyle:y2LabelStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = -4;
[y2customLabels addObject:newLabel];
newLabel.offset = 4;
[y3customLabels addObject:newLabel];
}
y2.axisLabels = [NSSet setWithArray:y2customLabels];
y2.majorTickLocations = [NSSet setWithArray:y2MajorLocation];
y2.minorTickLocations = [NSSet setWithArray:y2MinorLocation];
y2.majorTickLength = 4;
y2.minorTickLength = 2;
y2.majorTickLineStyle = graphLineStyle;
y2.minorTickLineStyle = graphLineStyle;
y2.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
CPTXYAxis *y3 = [(CPTXYAxis *)[CPTXYAxis alloc] initWithFrame:CGRectZero];
y3.labelingPolicy = CPTAxisLabelingPolicyNone;
y3.axisLineStyle = graphLineStyle;
y3.coordinate = CPTCoordinateY;
y3.plotSpace = plotSpace2;
y3.axisConstraints = [CPTConstraints constraintWithUpperOffset:0];
y3.labelOffset = 3;
y3.labelTextStyle = y2LabelStyle;
y3.majorTickLength = 4;
y3.minorTickLength = 2;
y3.majorTickLineStyle = graphLineStyle;
y3.minorTickLineStyle = graphLineStyle;
y3.tickDirection = CPTSignPositive;
y3.majorTickLocations = [NSSet setWithArray:y2MajorLocation];
y3.minorTickLocations = [NSSet setWithArray:y2MinorLocation];
y3.axisLabels = [NSSet setWithArray:y3customLabels];
graph2.axisSet.axes = #[x2, y2, y3];

Related

Core plot issue y axis plot point are not visible

Hi i am using core plot for scatter graph plotting. I am plotting data (upto 3 decimal) against the datetime. Data on yaxis and date time on xaxis. For some data value yaxis Custom tick label are not visible.
Below are the code
CPTXYGraph *scatterGraph = [[CPTXYGraph alloc] initWithFrame:graphView.bounds];
graphView.hostedGraph = scatterGraph;
NSMutableArray *plotdataArray = [[NSMutableArray alloc] init];
NSMutableArray *timedataArray = [[NSMutableArray alloc] init];
plotdataArray = arrPlotData;
timedataArray = arrTimeData;
//create data plot
self.dataSourceLinePlot = [[CPTScatterPlot alloc]init];
self.dataSourceLinePlot.identifier = NSLocalizedString(#"Glucose Trend Chart", nil);
self.dataSourceLinePlot.cachePrecision = CPTPlotCachePrecisionDecimal;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) scatterGraph.defaultPlotSpace;
//assign data to chartdata class
CGM_GlucoseChartData *chartDataObj = [[CGM_GlucoseChartData alloc] initWithView:self.dataSourceLinePlot];
chartDataObj.parent = self;
self.dataSourceLinePlot.dataSource = chartDataObj;
chartDataObj.dataForPlot = arrPlotData;
chartDataObj.arrTimeData = timedataArray;
[scatterGraph addPlot:self.dataSourceLinePlot];
scatterGraph.plotAreaFrame.frame = scatterGraph.bounds;
//add padding to graphview
scatterGraph.plotAreaFrame.paddingTop = 0;
scatterGraph.plotAreaFrame.paddingRight = 0;
scatterGraph.plotAreaFrame.paddingBottom = 90;
scatterGraph.plotAreaFrame.paddingLeft = 66;
scatterGraph.plotAreaFrame.masksToBorder = YES;
//add linestyle to graph
CPTMutableLineStyle *lineStyle = [self.dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 1.0;
lineStyle.lineColor = [CPTColor blueColor];
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
plotSymbol.size = CGSizeMake(1.0f, 1.0f);
self.dataSourceLinePlot.dataLineStyle = lineStyle;
//calculate min and max values from array
float yMax = [[arrPlotData valueForKeyPath:#"#max.self"] floatValue];
float yMin = [[arrPlotData valueForKeyPath:#"#min.self"] floatValue];
NSUInteger xMax = timedataArray.count;
//create axis set for graph
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)scatterGraph.axisSet;
// Configure Line-Style //
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineWidth = 1.0f;
tickLineStyle.lineColor = [CPTColor blackColor];
axisSet.yAxis.majorTickLineStyle = tickLineStyle;
axisSet.xAxis.majorTickLineStyle = tickLineStyle;
// y-axis labels
CPTXYAxis *y = axisSet.yAxis;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
y.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0.0);
y.labelingPolicy = CPTAxisLabelingPolicyNone;
//set axis title
NSString *temp = NSLocalizedString(#"Glucose", nil);
y.title = [NSString stringWithFormat:#"%# (%#)",temp,unitType];
y.titleOffset = 52;
// x-axis labels
CPTXYAxis *x = axisSet.xAxis;
//set axis title
x.title = NSLocalizedString(#"Time (Hour)", nil);
x.titleOffset = 75;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0.0);
x.labelingPolicy = CPTAxisLabelingPolicyNone;
if(plotdataArray.count == 0) {
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInteger(1)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInteger(1)];
}
else{
float centerLocation = (yMax+yMin) / 2;
float ytemp1 = (yMin +centerLocation) / 2;
float ytemp2 = (yMax+centerLocation) / 2;
//if differnce between min and max values is 0.1
if((yMax - yMin) == 0.001) {
customTickLocations = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%.3f",yMin],
[NSString stringWithFormat:#"%.3f",yMax],nil];
yAxisLabels = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%.3f",yMin],
[NSString stringWithFormat:#"%.3f",yMax],nil];
tempVal = yMax/4;
}
else{
if(ytemp1 == centerLocation || ytemp2 == centerLocation) {
customTickLocations = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%.3f",yMin],
[NSString stringWithFormat:#"%.3f",centerLocation],
[NSString stringWithFormat:#"%.3f",yMax],nil];
yAxisLabels = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%.3f",yMin],
[NSString stringWithFormat:#"%.3f",centerLocation],
[NSString stringWithFormat:#"%.3f",yMax],nil];
tempVal = yMax/4;;
}
else{
customTickLocations = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%.3f",yMin],
[NSString stringWithFormat:#"%.3f",ytemp1],
[NSString stringWithFormat:#"%.3f",centerLocation],
[NSString stringWithFormat:#"%.3f",ytemp2],
[NSString stringWithFormat:#"%.3f",yMax],nil];
yAxisLabels = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%.3f",yMin],
[NSString stringWithFormat:#"%.3f",ytemp1],
[NSString stringWithFormat:#"%.3f",centerLocation],
[NSString stringWithFormat:#"%.3f",ytemp2],
[NSString stringWithFormat:#"%.3f",yMax],nil];
tempVal = (yMax-ytemp2)/2;
}
}
}
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[yAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [yAxisLabels objectAtIndex:labelLocation++] textStyle:y.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = y.labelOffset + y.majorTickLength;
[customLabels addObject:newLabel];
}
y.majorTickLocations = [NSSet setWithArray:customTickLocations];
y.axisLabels = [NSSet setWithArray:customLabels];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd-MM (HH:mm)";
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT+0:00"]];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:timedataArray.count];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:timedataArray.count];
NSUInteger maximumValue = 0;
if(timedataArray.count > 5 && timedataArray.count < 15) {
maximumValue = xMax - 1;
}
else {
maximumValue = xMax - (timedataArray.count/15);
}
if(timedataArray.count <= 5) {
for (NSInteger j = 0; j < timedataArray.count; j ++) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[dateFormatter stringFromDate:timedataArray[j]] textStyle:x.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j);
label.tickLocation = location;
if (label)
{
[xLabels addObject:label];
label.offset = 5.0f;
[xLocations addObject:[dateFormatter stringFromDate:timedataArray[j]]];
}
}
x.majorTickLocations = xLocations;
x.axisLabels = xLabels;
}
else {
//if(timedataArray.count < 15) {
NSInteger centerLocation = timedataArray.count / 2;
NSInteger temp1 = centerLocation / 2;
NSInteger temp2 = (timedataArray.count-centerLocation) / 2;
NSArray *customTickLocations = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%.1f",0.0],
[NSString stringWithFormat:#"%.1ld",(long)temp1],
[NSString stringWithFormat:#"%.1ld",(long)centerLocation],
[NSString stringWithFormat:#"%.1ld",(long)centerLocation + temp2],
[NSString stringWithFormat:#"%.1lu",(unsigned long)maximumValue],nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:timedataArray[0]]],
[NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:timedataArray[temp1]]],
[NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:timedataArray[centerLocation]]],
[NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:timedataArray[centerLocation + temp2]]],
[NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:timedataArray[maximumValue]]],nil];
x.majorTickLocations = [NSSet setWithArray:customTickLocations];
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];
}
//rotate the x axis label
x.labelRotation = M_PI/3;
//create plot space and assign range for graph
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInteger(xMax)];
if(timedataArray.count == 1 || yMax == yMin) {
if(yMax == 0) {
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0)
length:CPTDecimalFromFloat(1.0)];
}
else{
if(tempVal == 0) {
tempVal = yMax/4;
}
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0)
length:CPTDecimalFromString([NSString stringWithFormat:#"%.2f",yMax+tempVal])];
}
}
else{
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromString([NSString stringWithFormat:#"%.2f",yMin])
length:CPTDecimalFromString([NSString stringWithFormat:#"%.2f",(yMax-yMin)+tempVal])];
}
}
[self.dataSourceLinePlot reloadData];
please suggest some thing what is issue with my code

Change the Y-axis labels and lines dynamically in core plot

I want to change the Y-axis dynamically. I have max and min text fields. When I change the max or min fields, the graph will change the Y-axis.
I have tried like following but only two line are coming. Please check the following code.
This is the method for Configure Y-axis.
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.majorTickLength = 0.0;
NSMutableSet *yLabels = [NSMutableSet setWithCapacity:4];
NSMutableSet *yLocations = [NSMutableSet setWithCapacity:4];
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
CGFloat max_value = [[preferences valueForKey:MAX_KEY] integerValue];
CGFloat min_value = [[preferences valueForKey:MIN_KEY] integerValue];
NSLog(#"max_value%f",max_value);
NSInteger range = ((max_value-min_value)*10)/100;
NSLog(#"range%d",range);
newmaxValue = (max_value-min_value) - range;
newMinValue = min_value - range;
yAxisLabels = [NSArray arrayWithObjects:[NSString stringWithFormat:#"%.f",newMinValue], [NSString stringWithFormat:#"%.f",newMinValue+20],[NSString stringWithFormat:#"%.f",newmaxValue-20],[NSString stringWithFormat:#"%.f",newmaxValue], nil];
yAxiscustomTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:newMinValue], [NSDecimalNumber numberWithInt:newMinValue+20], [NSDecimalNumber numberWithInt:newmaxValue-20], [NSDecimalNumber numberWithInt:newmaxValue],nil];
NSNumberFormatter * nFormatter = [[NSNumberFormatter alloc] init];
[nFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
for ( NSUInteger i = 0; i < [yAxisLabels count]; i++ ) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:#"%#",[yAxisLabels objectAtIndex:i]] textStyle:axisTextStyle];
label.tickLocation = CPTDecimalFromInteger([[yAxiscustomTickLocations objectAtIndex:i] integerValue]);
//label.offset = y.labelOffset + y.majorTickLength;
NSLog(#"%d",[[yAxiscustomTickLocations objectAtIndex:i] integerValue]);
label.offset = y.majorTickLength+2;
if (label) {
[yLabels addObject:label];
[yLocations addObject:[NSString stringWithFormat:#"%d",[[yAxiscustomTickLocations objectAtIndex:i] integerValue]]];
}
label = nil;
}
NSLog(#"newmaxValue%f",newmaxValue);
NSLog(#"newMinValue%f",newMinValue);
y.axisLabels = yLabels;
y.majorTickLocations = [NSSet setWithArray:yAxiscustomTickLocations];
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat((NSInteger)newMinValue) length:CPTDecimalFromFloat((NSInteger)newmaxValue)];
y.labelFormatter = nFormatter;
And also I am setting the range like this
[self getCoreplotSpace].xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0.0f) length:CPTDecimalFromUnsignedInteger(1800.0f)];
[self getCoreplotSpace].yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger((NSInteger)newMinValue) length:CPTDecimalFromInteger((NSInteger)newmaxValue)];
EDIT::
Log OUTPUT
newmaxValue225.000000
newMinValue-75.000000
yAxisLabels(
"-75",
"-55",
205,
225
)
yAxiscustomTickLocations(
"-75",
"-55",
205,
225
)
Could you please help me guys...
Thanks
Your problem is at the end where you specify the y axis visible range:
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat((NSInteger)newMinValue) length:CPTDecimalFromFloat((NSInteger)newmaxValue)];
The length should be the range, not the newmaxValue:
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat((NSInteger)newMinValue) length:CPTDecimalFromFloat((NSInteger)(newmaxValue-newminValue))];

plot negative and positive values in same Y-axis in coreplot ios

This is the I am creating scatterplot:
-(void)createScatterPlotsWithIdentifier:(NSString *)identifier color:(CPTColor *)color forGraph:(CPTGraph *)graph forXYPlotSpace:(CPTXYPlotSpace *)plotSpace{
CPTScatterPlot *scatterPlot = [[CPTScatterPlot alloc] init];
scatterPlot.dataSource = self;
scatterPlot.identifier = identifier;
//Plot a graph with in the plotspace
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:scatterPlot, nil]];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(30)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(-50) length:CPTDecimalFromUnsignedInteger(250)];
CPTMutablePlotRange *xRange = [[self getCoreplotSpace].xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(-1)];
[self getCoreplotSpace].xRange = xRange;
CPTMutableLineStyle *scatterLineStyle = [scatterPlot.dataLineStyle mutableCopy];
scatterLineStyle.lineWidth = 1;
scatterLineStyle.lineColor = color;
scatterPlot.dataLineStyle = scatterLineStyle;
CPTMutableLineStyle *scatterSymbolLineStyle = [CPTMutableLineStyle lineStyle];
scatterSymbolLineStyle.lineColor = color;
CPTPlotSymbol *scatterSymbol = [CPTPlotSymbol ellipsePlotSymbol];
scatterSymbol.fill = [CPTFill fillWithColor:color];
scatterSymbol.lineStyle = scatterSymbolLineStyle;
scatterSymbol.size = CGSizeMake(2.0f, 2.0f);
scatterPlot.plotSymbol = scatterSymbol;
[graph addPlot:scatterPlot toPlotSpace:plotSpace];
}
Configuring the Y-axis like this:
NSMutableSet *yLabels = [NSMutableSet setWithCapacity:4];
NSMutableSet *yLocations = [NSMutableSet setWithCapacity:4];
NSArray *yAxisLabels = [NSArray arrayWithObjects:#"-50", #"33", #"117", #"200", nil];
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithUnsignedInt:-50], [NSDecimalNumber numberWithInt:33], [NSDecimalNumber numberWithInt:117], [NSDecimalNumber numberWithInt:200],nil];
for ( NSUInteger i = 0; i < [yAxisLabels count]; i++ ) {
NSLog(#"%#",[yAxisLabels objectAtIndex:i]);
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:#"%#",[yAxisLabels objectAtIndex:i]] textStyle:axisTextStyle];
label.tickLocation = CPTDecimalFromInteger([[customTickLocations objectAtIndex:i] integerValue]);
label.offset = y.majorTickLength;
if (label) {
[yLabels addObject:label];
[yLocations addObject:[NSString stringWithFormat:#"%d",[[customTickLocations objectAtIndex:i] integerValue]]];
}
label = nil;
}
y.axisLabels = yLabels;
y.majorTickLocations = [NSSet setWithArray:customTickLocations];
Could you please find the screenshot.
I have following issues.
I am able to set the y axis but the graph is taking -50 as 0.
In screenshot, y axis is not aligned properly.
only axis are in bold color. how can I change that to normal.
could you please help me on these issues.
Thanks ...
Thanks
NSString has an integerValue method that will parse the string contents into an NSInteger. Use the doubleValue method for floating point values. For more complex number parsing tasks, use NSScanner.
For example,
label.tickLocation = CPTDecimalFromInteger(yAxisLabels[i].integerValue);
and
[yLocations addObject:#(yAxisLabels[i].integerValue)];

Core-plot bargraph with duplicated legend

I am working with the core-plot framework to create a bar graph with a legend. It all works except that the legend is duplicating the the columns in each section of the bar graph (Please see attached screen shot).
I have looked through the core-plot class reference and was not able to find anything to fix this problem. I have tried to set the CPTLegend properties numberOfColumns to 1 and the numberOfRows to 3 and that made the legend display the appropriate amount of items in the legend, but the data it was displaying was not correct.
Below is the code I'm using to build the bar graph. Do you all have any suggestions how I can fix this issue? I am assuming this is not so much a bug with core-plot but a limitation of the legend and am hoping there is a workaround.
// Create barChart from theme
barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[barChart applyTheme:theme];
chartView.hostedGraph = barChart;
barChart.plotAreaFrame.masksToBorder = NO;
barChart.paddingLeft = 60.0;
barChart.paddingTop = 10.0;
barChart.paddingRight = 0.0;
barChart.paddingBottom = 30.0;
//find max y
int maxY = 0;
NSMutableArray *maxDrillDownData = [chartData.drillDownData objectForKey:DRILLDOWN_EQUIPMENT_ALL_TYPE];
for (NSMutableArray *dataArray in maxDrillDownData) {
maxY = dataArray.count>maxY?dataArray.count:maxY;
}
//add buffer
maxY = maxY+100;
// Add plot space for horizontal bar charts
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)barChart.defaultPlotSpace;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromInt(maxY)];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(3.25)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChart.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.majorIntervalLength = CPTDecimalFromString(#"1");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
x.titleLocation = CPTDecimalFromFloat(7.5f);
x.titleOffset = 55.0f;
x.labelOffset = 2.0;
// Define some custom labels for the data elements
x.labelRotation = M_PI/4;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithFloat:0.5], [NSDecimalNumber numberWithFloat:1.5], [NSDecimalNumber numberWithFloat:2.5], nil];
NSArray *xAxisLabels = (NSMutableArray *)[chartData.labels objectForKey:LABELS_EQUIPMENT_TYPE];
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;
[customLabels addObject:newLabel];
//[newLabel release];
}
x.axisLabels = [NSSet setWithArray:customLabels];
CPTXYAxis *y = axisSet.yAxis;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
y.majorIntervalLength = CPTDecimalFromString(#"50");
y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
y.title = (NSString *)[chartData.labels objectForKey:LABELS_Y_AXIS];
y.titleOffset = 45.0f;
y.titleLocation = CPTDecimalFromFloat(150.0f);
//identifiers
NSMutableArray *identifiers = (NSMutableArray *)[chartData.identifiers objectForKey:IDENTIFIER_EQUIPMENT_TYPE];
// First bar plot
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO];
barPlot.baseValue = CPTDecimalFromString(#"0");
barPlot.dataSource = self;
barPlot.barOffset = CPTDecimalFromFloat(-0.6f);
barPlot.barWidth = CPTDecimalFromFloat(.5);
barPlot.delegate = self;
barPlot.identifier = (NSString *)[identifiers objectAtIndex:0];
[barChart addPlot:barPlot toPlotSpace:plotSpace];
NSLog(#"barPlot.identifier: %#", barPlot.identifier);
// Second bar plot
barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.barOffset = CPTDecimalFromFloat(-0.4f);
barPlot.barWidth = CPTDecimalFromFloat(.5);
barPlot.baseValue = CPTDecimalFromString(#"0");
barPlot.identifier = (NSString *)[identifiers objectAtIndex:1];
barPlot.delegate = self;
[barChart addPlot:barPlot toPlotSpace:plotSpace];
NSLog(#"barPlot.identifier: %#", barPlot.identifier);
//third bar plot
barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.barOffset = CPTDecimalFromFloat(-0.2f);
barPlot.barWidth = CPTDecimalFromFloat(.5);
barPlot.baseValue = CPTDecimalFromString(#"0");
barPlot.identifier = (NSString *)[identifiers objectAtIndex:2];
barPlot.delegate = self;
[barChart addPlot:barPlot toPlotSpace:plotSpace];
NSLog(#"barPlot.identifier: %#", barPlot.identifier);
// Add legend
CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
theLegend.numberOfColumns = 3;
theLegend.numberOfRows = 1;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.cornerRadius = 5.0;
barChart.legend = theLegend;
barChart.legendAnchor = CPTRectAnchorTopRight;
barChart.legendDisplacement = CGPointMake(-10.0f, -20.0f);
Thanks for the help!
Make sure neither of the following methods are implemented in your plot datasource:
-legendTitleForBarPlot:recordIndex:
-barFillForBarPlot:recordIndex:
If either of these methods exist, the plot creates a legend entry for each bar rather than using a single legend entry for the whole plot.
I know this is a very old question, but the provided answers didn't help me in any way. So i looked into it and tried to figure it out. In my opinion the best solution is to only add one plot to your legend instead of adding the graph. Adding the graph adds all plots associated to your graph to the legend.
CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
replace with
NSMutableArray *legendPlots =[[NSMutableArray alloc] init];
[legendPlots addObject:barPlot];
CPTLegend *theLegend = [CPTLegend legendWithPlots:legendPlots];
I had the same problem with multiple Pie charts in one Graph. This resolved the problem for me.

Core plot: axis alternatingBandFills with custom axis labels

I am using the Core Plot library for drawing graphs on the iOS. I want to add colors between the grid lines of the yAxis. I have managed to do it with setting the alternatingBandFills property of the axis. However, I have to use custom labels on the yAxis as well, and when I am providing the custom labels, the alternatingBandFills property does not work for some reason.
Any help how to add colors to the spaces between the grid lines on the yAxis as well as using custom labels will be much appreciated.
The code that I am using now looks like:
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.hostedGraph.axisSet;
CPTXYAxis *yAxis = axisSet.yAxis;
yAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(minValueX);
yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
NSArray *yAxisTickLocations = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithDouble:lowerRedRangeFrom],
[NSDecimalNumber numberWithDouble:lowerOrangeRangeFrom],
[NSDecimalNumber numberWithDouble:greenRangeFrom],
[NSDecimalNumber numberWithDouble:upperOrangeRangeFrom],
[NSDecimalNumber numberWithDouble:upperRedRangeFrom],
[NSDecimalNumber numberWithDouble:upperRedRangeTo],
nil];
NSArray *yAxisLabels = [NSArray arrayWithObjects:#"Label1",#"Label2", #"Label3",#"Label4",#"Label5",#"Label6", nil];
NSUInteger labelLocationY = 0;
NSMutableArray *customLabelsY = [NSMutableArray arrayWithCapacity:[yAxisLabels count]];
for (NSNumber *tickLocation in yAxisTickLocations) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [yAxisLabels objectAtIndex:labelLocationY++] textStyle:axisSet.xAxis.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
newLabel.rotation = M_PI/4;
[customLabelsY addObject:newLabel];
}
axisSet.yAxis.axisLabels = [NSSet setWithArray:customLabelsY];
yAxis.alternatingBandFills = [NSArray arrayWithObjects:
[CPTColor redColor],
[CPTColor orangeColor],
[CPTColor greenColor],
[CPTColor orangeColor],
[CPTColor redColor], nil];
I have figured it out:
The labeling policy of the axis should be: CPTAxisLabelingPolicyLocationsProvided, for which the documentation states: " User sets tick locations; axis makes labels.".
Now we only need to specify the locations of the ticks. This is done by creating a NSSetobject with the locations. Then we have to set the majorTickLocations property of the axis.
So my code now looks like:
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.hostedGraph.axisSet;
CPTXYAxis *yAxis = axisSet.yAxis;
yAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(minValueX);
yAxis.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;
NSSet *majorTickLocations = [NSSet setWithObjects:
[NSDecimalNumber numberWithDouble:lowerRedRangeFrom],
[NSDecimalNumber numberWithDouble:lowerOrangeRangeFrom],
[NSDecimalNumber numberWithDouble:greenRangeFrom],
[NSDecimalNumber numberWithDouble:upperOrangeRangeFrom],
[NSDecimalNumber numberWithDouble:upperRedRangeFrom],
[NSDecimalNumber numberWithDouble:upperRedRangeTo],
nil];
yAxis.majorTickLocations = majorTickLocations;
yAxis.alternatingBandFills = [NSArray arrayWithObjects:
[CPTColor redColor],
[CPTColor orangeColor],
[CPTColor greenColor],
[CPTColor orangeColor],
[CPTColor redColor], nil];

Resources