如何使用 Core Plot 设置图表宽度?
我有一个 nib 文件,文件所有者设置为此视图,并且它只有一个设置了自定义类 CPTGraphHostingView 的视图。它通过插座连接到该视图。此视图已加载,但它显示的是一个非常小的条形图。我真的不确定为什么。此外,添加到 IB 视图中的任何内容都会颠倒过来。
图表在什么时候设置宽度?我认为使用 self.view.bounds 只会使图表耗尽所有可用的屏幕空间。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:@"PollResultView" bundle:nil];
self.title = @"Results";
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
CPTXYPlotSpace *barPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(100)];
barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(30)];
[graph addPlotSpace:barPlotSpace];
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 1.0f;
majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.75f];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 1.0f;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.25f];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromInteger(10);
x.minorTicksPerInterval = 9;
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.labelOffset = 10.0f;
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f)
length:CPTDecimalFromFloat(10.0f)];
x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)
length:CPTDecimalFromFloat(100.0f)];
x.title = @"X Axis";
x.titleOffset = 30.0f;
x.titleLocation = CPTDecimalFromFloat(5.0f);
x.plotSpace = barPlotSpace;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromInteger(10);
y.minorTicksPerInterval = 9;
y.orthogonalCoordinateDecimal = CPTDecimalFromInteger(-0.5);
y.preferredNumberOfMajorTicks = 8;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.labelOffset = 10.0f;
y.labelRotation = M_PI/2;
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)
length:CPTDecimalFromFloat(100.0f)];
y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f)
length:CPTDecimalFromFloat(10.0f)];
y.title = @"Y Axis";
y.titleOffset = 30.0f;
y.titleLocation = CPTDecimalFromInteger(55);
y.plotSpace = barPlotSpace;
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil];
CPTMutableLineStyle *barLineStyle = [[[CPTMutableLineStyle alloc] init] autorelease];
barLineStyle.lineWidth = 1.0f;
barLineStyle.lineColor = [CPTColor whiteColor];
CPTBarPlot *barPlot = [[[CPTBarPlot alloc] init] autorelease];
barPlot.lineStyle = barLineStyle;
barPlot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.0f
green:0.0f
blue:0.5f
alpha:0.5f]];
barPlot.barBasesVary = YES;
barPlot.barWidth = CPTDecimalFromFloat(0.5f); // bar is 50% of the available space
barPlot.barCornerRadius = 10.0f;
barPlot.barsAreHorizontal = NO;
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
barPlot.barLabelTextStyle = whiteTextStyle;
// barPlot.delegate = self;
barPlot.dataSource = self;
barPlot.identifier = @"Bar Plot 1";
[graph addPlot:barPlot toPlotSpace:barPlotSpace];
CPTBarPlot *barPlot2 = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor]
horizontalBars:NO];
barPlot2.lineStyle = barLineStyle;
barPlot2.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.0f
green:1.0f
blue:0.5f
alpha:0.5f]];
barPlot2.barBasesVary = YES;
barPlot2.barWidth = CPTDecimalFromFloat(1.0f);
barPlot2.barCornerRadius = 2.0f;
barPlot2.barsAreHorizontal = NO;
//barPlot2.delegate = self;
barPlot2.dataSource = self;
barPlot2.identifier = @"Bar Plot 2";
[graph addPlot:barPlot2 toPlotSpace:barPlotSpace];
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
theLegend.numberOfRows = 2;
theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]];
theLegend.borderLineStyle = barLineStyle;
theLegend.cornerRadius = 10.0;
theLegend.swatchSize = CGSizeMake(20.0, 20.0);
whiteTextStyle.fontSize = 16.0;
theLegend.textStyle = whiteTextStyle;
theLegend.rowMargin = 10.0;
theLegend.paddingLeft = 12.0;
theLegend.paddingTop = 12.0;
theLegend.paddingRight = 12.0;
theLegend.paddingBottom = 12.0;
NSArray *plotPoint = [NSArray arrayWithObjects:[NSNumber numberWithInteger:0], [NSNumber numberWithInteger:95], nil];
CPTPlotSpaceAnnotation *legendAnnotation = [[[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:barPlotSpace anchorPlotPoint:plotPoint] autorelease];
legendAnnotation.contentLayer = theLegend;
legendAnnotation.contentAnchorPoint = CGPointMake(0.0f, 1.0f);
[graph.plotAreaFrame.plotArea addAnnotation:legendAnnotation];
graphHostingView.hostedGraph = graph;
}
I have a nib file with File's Owner set to this view and it only has one View that has a custom class CPTGraphHostingView set. It is connected to this view via an outlet. This view loads, but what it displays is a very tiny bar graph. And I am really unsure why. Also, anything that is added to the view in IB turns up upside down.
At which point is the chart getting a width set to it? I assume that using self.view.bounds would just make the chart use up all available screen space.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:@"PollResultView" bundle:nil];
self.title = @"Results";
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
CPTXYPlotSpace *barPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(100)];
barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(30)];
[graph addPlotSpace:barPlotSpace];
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 1.0f;
majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.75f];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 1.0f;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.25f];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromInteger(10);
x.minorTicksPerInterval = 9;
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.labelOffset = 10.0f;
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f)
length:CPTDecimalFromFloat(10.0f)];
x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)
length:CPTDecimalFromFloat(100.0f)];
x.title = @"X Axis";
x.titleOffset = 30.0f;
x.titleLocation = CPTDecimalFromFloat(5.0f);
x.plotSpace = barPlotSpace;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromInteger(10);
y.minorTicksPerInterval = 9;
y.orthogonalCoordinateDecimal = CPTDecimalFromInteger(-0.5);
y.preferredNumberOfMajorTicks = 8;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.labelOffset = 10.0f;
y.labelRotation = M_PI/2;
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)
length:CPTDecimalFromFloat(100.0f)];
y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f)
length:CPTDecimalFromFloat(10.0f)];
y.title = @"Y Axis";
y.titleOffset = 30.0f;
y.titleLocation = CPTDecimalFromInteger(55);
y.plotSpace = barPlotSpace;
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil];
CPTMutableLineStyle *barLineStyle = [[[CPTMutableLineStyle alloc] init] autorelease];
barLineStyle.lineWidth = 1.0f;
barLineStyle.lineColor = [CPTColor whiteColor];
CPTBarPlot *barPlot = [[[CPTBarPlot alloc] init] autorelease];
barPlot.lineStyle = barLineStyle;
barPlot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.0f
green:0.0f
blue:0.5f
alpha:0.5f]];
barPlot.barBasesVary = YES;
barPlot.barWidth = CPTDecimalFromFloat(0.5f); // bar is 50% of the available space
barPlot.barCornerRadius = 10.0f;
barPlot.barsAreHorizontal = NO;
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
barPlot.barLabelTextStyle = whiteTextStyle;
// barPlot.delegate = self;
barPlot.dataSource = self;
barPlot.identifier = @"Bar Plot 1";
[graph addPlot:barPlot toPlotSpace:barPlotSpace];
CPTBarPlot *barPlot2 = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor]
horizontalBars:NO];
barPlot2.lineStyle = barLineStyle;
barPlot2.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.0f
green:1.0f
blue:0.5f
alpha:0.5f]];
barPlot2.barBasesVary = YES;
barPlot2.barWidth = CPTDecimalFromFloat(1.0f);
barPlot2.barCornerRadius = 2.0f;
barPlot2.barsAreHorizontal = NO;
//barPlot2.delegate = self;
barPlot2.dataSource = self;
barPlot2.identifier = @"Bar Plot 2";
[graph addPlot:barPlot2 toPlotSpace:barPlotSpace];
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
theLegend.numberOfRows = 2;
theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]];
theLegend.borderLineStyle = barLineStyle;
theLegend.cornerRadius = 10.0;
theLegend.swatchSize = CGSizeMake(20.0, 20.0);
whiteTextStyle.fontSize = 16.0;
theLegend.textStyle = whiteTextStyle;
theLegend.rowMargin = 10.0;
theLegend.paddingLeft = 12.0;
theLegend.paddingTop = 12.0;
theLegend.paddingRight = 12.0;
theLegend.paddingBottom = 12.0;
NSArray *plotPoint = [NSArray arrayWithObjects:[NSNumber numberWithInteger:0], [NSNumber numberWithInteger:95], nil];
CPTPlotSpaceAnnotation *legendAnnotation = [[[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:barPlotSpace anchorPlotPoint:plotPoint] autorelease];
legendAnnotation.contentLayer = theLegend;
legendAnnotation.contentAnchorPoint = CGPointMake(0.0f, 1.0f);
[graph.plotAreaFrame.plotArea addAnnotation:legendAnnotation];
graphHostingView.hostedGraph = graph;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
托管视图将使图表自动填充其边界。只要托管视图的大小正确适合其容器,初始化时使用什么并不重要。这里发生了几件事:
您在白色背景上使用白色网格线。他们很难这么看。 :-) 更改线条颜色或在图表上设置不同颜色的填充。
y 轴上的复制/粘贴错误:
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
应该是
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
无需为轴设置可见范围 - 无论如何您都不会绘制它们。
也无需设置 gridLinesRange - 默认值是您正在使用的绘图范围。
使用 barBasesVary,确保您的数据源还提供
CPTBarPlotFieldBarBase
字段的数据。使用绘图空间注释时,图例的锚点必须位于绘图空间范围内(当前为 x: [0, 100] 和 y: [0, 30]),否则它将不可见。
您需要在
graph.plotAreaFrame
上设置填充以使轴标签和标题可见。您还可以调整graph
上的填充以设置绘图区域框架之外的整体边距。埃里克
The hosting view will make the graph fill it's bounds automatically. It doesn't matter what you use when you initialize it as long as the hosting view is sized to its container correctly. There are several things going on here:
You're using white gridlines on a white background. They're hard to see that way. :-) Change the line colors or set a fill on the graph that's a different color.
Copy/paste error on the y-axis:
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
should be
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
There's no need to set the visible range for your axes--you're not drawing them anyway.
There's no need to set the gridLinesRange either--the default is the plot range which is what you're using.
With barBasesVary, make sure your datasource also provides data for the
CPTBarPlotFieldBarBase
field.When using a plot space annotation, the anchor point for the legend has to be within the plot space ranges (currently x: [0, 100] and y: [0, 30]) or it won't be visible.
You need to set the padding on
graph.plotAreaFrame
to make the axis labels and titles visible. You can also adjust the padding ongraph
to set the overall margin outside the plot area frame.Eric
您可以使用以下命令将其设置为非常小的图形:
对于实际绘图区域,100 像素宽和 30 像素高非常小。
我的代码使用
graph = [CPTXYGraph alloc] initWithFrame:CGRectZero];
进行初始设置,并且工作正常。我认为实现中的一些代码会在发生这种情况时将大小设置为包含视图。乔
You're setting it to a very small graph with these commands:
100 pixels wide and 30 pixels high is very small, for the actual plot area.
My code uses
graph = [CPTXYGraph alloc] initWithFrame:CGRectZero];
for the initial setup, and it works fine. I think there's some code in the implementation that sets the size to the containing view when that happens.joe