Questions about scatter plot in Core Plot - ios

I am making a scatter plot app in Core Plot 1.4. I am new to Objective-C and Core Plot, so sorry for asking some basic questions. Here is some code I wrote by imitating the sample program(CPTTestApp-iphone).
- (void)viewDidLoad
{
[super viewDidLoad];
...
// Add some initial data
NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
int i;
for ( i = 0; i < 60; i++ ) {
id x = [NSNumber numberWithFloat:1 + i * 0.05];
id y = [NSNumber numberWithInt:1]; //[NSNumber numberWithFloat:1.2 * rand() / (float)RAND_MAX + 1.2];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, #"x", y, #"y", nil]];
}
dataForPlot = contentArray;
// Create graph from theme
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] init];
hostingView.collapsesLayers = NO; // Setting to YES reduces GPU memory usage, but can slow drawing/scrolling
hostingView.hostedGraph = graph;
[self setView:hostingView];
graph.paddingLeft = 0.0;
graph.paddingTop = 0.0;
graph.paddingRight = 0.0;
graph.paddingBottom = 30.0;
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
[plotSpace setXRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(5)]];
[plotSpace setYRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(7)]];
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromString(#"1");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
x.minorTicksPerInterval = 5;
NSArray *exclusionRanges = [NSArray arrayWithObjects:
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(2.99) length:CPTDecimalFromFloat(0.02)],
nil];
x.labelExclusionRanges = exclusionRanges;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromString(#"1");
y.minorTicksPerInterval = 5;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
exclusionRanges = [NSArray arrayWithObjects:
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(3.99) length:CPTDecimalFromFloat(0.02)],
nil];
y.labelExclusionRanges = exclusionRanges;
y.delegate = self;
// Create a blue plot area
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.miterLimit = 1.0f;
lineStyle.lineWidth = 3.0f;
lineStyle.lineColor = [CPTColor blueColor];
boundLinePlot.dataLineStyle = lineStyle;
boundLinePlot.identifier = #"Blue Plot";
boundLinePlot.dataSource = self;
[graph addPlot:boundLinePlot toPlotSpace:plotSpace];
// Do a blue gradient
CPTColor *areaColor1 = [CPTColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8];
CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]];
areaGradient1.angle = -90.0f;
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient1];
boundLinePlot.areaFill = areaGradientFill;
boundLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];
// interpolation
boundLinePlot.interpolation = CPTScatterPlotInterpolationCurved;
#ifdef PERFORMANCE_TEST
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(changePlotRange) userInfo:nil repeats:YES];
#endif
}
Now I have some questions.
From dataForPlot, a blue line with gradient must be rendered the height y=1, but after running this it was not shown. (I haven't adjust the range of x values in dataForPlot but a partial line should be rendered.)
How can I move the origin (0,0) to the middle of the screen?
Why do 1.0-3.0 in x-axis and 1.0, 2.0, 4.0 in y-axis disappear?
Can anyone help me figure these out? Thank you.

What data is being plotted? Please show the datasource methods.
The visible data ranges in the plot area are controlled by the plot space:
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(-5)
length:CPTDecimalFromInteger(10)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(-7)
length:CPTDecimalFromInteger(14)];
The labelExclusionRanges skips over any labels in the specified ranges. If you want to see all of the ticks and labels, don't set this property.

Related

Cannot Remove or Hide Axes from Core Plot, RealTimePlot

