iphone - 核心图条形图 - 条不显示

标签 iphone ios ipad graph core-plot

我已经实现了如下的核心绘图条形图,它触发数据源并获取值。但情节没有显示任何条形图。有人可以告诉我问题出在哪里。我刚刚开始使用 CorePlot for Graphics。

- (NSMutableArray *)plotDataValues
{
    NSMutableArray *array = [NSMutableArray arrayWithObjects:[NSNumber numberWithDouble:4500123.213],[NSNumber numberWithDouble:12000500.213],[NSNumber numberWithDouble:10800456.213],[NSNumber numberWithDouble:7000500.213],[NSNumber numberWithDouble:5500300.213],[NSNumber numberWithDouble:3800623.213],[NSNumber numberWithDouble:2600345.213],[NSNumber numberWithDouble:1900764.213],[NSNumber numberWithDouble:8600500.213],[NSNumber numberWithDouble:1200376.213], nil];

    return array;
}



- (void)initTopPropertyPlot
{
    self.topWellsGraphHostingView.allowPinchScaling = NO;
    [self configurePropertyListGraph];
    [self configurePropertyPlot];
    [self configurePropertyPlotAxes];
}

- (void)configurePropertyListGraph
{
    //Create the Bar Graph.
    self.propertyListBarChart = [[CPTXYGraph alloc] initWithFrame:self.topWellsGraphHostingView.bounds];
    self.propertyListBarChart.plotAreaFrame.masksToBorder = NO;
    self.topWellsGraphHostingView.hostedGraph = self.propertyListBarChart;

    // 2 - Configure the graph
    //[self.propertyListBarChart applyTheme:[CPTTheme themeNamed:kCPTSlateTheme]];
    [self.propertyListBarChart applyTheme:nil];
    self.propertyListBarChart.paddingBottom = 30.0f;
    self.propertyListBarChart.paddingLeft   = 50.0f;
    self.propertyListBarChart.paddingTop    = 20.0f;
    self.propertyListBarChart.paddingRight  = 0.0f;

    // 3 - Set up styles
    CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
    titleStyle.color = [CPTColor whiteColor];
    titleStyle.fontName = @"Helvetica-Bold";
    titleStyle.fontSize = 16.0f;
    // 4 - Set up title
    NSString *title = @"";
    self.propertyListBarChart.title = title;
    self.propertyListBarChart.titleTextStyle = titleStyle;
    self.propertyListBarChart.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
    self.propertyListBarChart.titleDisplacement = CGPointMake(0.0f, -16.0f);

    // 5 - Set up plot space
    //NSNumber *topValue = (NSNumber *)[[[DataStore sharedStore] valuesForBars] objectAtIndex:0];

    //X-Axis will contain Amount and Y-Axis acutally there is nothing to plot.

    CGFloat xMin = 0.0f;
    CGFloat xMax = 15000000.0f;
    CGFloat yMin = 0.0f;
    CGFloat yMax = 7.0f;  // should determine dynamically based on max price
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.propertyListBarChart.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xMin) length:CPTDecimalFromDouble(xMax)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(yMin) length:CPTDecimalFromDouble(yMax)];
}

- (void)configurePropertyPlot
{
    // 1 - Set up Bar plot.
    self.propertyBarPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor greenColor] horizontalBars:NO];
    self.propertyBarPlot.identifier = kTopWellsBarPlot;
    [self.propertyBarPlot setBarsAreHorizontal:YES];

    // 3 - Add plots to graph
    CPTGraph *graph = self.propertyListBarChart;
    self.propertyBarPlot.dataSource = (id)self;
    self.propertyBarPlot.barWidth = CPTDecimalFromDouble(4.0f);
    //self.propertyBarPlot.lineStyle = barLineStyle;
    [self.propertyListBarChart addPlot:self.propertyBarPlot toPlotSpace:graph.defaultPlotSpace];
}

-(void)configurePropertyPlotAxes
{
    // 1 - Configure styles
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor whiteColor];
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 12.0f;

    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 3.0f;
    //axisLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:1];
    axisLineStyle.lineColor = [CPTColor blueColor];

    // 2 - Get the graph's axis set
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.topWellsGraphHostingView.hostedGraph.axisSet;

    // 3 - Configure the x-axis
    axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
    axisSet.xAxis.title = @"Amount";
    axisSet.xAxis.titleTextStyle = axisTitleStyle;
    axisSet.xAxis.titleOffset = 10.0f;
    axisSet.xAxis.axisLineStyle = axisLineStyle;

    // 4 - Configure the y-axis
    axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
    axisSet.yAxis.title = @"Top 5";
    axisSet.yAxis.titleTextStyle = axisTitleStyle;
    axisSet.yAxis.titleOffset = 5.0f;
    axisSet.yAxis.axisLineStyle = axisLineStyle;
}

#pragma mark -
#pragma mark - CPTPlotDataSource methods
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    NSLog(@"Names Array: %@",[[DataStore sharedStore] namesForBars]);
    NSLog(@"Values Array: %@",[[DataStore sharedStore] valuesForBars]);
    NSLog(@"Formatted Values Array: %@",[[DataStore sharedStore] formattedValuesForBars]);

    //return [[[DataStore sharedStore] namesForBars] count];
    return [[self plotDataValues] count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    NSNumber *num = nil;

    if ((fieldEnum == 1 || fieldEnum == 0 || fieldEnum == 2) && (index < [[[DataStore sharedStore] valuesForBars] count]))
    {
        float value = 0.00f;

        if ([plot isKindOfClass:[CPTBarPlot class]])
        {
            NSString *plotIdentifier = (NSString *)plot.identifier;

            if ([plotIdentifier isEqualToString:kTopWellsBarPlot])
            {
                value = [[[self plotDataValues] objectAtIndex:index] doubleValue];
            }
        }

        num = [NSNumber numberWithFloat:value];
    }
    return num;
}

有人可以告诉我实现中是否存在问题。托管 View 已添加到 Xib。

最佳答案

  • 数据源应使用预定义的常量( CPTBarPlotFieldBarLocationCPTBarPlotFieldBarTipCPTBarPlotFieldBarBase )而不是使用“魔术”数字来检查绘图字段。
  • 大多数条形图至少需要每个条的位置和提示值。确保数据源为每个字段返回正确的值。
  • length绘图范围的大小应为 max - min ,而不是 max .在这种情况下,这无关紧要,因为 min值为零,但在其他情况下会很重要。

  • Core Plot 示例应用程序中有许多条形图示例。 Plot Gallery 应用程序是一个很好的起点。

    关于iphone - 核心图条形图 - 条不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906850/

    相关文章:

    iphone - iOS 默认文档文件

    ios - 无法登录 - 已在此 iPhone 上激活最大数量的免费帐户

    iphone - RMMapView setCenterCoordinate 函数不正常

    ios - 在文本字段中输入时更新/运行代码 [Swift/iOS]

    ios - 无法同时满足约束 - 没有适当的约束

    iphone - 如何从 UIWebView 获取最后的 HTTP 状态代码?

    iphone - 自定义 UITableViewCell

    ios - RK对象管理器 : Disable Certificate Validation

    ios - Objective-C警告,使用未声明的标识符“new”

    iphone - 添加 TableView subview 会使应用程序崩溃