我试图绘制一个简单的图形(不能太难!我以前成功使用过核心图!)。我有一些日期要在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?
答案 0 :(得分:0)
你在任何地方设置axisSet.yAxis.majorTickLength吗?我在上面的代码中没有看到它。
如果majorTickLength为零,则不会显示任何内容。
答案 1 :(得分:0)
将范围长度(如Erik Skroch所建议)设置为(tHigh-yelLow)帮助