0

我将提供我的核心绘图图的屏幕截图以供参考,但我还不允许(新用户),所以我可以根据要求通过电子邮件发送它。

我有一组数据点,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 轴标签显示为整数而不是浮点数(希望截断小数点)?

我将在下面给出我迄今为止所取得的成果的源代码。

谢谢,

布兰登

初始化绘图的源代码:

-(void)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]];

}

4

1 回答 1

0
  1. 更改标签政策。我想CPTAxisLabelingPolicyEqualDivisions会做你想做的。Plot Gallery 示例应用程序中有一个演示,它显示了所有可用的标签策略。

  2. labelFormatter在 x 轴上设置。它是一个标准的NSNumberFormatter

于 2012-08-03T01:02:02.680 回答