As mentioned in title, I have succesfully add CorePlot into my project and it works as expected. Except there are still axes label shown which I cannot remove or hide. This is my renderInLayer code,
-(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme animated:(BOOL)animated
{
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
CGRect bounds = layerHostingView.bounds;
#else
CGRect bounds = NSRectToCGRect(layerHostingView.bounds);
#endif
CPTGraph *graph = [[[CPTXYGraph alloc] initWithFrame:bounds] autorelease];
[self addGraph:graph toHostingView:layerHostingView];
graph.plotAreaFrame.paddingTop = 0.0;
graph.plotAreaFrame.paddingRight = 0.0;
graph.plotAreaFrame.paddingBottom = 0.0;
graph.plotAreaFrame.paddingLeft = 0.0;
graph.plotAreaFrame.masksToBorder = NO;
// Create the plot
CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = kPlotIdentifier;
dataSourceLinePlot.cachePrecision = CPTPlotCachePrecisionDouble;
CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
lineStyle.lineWidth = 3.0;
lineStyle.lineColor = [CPTColor whiteColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
dataSourceLinePlot.interpolation = CPTScatterPlotInterpolationCurved;
[graph addPlot:dataSourceLinePlot];
// Plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(kMaxDataPoints - 2)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(1)];
[dataTimer invalidate];
[dataTimer release];
if ( animated ) {
dataTimer = [[NSTimer timerWithTimeInterval:1.0 / kFrameRate
target:self
selector:#selector(newData:)
userInfo:nil
repeats:YES] retain];
[[NSRunLoop mainRunLoop] addTimer:dataTimer forMode:NSRunLoopCommonModes];
}
else {
dataTimer = nil;
}
}
And visual out with bad data,
EDIT
I have tried this but it didnt work,
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *axis = axisSet.xAxis;
axis.hidden = YES;
for (CPTAxisLabel *axisLabel in axis.axisLabels) {
axisLabel.contentLayer.hidden = YES;
}
Ok so I have managed to find the answer my self. If anyone in future wants to remove just the label. Then please do this,
axis.labelingPolicy = CPTAxisLabelingPolicyNone;
If you want to remove the axis of graph completly, then.
graph.axisSet = nil;
Boom all gone, you have clear and nice looking graph now :)
You can simply hide all labels.
CPTAxis *axis = xAxis;
axis.hidden = YES;
for (CPTAxisLabel *axisLabel in axis.axisLabels) {
axisLabel.contentLayer.hidden = YES;
}
I am posting how I customized my xAxis labels:
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)_graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromString(#"5");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
x.minorTicksPerInterval = 4;
x.title = #"";
x.titleTextStyle = axisTitleStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone; //No labels provided; user sets labels and tick locations.
//x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 5.0f;
x.tickDirection = CPTSignNegative;
CPTMutableTextStyle *axisLabelStyle = [CPTMutableTextStyle textStyle];
axisLabelStyle.fontName = #"Helvetica Neue Thin";
axisLabelStyle.fontSize = 8.0f;
NSArray *lastThirtyDates = [self.analyticsDataController lastThirtyDays];
NSUInteger daysCount = 31;
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:daysCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:daysCount];
NSMutableSet *xMinorLocations = [NSMutableSet setWithCapacity:daysCount];
for (//iterate over your data source for x values and create your custom labels) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:#"" textStyle:axisLabelStyle];
CGFloat location = i;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = x.majorTickLength;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
x.minorTickLocations = xMinorLocations;

Multiple y axes with custom label in core plot

-(void)configureAxes {
CPTGraph* graph = self.hostView.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTScatterPlot* plot = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
plot.dataSource = self;
[graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
[plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( 8 )]];
[plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( 10 )]];
self.arrayData=[[NSMutableArray alloc] initWithObjects:#"A",#"B",#"C",#"D",#"10",#"20",#"E", nil];
self.arrayDataY=[[NSMutableArray alloc] initWithObjects:#"CP",#"BD",#"Test",#"Notes",#"CM",#"Day",#"97.7",#"97.8",#"97.9", nil];
CPTMutableLineStyle *yMajorGridLineStyle = [CPTLineStyle lineStyle];
yMajorGridLineStyle.lineCap = kCGLineCapRound;
yMajorGridLineStyle.lineColor = [CPTColor whiteColor];
yMajorGridLineStyle.lineWidth = 1.0;
CPTMutableLineStyle *lineStyle=[CPTLineStyle lineStyle];
lineStyle.lineColor=[CPTColor redColor];
lineStyle.lineWidth=2.0f;
CPTMutableLineStyle *lineStyle1=[CPTLineStyle lineStyle];
lineStyle1.lineColor=[CPTColor greenColor];
lineStyle1.lineWidth=2.0f;
CPTMutableLineStyle *lineStyle2=[CPTLineStyle lineStyle];
lineStyle2.lineColor=[CPTColor blackColor];
lineStyle2.lineWidth=1.0f;
CPTXYAxis *x = axisSet.xAxis;
NSMutableArray *labelTick=[[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6", nil];
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorIntervalLength= CPTDecimalFromInteger(1);
x.minorTicksPerInterval = 0;
x.majorTickLineStyle = lineStyle;
x.minorTickLineStyle = lineStyle1;
x.axisLineStyle = lineStyle2;
x.minorTickLength = 2.0f;
x.majorTickLength = 3.0f;
x.orthogonalCoordinateDecimal=CPTDecimalFromDouble(0.0);
x.majorTickLocations=[NSSet setWithArray:labelTick];
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([arrayDataY count])];
x.gridLinesRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( [arrayDataY count] )];
x.labelOffset=30.0f;
x.majorGridLineStyle = yMajorGridLineStyle;
CGFloat dateCount = [self.arrayData count];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
NSInteger i = 0;
for (NSString *d in self.arrayData)
{
NSLog(#":%#",d);
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:d textStyle:axisSet.xAxis.labelTextStyle];
// [label setRotation:45.0];
CGFloat location = i++;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = axisSet.xAxis.majorTickLength;
//label.rotation = M_PI/4;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
x.axisLabels=xLabels;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
gridLineStyle.dashPattern=[NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1],[NSDecimalNumber numberWithInt:2],nil];
CPTXYAxis *y = axisSet.yAxis;
NSMutableArray *ylabelTick=[[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8", nil];
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.majorIntervalLength = CPTDecimalFromInteger(0.5);
y.minorTicksPerInterval = 0;
y.majorTickLineStyle = lineStyle;
y.minorTickLineStyle = lineStyle1;
y.axisLineStyle = lineStyle2;
y.minorTickLength = 3.0f;
y.majorTickLength = 5.0f;
y.labelOffset=10.0f;
y.orthogonalCoordinateDecimal=CPTDecimalFromDouble(0.0);
y.majorTickLocations=[NSSet setWithArray:ylabelTick];
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([arrayDataY count])];
y.gridLinesRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat([arrayData count] )];
y.majorGridLineStyle=gridLineStyle;
CGFloat dateCounty = [self.arrayData count];
NSMutableSet *yLabels = [NSMutableSet setWithCapacity:dateCounty];
NSMutableSet *yLocations = [NSMutableSet setWithCapacity:dateCounty];
NSInteger j = 0;
for (NSString *d in self.arrayDataY)
{
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:d textStyle:axisSet.yAxis.labelTextStyle];
// [label setRotation:45.0];
CGFloat location = j++;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = axisSet.yAxis.majorTickLength;
if (j<7) {
[label setAlignment:CPTAlignmentBottom];
}
//label.rotation = M_PI/4;
if (label) {
[yLabels addObject:label];
[yLocations addObject:[NSNumber numberWithFloat:location]];
}
}
y.axisLabels=yLabels;
CPTXYPlotSpace *plotSpace2 =[[CPTXYPlotSpace alloc] init];
plotSpace2.xRange = plotSpace.xRange;
plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([arrayDataY count])];
[plotSpace2 setAllowsUserInteraction:YES];
[graph addPlotSpace:plotSpace2];
CPTXYAxis *y2 = [[CPTXYAxis alloc] init];
y2.plotSpace =plotSpace2;
y2.labelingPolicy = CPTAxisLabelingPolicyNone;
y2.orthogonalCoordinateDecimal = CPTDecimalFromFloat([arrayData count]);
y2.majorTickLineStyle=lineStyle;
y2.minorTickLineStyle=lineStyle1;
y2.axisLineStyle = lineStyle2;
y2.minorTicksPerInterval = 2;
y2.majorIntervalLength = CPTDecimalFromInteger(1);
y2.minorTickLength=5;
y2.coordinate =CPTCoordinateY;
y2.labelOffset = -30.0f;
y2.majorTickLocations=[NSSet setWithArray:ylabelTick];
y2.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([arrayDataY count])];
y2.axisLabels=yLabels;
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
}
Please help me to add multiple y axis with custom label. also i want same x axis values in top of graph also (i.e. have two x axes, one at the top and one at the bottom).
In above image y2 axis label not showing. How to set y2 axis label with custom label
Your approach is correct. All you need now is to create more axes and add them to the axis set. In my example I created an indicator line that moves with the mouse pointer when the mouse is over the graph. You can easily adjust that for fixed x and y axes (see the .coordinate member to determine what type of axis it should be).
// The second y axis is used as the current location identifier.
mainIndicatorLine = [[CPTXYAxis alloc] init];
mainIndicatorLine.hidden = YES;
mainIndicatorLine.coordinate = CPTCoordinateY;
mainIndicatorLine.plotSpace = plotSpace;
mainIndicatorLine.axisConstraints = [CPTConstraints constraintWithLowerOffset: 0];
mainIndicatorLine.labelingPolicy = CPTAxisLabelingPolicyNone;
mainIndicatorLine.separateLayers = YES;
mainIndicatorLine.preferredNumberOfMajorTicks = 6;
mainIndicatorLine.minorTicksPerInterval = 0;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 1;
lineStyle.lineColor = [CPTColor colorWithGenericGray: 64 / 255.0];
lineStyle.lineCap = kCGLineCapRound;
lineStyle.dashPattern = #[#10.0f, #5.0f];
mainIndicatorLine.axisLineStyle = lineStyle;
mainIndicatorLine.majorTickLineStyle = nil;
// Add the mainIndicatorLine to the axis set.
// It is essential to first assign the axes to be used in the arrayWithObject call
// to local variables or all kind of weird things start showing up later (mostly with invalid coordinates).
CPTXYAxisSet *axisSet = (id)mainGraph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
CPTXYAxis *y = axisSet.yAxis;
axisSet.axes = #[x, y, mainIndicatorLine];

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.

