问题描述
||
嗨,我想制作条形图,如下所示
我想使用coreplot示例绘制图形:(coreplot画廊-垂直条形图)
当前默认代码绘制该图,如下所示
我该如何实现?
我尝试添加另一个图,但效果不佳。
请帮助和建议
谢谢
解决方法
这是我的解决方案:
在您的代码中,您需要添加以下内容:
-(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)加载图中执行此操作:
// 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];
您定义2个数组,一个带有标签,另一个带有将它们放置在其中的值,定义\“ CPXAxisLabel \”并将它们放置在另一个数组中,然后可以将该数组放在图形上。
希望这可以帮助。