CorePlot: what is the difference between majorTickLocations (graph) and setYRange (plot space)?

三世轮回 提交于 2019-12-23 05:41:36

问题


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];

回答1:


Try to set this policy:

y.labelingPolicy = CPTAxisLabelingPolicyNone;


来源:https://stackoverflow.com/questions/32199713/coreplot-what-is-the-difference-between-majorticklocations-graph-and-setyrang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!