Ale*_*exR 4 charts objective-c core-plot ios
我正在使用Core Plot 条形图来描绘公司的增长率.我想将公司的股票符号作为x轴上的标签,其中心位于各自的条形下方.不幸的是,我花了很多时间寻找一种正确对准x标签的方法,但是使用我的代码却没有成功.如何让x轴标签正确居中?
我的图表设置如下:
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
barPlot.baseValue = CPTDecimalFromInt(0);
barPlot.barOffset = CPTDecimalFromFloat(0.5f);
barPlot.barWidth = CPTDecimalFromFloat(0.5f);
double xAxisStart = 0;
double xAxisLength = self.companies.count;
double yAxisStart = 0;
double yAxisLength = 0.5;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xAxisStart) length:CPTDecimalFromDouble(xAxisLength + 1.0)] ;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(yAxisStart) length:CPTDecimalFromDouble(yAxisLength)] ;
barPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(+0.0) length:CPTDecimalFromDouble(xAxisLength)] ;
Run Code Online (Sandbox Code Playgroud)
在下面的代码片段中,我尝试使用自定义标签,但没有成功,如下面的示例图表所示.
xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
NSMutableArray *customLabels = [[NSMutableArray alloc]init];
[self.companies enumerateObjectsUsingBlock:^(IBCompany *company, NSUInteger idx, BOOL *stop) {
NSString *labelText = company.contrSymbol;
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:labelText textStyle:xAxis.labelTextStyle];
label.tickLocation = CPTDecimalFromDouble(idx + 0.5);
label.offset = xAxis.labelOffset + xAxis.majorTickLength;
label.rotation = M_PI * 2;
[customLabels addObject:label];
}];
xAxis.axisLabels = [NSSet setWithArray:customLabels];
Run Code Online (Sandbox Code Playgroud)
请注意,我的条形图索引从1开始:
-(NSArray *)numbersForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange
{
if ( [plot.identifier isEqual:plotIdentifier] ) {;
if ( fieldEnum == CPTScatterPlotFieldX ) {
NSMutableArray *indexArray = [[NSMutableArray alloc] init];
[self.companies enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[indexArray addObject:[NSDecimalNumber numberWithInt: idx + 1]];
}];
return [indexArray copy];
}
else if ( fieldEnum == CPTScatterPlotFieldY ) {
// ..
}
}
else return nil; // should be considered as ERROR
Run Code Online (Sandbox Code Playgroud)
}
您应该养成在数据源中使用正确的绘图类型的字段标识符的习惯.对于条形图,你应该使用CPTBarPlotFieldBarLocation
和CPTBarPlotFieldBarTip
.在这种情况下没有区别,但对于其他情节类型并不总是如此.例如,水平条形图的x和y坐标相反.
数据源始终返回相同的数据集.您需要检查indexRange
参数并仅返回请求的范围.
x轴的绘图范围在0到4之间.标签分别为0.5,1.5和2.5.该plotRange
是什么搞乱了钢筋间距.将其设置为默认值,nil
您应该获得所需的外观.来自plotRange文档:
如果提供绘图范围,则在整个绘图范围内均匀间隔条.如果plotRange为nil,则条形位置由Cocoa绑定或条形图数据源提供.如果绑定或数据源未提供位置,则第一个条将位于零(0),后续条将位于连续的正整数坐标处.
归档时间: |
|
查看次数: |
3228 次 |
最近记录: |