我有一个像这样的Core Plot BarChart:
如您所见 - 第一个条的一半被y轴隐藏。如何使图形的可见区域的边界更宽一些?
修改:
- (void) drawBarPlot
{
[self configureBarGraph];
[self configureBarPlot];
[self configureAxes];
}
- (void) configureBarGraph {
// 1 - Create the graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.barChartView.bounds];
graph.plotAreaFrame.masksToBorder = NO;
self.barChartView.hostedGraph = graph;
// 2 - Configure the graph
[graph applyTheme:nil];
graph.paddingBottom = 0.0f;
graph.paddingLeft = 0.0f;
graph.paddingTop = 0.0f;
graph.paddingRight = 0.0f;
graph.plotAreaFrame.paddingLeft = 30.0f;
graph.plotAreaFrame.paddingTop = 20.0f;
graph.plotAreaFrame.paddingRight = 30.0f;
graph.plotAreaFrame.paddingBottom = 20.0f;
// 3 - Set up plot space
CGFloat xMin = 0.0f;
CGFloat xMax = (CGFloat)[[[[PlotDataStore sharedInstance] docsCount] valueForKey:@"docs"] count];
CGFloat yMin = 0.0f;
CGFloat yMax = 10.0f; // should determine dynamically based on max price
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin) length:CPTDecimalFromFloat(xMax)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax)];
}
- (void) configureBarPlot {
// 1 - Set up the plot
self.barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO];
// 2 - Set up line style
CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init];
barLineStyle.lineColor = [CPTColor lightGrayColor];
barLineStyle.lineWidth = 0.5;
// 3 - Add plots to graph
CPTGraph *graph = self.barChartView.hostedGraph;
self.barPlot.dataSource = self;
self.barPlot.delegate = self;
self.barPlot.barCornerRadius = 0.0f;
self.barPlot.lineStyle = barLineStyle;
self.barPlot.baseValue = CPTDecimalFromDouble(0.0f);
self.barPlot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:100.0f / 255.0f green:192.0f / 255.0f blue:245.0f / 255.0f alpha:1.0f]];
[graph addPlot:self.barPlot toPlotSpace:(CPTPlotSpace *) graph.defaultPlotSpace];
}
- (void) configureAxes {
// 1 - Configure styles
CPTMutableTextStyle *axisLabelsStyle = [CPTMutableTextStyle textStyle];
axisLabelsStyle.color = [CPTColor grayColor];
axisLabelsStyle.fontName = @"Helvetica";
// 2 - Get the graph's axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.barChartView.hostedGraph.axisSet;
axisSet.xAxis.axisLineStyle = nil;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.xAxis.labelTextStyle = axisLabelsStyle;
NSArray *customTickLocations = [NSArray arrayWithObjects: [NSDecimalNumber numberWithInt:0], [NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:2], [NSDecimalNumber numberWithInt:3], [NSDecimalNumber numberWithInt:4], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:6], [NSDecimalNumber numberWithInt:7], [NSDecimalNumber numberWithInt:8], [NSDecimalNumber numberWithInt:9], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:11], [NSDecimalNumber numberWithInt:11], nil];
NSArray *xAxisLabels = [[[PlotDataStore sharedInstance] docsCount] valueForKey:@"dates"];
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:[tickLocation intValue]] textStyle:axisSet.xAxis.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.minorTickLength;
[customLabels addObject:newLabel];
}
axisSet.xAxis.axisLabels = [NSSet setWithArray:customLabels];
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.yAxis.axisLineStyle = nil;
axisSet.yAxis.axisLabels = nil;
}
- (NSUInteger) numberOfRecordsForPlot:(CPTPlot *)plot {
if ([plot class] == [CPTBarPlot class]) {
return (NSUInteger)[[[[PlotDataStore sharedInstance] docsCount] valueForKey:@"docs"] count];
}
return 0;
}
- (NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
if ([plot class] == [CPTBarPlot class]) {
switch ( fieldEnum ) {
case CPTBarPlotFieldBarLocation:
return [NSNumber numberWithUnsignedInteger:index];
break;
case CPTBarPlotFieldBarTip:
return (NSNumber *)[[[[PlotDataStore sharedInstance] docsCount] valueForKey:@"docs"] objectAtIndex:index];
break;
}
}
}
答案 0 :(得分:1)
条形位置是整数。因此,当xRange
从零(0)开始时,第一个条位于图的左边缘的中心。要修复它,请设置起始位置-0.5和长度(条形数+ 1)。
答案 1 :(得分:0)
您可以设置图形的各种属性或绘制影响显示宽度的空间。例如,您可以设置plotSpace的xRange
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(calculatedLength)];
或y轴和x轴的orthogonalCoordinateDecimal
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0);