我正在尝试绘制一个简单的图表(不能这么难!我之前已经成功使用过核心图!)。我有一些日期要绘制在 x 轴上。还有一些 y 轴上的小浮点值,比如 0.139503、0.139481 等。我必须绘制 5 个这样的值(我从后端获取)。对于 y 轴范围,我找到这些值中的最低值并将其设置为 y 轴范围的开始。并将majorIntervalLength 设置为(highestValue - LowestValue)/5,即(例如)0.007946。但是 y 轴间隔似乎固定为 1。所以,由于我的 y 轴值非常小(它们之间的差异在 0.01 左右),所有图都在 y=0.0 处绘制。这是代码片段:
-(void) initializeGraph {
if ((self.graph == nil) || (self.graph == NULL)) {
self.graphHostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostView.bounds];
graph.paddingLeft = 2.0;
graph.paddingRight = 2.0;
graph.paddingTop = 2.0;
graph.paddingBottom = 2.0;
graph.title = @"Graph Plot";
// plotting style is set to line plots
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.graph.axisSet;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
axisSet.xAxis.labelRotation = 90.0;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.graph.defaultPlotSpace;
// Determine the lowest and highest values to be plotted in Y Axis)
CGFloat yLow = 0.1;
CGFloat yHigh = yLow;
for (int i=0 ; i<self.dict.count ; i++) {
MyCustomObject *myData = (MyCustomObject *) [dict objectForKey:
[self.arrayKeys objectAtIndex:i]];
if (myData.mem < yLow) {
yLow = myData.mem;
}
if (yHigh < myData.mem) {
yHigh = myData.mem;
}
}
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(5)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yLow)
length:CPTDecimalFromFloat(5)];
// To determine the point where your graph starts
//axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(yLow);
axisSet.yAxis.majorIntervalLength = CPTDecimalFromCGFloat((yHigh-yLow)/5.0);
NSLog(@"Y axis interval : %f", (yHigh-yLow)/5.0);
...
}
-(NSNumber *) numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index {
if ( fieldEnum == CPTScatterPlotFieldY ) {
return [NSNumber numberWithFloat:myData.mem];
} else {
return [NSNumber numberWithInt:index];
}
}
为什么设置的 y 轴的 majorIntervalLength 不起作用?