請看下面的這張圖片。我附加了文件。我的問題與CPTAxisLabel
標籤。他們被放在勾號下,但他們不是根據我想要的CPTPlot
。iOS核心繪圖CPTAxisLabel與CPTPlot對齊
如何爲此標籤添加左偏移量?我需要將我的標籤放在CPTPlot
對象的中間。
(修訂版:)
-(void)configureGraph
{
NSInteger max = 0;
for (NSNumber *number in self.values) {
NSInteger num = [number integerValue];
if (num > max) {
max = num;
}
}
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.bounds];
graph.plotAreaFrame.masksToBorder = NO;
self.hostedGraph = graph;
graph.paddingBottom = 80.0f;
graph.paddingLeft = 0.0f;
graph.paddingTop = 50.0f;
graph.paddingRight = 0.0f;
CGFloat xMin = -0.5f;
CGFloat xMax = [self.titles count];
if (xMax < 7.0) {
xMax = 7.0;
}
CGFloat yMin = 0.0f;
CGFloat yMax = max + 25;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin) length:CPTDecimalFromFloat(xMax)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax)];
//graph.backgroundColor = [[UIColor grayColor] CGColor];
//graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor redColor]];
}
-(void)configurePlots
{
CPTBarPlot *plot = [[CPTBarPlot alloc] init];
//plot.barOffset = CPTDecimalFromFloat(0.30);
plot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:190.0f/255.0f green:203.0f/255.0f blue:103.0f/255.0f alpha:1.0]];
//plot.barWidth = CPTDecimalFromDouble(0.75);
plot.lineStyle = nil;
plot.barCornerRadius = 1.0;
plot.dataSource = self;
plot.delegate = self;
CPTGraph *graph = self.hostedGraph;
[graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
}
#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [self.titles count];
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
if ((fieldEnum == CPTBarPlotFieldBarTip) && (index < [self.titles count])) {
return [self.values objectAtIndex:index];
}
return [NSDecimalNumber numberWithUnsignedInteger:index];
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)idx
{
plot.labelOffset = 0;
CPTMutableTextStyle *style = [CPTMutableTextStyle textStyle];
style.color = [[CPTColor whiteColor] colorWithAlphaComponent:1];
style.fontName = @"Helvetica-Bold";
style.fontSize = 12.0f;
NSString *valueString = [NSString stringWithFormat:@"%@", [self.values objectAtIndex:idx]];
return [[CPTTextLayer alloc] initWithText:valueString style:style];
}
- (void)configureAxes
{
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostedGraph.axisSet;
axisSet.xAxis.hidden = YES;
axisSet.yAxis.hidden = YES;
CPTMutableTextStyle *style = [CPTMutableTextStyle textStyle];
style.color = [[CPTColor whiteColor] colorWithAlphaComponent:1];
style.fontName = @"Helvetica-Bold";
style.fontSize = 12.0f;
NSMutableArray *labels = [[NSMutableArray alloc] initWithCapacity:5];
int idx =0;
for (NSString *string in self.titles)
{
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:string textStyle:style];
label.rotation = M_PI/2;
label.tickLocation = CPTDecimalFromInt(idx);
label.offset = 10.0f;
[labels addObject:label];
idx+=1;
}
axisSet.xAxis.axisLabels = [NSSet setWithArray:labels];
}
你好,謝謝你的回答,你能否多描述一下,如何做到這一點。因爲我是核心圖 –
因爲這裏有很多選項,請在您的問題中添加一些更多信息,以幫助我們根據您的情況更清晰地回答問題。顯示如何配置情節,數據源提供了什麼樣的價值,標籤在哪裏等等。 –
當然,謝謝你的迴應。我已更新問題。 –