核心情节“numberForPlot”没有正确执行

时间:2012-02-20 03:13:03

标签: xcode core-plot

我正在努力学习CorePlot。我在“codejunkster”中找到了一个代码示例,我跟随“T”,但它无效。我将其缩小到numberForPlot方法。代码如下。这两种情况都不对,我无法弄清楚原因。

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    if ( [plot.identifier isEqual:@"chocoplot"] )
    {
        NSDictionary *bar = [self.dataForChart objectAtIndex:index];

        if(fieldEnum == CPTBarPlotFieldBarLocation)
        {
            return [bar valueForKey:BAR_POSITION];
            NSLog(@"return [bar valueForKey:BAR_POSITION]");
        }
        else if(fieldEnum ==CPTBarPlotFieldBarTip){
            NSLog(@"return [bar valueForKey:BAR_HEIGHT];");
            return [bar valueForKey:BAR_HEIGHT];
        }
    }
   return [NSNumber numberWithFloat:0];
}

根据教程,下面是一段定义CPTBarPlot ...

的代码片段
// Create bar plot and add it to the graph
CPTBarPlot *plot = [[CPTBarPlot alloc] init] ;
plot.dataSource = self;
plot.delegate = self;
plot.barWidth = [[NSDecimalNumber decimalNumberWithString:@"5.0"]
                 decimalValue];
plot.barOffset = [[NSDecimalNumber decimalNumberWithString:@"10.0"]
                  decimalValue];
plot.barCornerRadius = 5.0;
// Remove bar outlines
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor clearColor];
plot.lineStyle = borderLineStyle;

// Identifiers are handy if you want multiple plots in one graph
plot.identifier = @"chocoplot";

[self.graph addPlot:plot];

2 个答案:

答案 0 :(得分:2)

枚举定义在1月份发生了变化:

typedef enum _CPTBarPlotField {
    CPTBarPlotFieldBarLocation, ///< Bar location on independent coordinate axis.
    CPTBarPlotFieldBarTip,      ///< Bar tip value.
    CPTBarPlotFieldBarBase      ///< Bar base (used only if @link CPTBarPlot::barBasesVary barBasesVary @endlink is YES).
} CPTBarPlotField;

确保identifier测试正在通过。如果是,并且编译器由于某种原因仍然具有旧的枚举值,请尝试干净的构建。

答案 1 :(得分:0)

这可能是一个错误,或者您已经初始化了另一种类型的情节。

以下是CPTBarPlotField

的声明
/**     @brief Enumeration of bar plot data source field types
 **/
typedef enum _CPTBarPlotField {
    CPTBarPlotFieldBarLocation = 2,  ///< Bar location on independent coordinate axis.
    CPTBarPlotFieldBarTip         = 3,  ///< Bar tip value.
    CPTBarPlotFieldBarBase     = 4      ///< Bar base (used only if barBasesVary is YES).
} CPTBarPlotField;

如您所见,CPTBarPlot无法返回值1。检查初始化图的代码。