Iphone 核心图在垂直条形图中再添加一个图形
嗨,我想制作如图所示的条形图 我想使用coreplot示例绘制图形:(coreplot图库-垂直条形图) 当前默认代码绘制图形,如下所示 我怎样才能做到这一点 我尝试添加另一个图表,但效果不好 请提供帮助和建议 谢谢以下是我的解决方案: 在代码中,您需要添加以下内容:Iphone 核心图在垂直条形图中再添加一个图形,iphone,objective-c,cocoa-touch,core-plot,Iphone,Objective C,Cocoa Touch,Core Plot,嗨,我想制作如图所示的条形图 我想使用coreplot示例绘制图形:(coreplot图库-垂直条形图) 当前默认代码绘制图形,如下所示 我怎样才能做到这一点 我尝试添加另一个图表,但效果不好 请提供帮助和建议 谢谢以下是我的解决方案: 在代码中,您需要添加以下内容: -(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(N
-(NSNumber *)numberForPlot:(CPPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index
{
if (plot.identifier==@"locked") {
switch ( fieldEnum ) {
case CPBarPlotFieldBarLocation:
return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
case CPBarPlotFieldBarLength:
return [self.lockedPercentage objectAtIndex:index];
}
}else if (plot.identifier==@"occupied") {
switch ( fieldEnum ) {
case CPBarPlotFieldBarLocation:
return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
case CPBarPlotFieldBarLength:
return [self.occupiedPercentage objectAtIndex:index];
}
}else if (plot.identifier==@"empty") {
switch ( fieldEnum ) {
case CPBarPlotFieldBarLocation:
return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
case CPBarPlotFieldBarLength:
return [self.emptyPercentage objectAtIndex:index];
}
}
return nil;
}
线plot.identifier在这里很重要,它们检查要获取哪些数据,这里是空的还是锁定的
当然,您需要设置这些标识符,我在-(void)loadgraph中这样做:
// Bar plot Locked
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor blueColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.barOffset = 0.0f+viewOffset;
barPlot.barWidth = 15.0f;
barPlot.identifier = @"occupied";
[graph addPlot:barPlot toPlotSpace:plotSpace];
这里设置了我的一个标识符。要在x轴下方显示值,需要更改范围:
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(100.0f)];
希望这有帮助。我就是这样做的:
// Define some custom labels for the data elements
customTickLocations = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithInt:10],
[NSDecimalNumber numberWithInt:20],
[NSDecimalNumber numberWithInt:30],
[NSDecimalNumber numberWithInt:40],
[NSDecimalNumber numberWithInt:50],
[NSDecimalNumber numberWithInt:60],
[NSDecimalNumber numberWithInt:70],
[NSDecimalNumber numberWithInt:80],
[NSDecimalNumber numberWithInt:90],
[NSDecimalNumber numberWithInt:100],
nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"10%", @"20%", @"30%",
@"40%", @"50%", @"60%",@"70%",@"80%",@"90%",@"100%", nil];
labelLocation = 0;
customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++]
textStyle:y.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = y.labelOffset;
[customLabels addObject:newLabel];
//[newLabel release];
}
y.axisLabels = [NSSet setWithArray:customLabels];
y.majorTickLocations = [NSSet setWithArray:customTickLocations];
您定义了两个数组,一个带有标签,一个带有放置它们的值,定义“CPXAxisLabel”并将它们放在另一个数组中,这个数组可以放在图形上。
希望这能有所帮助。@Charles,谢谢你的回复。我可以在原始coreplot代码中绘制多个条形图,但当我尝试在我的应用程序中实现时,会出现错误“case CPBarPlotFieldBarLength:“未声明可能有什么问题?我只能猜测,因为我的代码中没有声明任何CPBarPlotFieldBarLength。你的代码里有吗?然后尝试注释它,因为您似乎不需要它。另一种可能是您没有设置plotspace.yrange或barplot.xrange CPXYPlotSpace*plotspace=(CPXYPlotSpace*)graph.defaultPlotSpace;plotSpace.yRange=[CPPlotRange plotRangeWithLocation:cpdecimalfloat(0.0f)长度:cpdecimalfloat(100.0f)];plotSpace.xRange=[CPPlotRange plotRangeWithLocation:cpdecimalfloat(0.0f)长度:cpdecimalfloat([name count]);我可以通过将CPBarPlotFieldBarLength重命名为CPBarPlotFieldBarTip来解决这个问题。。请您指导我如何在x轴和Y轴上设置核心图的标签如何设置两个标签之间的间隙(空白),因为您在Y轴上写了10%20%。再次感谢您的帮助,但我仍然面临在barPlot中排列条形图的问题。barOffset=0.0f+viewOffset;barPlot.barWidth=15.0f;在viewOffset中传递的值是多少?我安排不好酒吧。这可不容易。我是这样做的:CGFloat viewOffset=(1.0f/[name count])*15.0f;15,是条形图的宽度,名称计数与我的plotSpace.xRange的数量相同