Axis Position and Plot Color

I have got two problems.
My axis are not meeting on proper point i.e. I want 0 of my y-axis to intersect first value of x-axis. I have tried many ways through orthogonalCoordinateDecimal property, but I am unable to manage it.
Secondly, my 2nd plot on right hand side is not plotting of red color as I have coded. Can you tell me what possibly may I be missing?
Figure:
Code snippets of I have written:
NSTimeInterval oneDay = 24 * 60 * 60;
// Setup plot spaces
//1- calorie
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
NSTimeInterval xLow = 0.0f;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow) length:CPTDecimalFromFloat(oneDay * 7.0f)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(600)];
//2- exercise
plotSpace2 = [[[CPTXYPlotSpace alloc] init] autorelease];
plotSpace2.allowsUserInteraction = YES;
plotSpace2.xRange = plotSpace.xRange;
plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(60.0)];
[graph addPlotSpace:plotSpace2];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.graphHost.hostedGraph.axisSet;
// Axes
CPTXYAxis *x = axisSet.xAxis;
NSTimeInterval oneDay = 24 * 60 * 60;
x.majorIntervalLength = CPTDecimalFromFloat(oneDay);
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
x.minorTicksPerInterval = 0;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromInt(200);
y.minorTicksPerInterval = 9;
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0);
y.title = #"Calories Burned";
CPTXYAxis *y2 = [[[CPTXYAxis alloc] init]retain];
y2.coordinate = CPTCoordinateY;
y2.majorIntervalLength = CPTDecimalFromString(#"5.0");
y2.minorTicksPerInterval = 3;
y2.orthogonalCoordinateDecimal = CPTDecimalFromString(#"-20.0");
y2.title = #"Exercise Time (min)";
//I added these lines too
//adding 2nd axis
NSMutableArray *newAxes = [graph.axisSet.axes mutableCopy];
[newAxes addObject:y2];
graph.axisSet.axes = newAxes;
[newAxes release];
CPTScatterPlot *caloriePlot = [[CPTScatterPlot alloc] init];
caloriePlot.dataSource = self;
caloriePlot.identifier = #"Calorie";
//create styles and symbols
CPTMutableLineStyle *calorieLineStyle = [caloriePlot.dataLineStyle mutableCopy];
calorieLineStyle.lineWidth = 2.5;
calorieLineStyle.lineColor = [CPTColor greenColor];
caloriePlot.dataLineStyle = calorieLineStyle;
[graph addPlot:caloriePlot toPlotSpace:plotSpace];
CPTScatterPlot *exercisePlot = [[CPTScatterPlot alloc] init];
exercisePlot.dataSource = self;
exercisePlot.identifier = #"Exercise";
CPTMutableLineStyle *exerciseLineStyle = [exercisePlot.dataLineStyle mutableCopy];
exerciseLineStyle.lineWidth = 2.5;
exerciseLineStyle.lineColor = [CPTColor redColor];
exerciseLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0f], [NSNumber numberWithFloat:5.0f], nil];
exercisePlot.dataLineStyle =calorieLineStyle;
[graph addPlot:exercisePlot toPlotSpace:plotSpace2];
Any help will be appreciated.Thanks.
Hard to tell without seeing more of your code. How many plot spaces do you have? Which plot space is each axis assigned to? What are the plot ranges of each plot space?
Change this line:
exercisePlot.dataLineStyle = calorieLineStyle;
to
exercisePlot.dataLineStyle = exerciseLineStyle;

core plot - broken line graph

I am trying to produce a simple line graph using core plot and am having trouble getting a solid line. The issue I am having, is that it seems the line is defaulting to a dashed line.
I am new to iOS and Xcode development so forgive me if it's painfully simple. I have attached my code, can anyone see what am I missing?
thanks in advance!
The code is based on the example code included with the project:
-(void)constructScatterPlot
{
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
graphView.hostedGraph = graph;
graph.plotAreaFrame.borderLineStyle = nil;
graph.paddingLeft = 0;
graph.paddingTop = 0;
graph.paddingRight = 0;
graph.paddingBottom = 0;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(100.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(20.0)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelFormatter = nil;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.axisLineStyle = nil;
CPTXYAxis *y = axisSet.yAxis;
y.labelFormatter = nil;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.axisLineStyle = nil;
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 1.f;
lineStyle.lineColor = [CPTColor whiteColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
}
Is it really a dashed line, or just anti-aliasing artifacts? Try increasing the line width and/or changing the line color to rule that out first.

Resources