gpt4 book ai didi

ios - 第一个栏的一半隐藏在 Core Plot 中

转载 作者:行者123 更新时间:2023-11-29 03:17:58 25 4
gpt4 key购买 nike

我有一个像这样的核心图条形图:

enter image description here

如您所见 - 第一个条的一半被 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;

}
}
}

最佳答案

条形位置是整数。因此,当 xRange 从零 (0) 开始时,第一个条形图位于绘图左边缘的中心。要修复此问题,请将起始位置设置为 -0.5 并将长度设置为(条数 + 1)。

关于ios - 第一个栏的一半隐藏在 Core Plot 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21451015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com