我正在使用核心圖條形圖繪製公司的增長率。我希望將公司的股票代碼作爲標籤,以x軸爲中心,位於的下方。不幸的是,我花了很多時間尋找一種方法來正確地將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)] ;
在下面的代碼片段中,我試圖用自定義標籤,但沒有成功。如下圖示例圖表。
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];
請注意,我對條形圖指數與開始:
-(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
}