CorePlot:majorTickLocations(graph)和setYRange(plot space)有什么区别? [英] CorePlot: what is the difference between majorTickLocations (graph) and setYRange (plot space)?
问题描述
我想在图表中创建 3个色带,但我认为我做错了什么。 y的值应该在 0到1024 的范围内,我想要第一个颜色带从0到300,第二个从300到600,第三个从600到1024.但是,代码我写的创建了6个乐队而不是3个,它似乎忽略了1024个限制。下面是结果图和我使用的代码:
图:
它应该只有3个乐队,再加上它似乎忽略了我在绘图空间中设置1024的范围。但是我不确定主要的地点的含义和用法。我开始认为与剧情空间范围没有直接关系。
代码:
self。 graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostView.bounds];
self.graphHostView.hostedGraph = self.graph;
CPTMutableTextStyle * titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor blackColor];
titleStyle.fontName = @Helvetica;
titleStyle.fontSize = 12.0f;
//轴
//用固定间隔策略标记x轴
CPTXYAxisSet * axisSet =(CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis * x = axisSet.xAxis;
x.separateLayers = NO;
x.minorTicksPerInterval = 4;
x.minorTickLength = 8.0;
x.title = @X Axis;
x.titleTextStyle = titleStyle;
x.delegate = self;
CPTXYAxis * y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.separateLayers = YES;
y.majorTickLocations = [NSSet setWithObjects:
[NSDecimalNumber numberWithDouble:0],
[NSDecimalNumber numberWithDouble:300],
[NSDecimalNumber numberWithDouble:600],
[ NSDecimalNumber numberWithDouble:1024],
nil];
y.title = @Y轴;
y.titleTextStyle = titleStyle;
$ b $ y.alternatingBandFills = [NSArray arrayWithObjects:[CPTColor whiteColor],
[[CPTColor greenColor] colorWithAlphaComponent:CPTFloat(0.3)],
[[CPTColor redColor] colorWithAlphaComponent:CPTFloat (0.3)],
无];
y.delegate = self;
//将y2轴添加到轴设置
self.graph.axisSet.axes = @ [x,y];
self.graph.title = @My graph;
self.graph.titleDisplacement = CGPointMake(0.0f,-68.0f);
self.graph.titleTextStyle = titleStyle;
CPTMutableLineStyle * axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [[CPTColor blackColor] colorWithAlphaComponent:1];
//从图表中获取(默认)图形空间,以便我们可以设置其x / y范围
CPTXYPlotSpace * plotSpace =(CPTXYPlotSpace *)self.graph.defaultPlotSpace;
//请注意,这些CPTPlotRange由START和LENGTH(而不是START和END)定义!
[plotSpace setYRange:[CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:1024]]];
[plotSpace setXRange:[CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:100]]];
//创建图(我们没有定义实际的x / y值,这些将由数据源提供...)
self.plot = [[CPTScatterPlot alloc] initWithFrame :CGRectZero];
//让我们保持简单并让这个类作为数据源(因此我们实现< CPTPlotDataSource>)
self.plot.dataSource = self;
//最后,将创建的图添加到我们在
之前创建的CPTGraph对象的默认绘图空间[self.graph addPlot:self.plot toPlotSpace:self.graph.defaultPlotSpace];
尝试设置此政策:
y.labelingPolicy = CPTAxisLabelingPolicyNone;
I would like to create 3 colour bands in my graph but I think I am doing something wrong. The y values should range from 0 to 1024 and I would like to have a first colour band from 0 to 300, a second one from 300 to 600 and a third one from 600 to 1024. However the code I wrote creates 6 bands instead of 3 and it seems to ignore the 1024 limit. Here is the resulting graph and the code I used:
Graph:
It should have only 3 bands, plus it seems to ignore the fact that I set a range of 1024 in the plot space. However I am unsure on the meaning and usage of majorTickLocations. I am starting to think that there is no direct correlation with the plot space range.
Code:
self.graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostView.bounds];
self.graphHostView.hostedGraph = self.graph;
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor blackColor];
titleStyle.fontName = @"Helvetica";
titleStyle.fontSize = 12.0f;
// Axes
// Label x axis with a fixed interval policy
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.separateLayers = NO;
x.minorTicksPerInterval = 4;
x.minorTickLength = 8.0;
x.title = @"X Axis";
x.titleTextStyle = titleStyle;
x.delegate = self;
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.separateLayers = YES;
y.majorTickLocations = [NSSet setWithObjects:
[NSDecimalNumber numberWithDouble:0],
[NSDecimalNumber numberWithDouble:300],
[NSDecimalNumber numberWithDouble:600],
[NSDecimalNumber numberWithDouble:1024],
nil];
y.title = @"Y Axis";
y.titleTextStyle = titleStyle;
y.alternatingBandFills = [NSArray arrayWithObjects: [CPTColor whiteColor],
[[CPTColor greenColor] colorWithAlphaComponent:CPTFloat(0.3)],
[[CPTColor redColor] colorWithAlphaComponent:CPTFloat(0.3)],
nil];
y.delegate = self;
// Add the y2 axis to the axis set
self.graph.axisSet.axes = @[x, y];
self.graph.title = @"My graph";
self.graph.titleDisplacement = CGPointMake(0.0f, -68.0f);
self.graph.titleTextStyle = titleStyle;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [[CPTColor blackColor] colorWithAlphaComponent:1];
// Get the (default) plotspace from the graph so we can set its x/y ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.graph.defaultPlotSpace;
// Note that these CPTPlotRange are defined by START and LENGTH (not START and END) !!
[plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:1024]]];
[plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:100]]];
// Create the plot (we do not define actual x/y values yet, these will be supplied by the datasource...)
self.plot = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
// Let's keep it simple and let this class act as datasource (therefore we implemtn <CPTPlotDataSource>)
self.plot.dataSource = self;
// Finally, add the created plot to the default plot space of the CPTGraph object we created before
[self.graph addPlot:self.plot toPlotSpace:self.graph.defaultPlotSpace];
Try to set this policy:
y.labelingPolicy = CPTAxisLabelingPolicyNone;
这篇关于CorePlot:majorTickLocations(graph)和setYRange(plot space)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!