我准备提供我的核心图形的截图供参考,但我还没有被允许(新用户),所以我可以应要求发送电子邮件。Coreplot/Xcode帮助:在保持可见X轴的同时放大网格区域
我有一组数据点,其中x值为1〜150,y值为〜300〜450。 我想要做的就是用一个干净的网格覆盖图绘制这些点,放大 y轴(图y轴应该从y min开始,而不是0,并且到y max),同时仍然保留可见的x轴用于参考目的。
我相当接近实现我想要的东西,但我仍然在努力处理一些事情。
首先,我想在y轴和x轴之间的交叉点以及y轴的顶部的y轴上添加一个标签,以便为用户提供参考点。
为了达到这个目的,我已经开始通过玩大滴答间隔,以及在我的数据集上运行一些操作来确定最大和最小值。然而,当我然后尝试放大(即关注y-min和y max之间的范围)时,x轴不再可见。
为了解决这个问题,在整个净一些搜索后我碰到:
axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
虽然这似乎迫使x轴出现,但它似乎也会抛开我的图形......我的第一个y轴主刻度和原点之间只有3个小刻度,而原点上没有Y轴标签。
有没有人知道如何实现这个偏移量,同时强制原点成为第一个主刻度,并在原点和y-Max上显示y轴的标签?
作为一个侧面的问题,没有人知道如何显示x轴标签作为整数,而不是浮动(希望切断小数)?
我会扔我的源代码,我已经取得了迄今为止以下。
感谢,
布兰登用于初始化情节
源代码:
- (无效){initialisePlot
[self setDataMaxAndMins];
// Create a graph object which we will use to host just one scatter plot.
CGRect frame = [self.hostingView bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:frame];
// Add some padding to the graph, with more at the bottom for axis labels.
self.graph.plotAreaFrame.paddingTop = 50.0f;
self.graph.plotAreaFrame.paddingRight = 50.0f;
self.graph.plotAreaFrame.paddingBottom = 100.0f;
self.graph.plotAreaFrame.paddingLeft = 120.0f;
// Tie the graph we've created with the hosting view.
self.hostingView.hostedGraph = self.graph;
self.hostingView.backgroundColor = [UIColor blackColor];
// Create a line style that we will apply to the axis
CPTMutableLineStyle *axisStyle = [CPTMutableLineStyle lineStyle];
axisStyle.lineColor = [CPTColor grayColor];
axisStyle.lineWidth = 2.0f;
CPTColor * customGray = [CPTColor colorWithComponentRed:0.6f green: 0.6f blue: 0.6f alpha: 1.0f];
CPTMutableLineStyle *gridStyle = [CPTMutableLineStyle lineStyle];
gridStyle.lineColor = customGray;
gridStyle.lineWidth = 2.0f;
//Create plot symbol style
CPTMutableLineStyle * plotStyle = [CPTMutableLineStyle lineStyle];
plotStyle.lineColor = [CPTColor whiteColor];
plotStyle.lineWidth = 2.0f;
// Create a text style that we will use for the axis labels.
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 20;
textStyle.color = [CPTColor whiteColor];
// Create the plot symbol we're going to use.
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.lineStyle = plotStyle;
plotSymbol.size = CGSizeMake(3.0, 3.0);
//plotSymbol.
// Setup some floats that represent the min/max values on our axis.
float xAxisMin = xMin;
float xAxisMax = xMax + 5;
float yAxisMin = yMin;
float yAxisMax = yMax + 5;
// We modify the graph's plot space to setup the axis' min/max values.
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax - xAxisMin)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)];
// Modify the graph's axis with a label, line style, etc.
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
axisSet.xAxis.title = xAxisTitle;
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 50.0f;
axisSet.xAxis.axisLineStyle = axisStyle;
axisSet.xAxis.majorTickLineStyle = axisStyle;
axisSet.xAxis.minorTickLineStyle = axisStyle;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat((xAxisMax - xAxisMin)/4);//CPTDecimalFromFloat(25.0f);
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.yAxis.title = [yAxisTitle stringByAppendingString: @" ($M)"];
axisSet.yAxis.titleTextStyle = textStyle;
axisSet.yAxis.titleOffset = 80.0f;
axisSet.yAxis.axisLineStyle = axisStyle;
axisSet.yAxis.majorTickLineStyle = axisStyle;
axisSet.yAxis.minorTickLineStyle = axisStyle;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat((yAxisMax - yAxisMin)/4);
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
CPTColor * customLighterBlue = [CPTColor colorWithComponentRed:0.0f green: 0.0f blue: 0.4f alpha: 1.0f];
CPTColor * customDarkBlue = [CPTColor colorWithComponentRed:0.0f green: 0.0f blue: 0.3f alpha: 1.0f];
axisSet.xAxis.alternatingBandFills = [NSArray arrayWithObjects:customLighterBlue, customDarkBlue, nil];
axisSet.xAxis.majorGridLineStyle = gridStyle;
axisSet.yAxis.majorGridLineStyle = gridStyle;
axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
// Add a plot to our graph and axis. We give it an identifier so that we
// could add multiple plots (data lines) to the same graph if necessary.
CPTScatterPlot *plot = [[CPTScatterPlot alloc] init];
plot.dataSource = self;
plot.identifier = @"mainplot";
plot.dataLineStyle = nil;
plot.plotSymbol = plotSymbol;
[self.graph addPlot:plot];
//[_graph.defaultPlotSpace scaleToFitPlots:[_graph allPlots]];
}
我得到了标签格式化程序正常工作,感谢有关的头。 我检查了张贴CPTAxisLabelingPolicyEqualDivisions演示,但我有实现它的问题... 如果我设置的X和Y轴标签的政策,这一点,我失去了我的交替带完全,只有获得X网格线和y max ... 基本上,似乎政策只是将我的图转换为单个区域(x和y最大的1个tick,其间有4个小勾号)。 我希望能够保留区域的数量(通过在主要刻度上交替显示波段和网格线来定义) – Brandon99 2012-08-03 21:33:08
我开始将问题隔离得更好一些。 (xmax-xmin)/ 4间隔产生主要滴答,这确保了最后一个主要滴答(因而最后一个网格线)落在我的图的边缘,很好地装箱。 我使用类似的技术为yAxis,但由于我抵消了我的xAxis而遇到麻烦,因此第一个主要滴答不落在新偏移的xAxis上(滴答依然从0开始),因此我的网格最终以一个奇怪的位置。有任何想法吗? – Brandon99 2012-08-03 21:47:58
解决了它。非常感谢埃里克,你是一个核心剧情英雄:)。 使用CPTAxisLabelingPolicyEqualDivisions是解决方案的一半,另一半是设置axisSet.yAxis.preferredNumberOfMajorTicks ... 漂亮。 干杯, – Brandon99 2012-08-03 22:16:25