Ios CorePlot条形图未显示

Ios CorePlot条形图未显示,ios,objective-c,core-plot,Ios,Objective C,Core Plot,我在我的项目中有一个CorePlot的实现,虽然我在Xcode中没有相关的错误或警告,但是我没有成功地将该图显示在图表中。轴和图表显示良好,但图形中没有条形图 任何帮助或建议都将不胜感激 头文件 主文件 绘图范围作为起始位置和长度给出。在您的情况下,将起始位置设为最小值,长度设为最大-最小值。现在可以,因为最小值为零,但如果更改最小值,长度将是错误的 -numberForPlot:field:recordIndex:datasource方法必须检查字段参数并为字段返回正确的值。返回CPTBarP

我在我的项目中有一个CorePlot的实现,虽然我在Xcode中没有相关的错误或警告,但是我没有成功地将该图显示在图表中。轴和图表显示良好,但图形中没有条形图

任何帮助或建议都将不胜感激

头文件

主文件


绘图范围作为起始位置和长度给出。在您的情况下,将起始位置设为最小值,长度设为最大-最小值。现在可以,因为最小值为零,但如果更改最小值,长度将是错误的


-numberForPlot:field:recordIndex:datasource方法必须检查字段参数并为字段返回正确的值。返回CPTBarPlotFieldBarLocation字段的索引参数值。从数据集数组返回CPTBarPlotFieldBarTip字段的值。

这是一个非答案。我感谢您的输入,但这看起来像文档,并没有回答问题。照目前的情况,起始位置和长度应该是有效的,您确认了这一点。此外,我还在numberForPlot方法中返回有效数据。一个合适的答案会指向一个特定的问题区域。这个答案告诉你如何修复你的数据源。我只是没有包括一个代码示例。在这两种情况下,开发人员花在解决核心情节问题上的时间都是令人讨厌的。此时,任何CorePlot问题的正确答案都应该是“不要使用CorePlot”。
#import <UIKit/UIKit.h>
#import "PresentedViewControllerDelegate.h"
#import <CorePlot/ios/CorePlot.h>

@interface DeviceDetailModal : UIViewController     <PresentedViewControllerDelegate, UIWebViewDelegate, UITableViewDataSource, UITableViewDelegate, CPTPlotDataSource,CPTBarPlotDelegate>{
CPTGraph *graph;

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, weak) id <PresentedViewControllerDelegate> delegate;

@property (strong,nonatomic) NSArray *dataSet;

@property (nonatomic, strong) CPTBarPlot *aaplPlot;
@property (nonatomic, strong) CPTPlotSpaceAnnotation *priceAnnotation;

@end
#import <Foundation/Foundation.h>
#import "DeviceDetailModal.h"
#import "DetailCell.h"
#import <CorePlot/ios/CorePlot.h>
#import "Constants.h"

@implementation DeviceDetailModal

NSString *  const CPDTickerSymbolAAPL = @"AAPL";

CGFloat const CPDBarWidth = 0.25f;
CGFloat const CPDBarInitialX = 0.25f;
@synthesize aaplPlot = aaplPlot_;

-(void)viewDidLoad{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor colorWithRed:0.03 green:0.06 blue:0.10 alpha:1.0]];

    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(closeView)];
    self.navigationItem.leftBarButtonItem = closeButton;

    [[UIBarButtonItem appearance] setTintColor:[UIColor lightGrayColor]];

    [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];

    self.tableView.layer.cornerRadius = 10.0;
    self.tableView.layer.borderWidth = 0.7;
    self.tableView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.tableView.opaque = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.backgroundView = nil;

    self.dataSet = [[NSArray alloc] init];

    self.dataSet = [NSArray arrayWithObjects:
              [NSDecimalNumber numberWithFloat:571.70],
              [NSDecimalNumber numberWithFloat:560.28],
              [NSDecimalNumber numberWithFloat:610.00],
              [NSDecimalNumber numberWithFloat:607.70],
              [NSDecimalNumber numberWithFloat:603.00],
              nil];

    [self configureGraph];

    [graph reloadData];
}

-(void)configureGraph {

    CGRect hostingRect = CGRectMake(20, 350, 335, 310);

    CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:hostingRect];

    [self.view addSubview:hostingView];

    [hostingView setBackgroundColor:[UIColor colorWithRed:0.03 green:0.06 blue:0.10 alpha:1.0]];

    hostingView.layer.cornerRadius = 10.0;
    hostingView.layer.borderWidth = 0.7;
    hostingView.layer.borderColor = [UIColor whiteColor].CGColor;

    graph = [[CPTXYGraph alloc] initWithFrame:hostingView.bounds];

    hostingView.hostedGraph = graph;

    [[graph defaultPlotSpace] setAllowsUserInteraction:TRUE];

    // 1 - Create the graph
    graph.plotAreaFrame.masksToBorder = NO;
    // 2 - Configure the graph
    //[graph applyTheme:[CPTTheme themeNamed:kCPTPlainBlackTheme]];
    graph.paddingBottom = 30.0f;
    graph.paddingLeft  = 30.0f;
    graph.paddingTop    = -1.0f;
    graph.paddingRight  = -5.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 = @"Portfolio Prices: April 23 - 27, 2012";
    graph.title = title;
    graph.titleTextStyle = titleStyle;
    graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
    graph.titleDisplacement = CGPointMake(0.0f, -16.0f);
    // 5 - Set up plot space
    CGFloat xMin = 0.0f;
    CGFloat xMax = 7;
    CGFloat yMin = 0.0f;
    CGFloat yMax = 800.0f;  // should determine dynamically based on max price

    NSNumber *xAxisStart = [NSNumber numberWithFloat:xMin];
    NSNumber *xAxisLength = [NSNumber numberWithDouble:xMax];
    NSNumber *yAxisStart = [NSNumber numberWithFloat:yMin];
    NSNumber *yAxisLength = [NSNumber numberWithDouble:yMax];

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:xAxisStart length:xAxisLength];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:yAxisStart length:yAxisLength];

    // 1 - Set up the three plots
    self.aaplPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO];
    self.aaplPlot.identifier = CPDTickerSymbolAAPL;
    // 2 - Set up line style
    CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init];
    barLineStyle.lineColor = [CPTColor lightGrayColor];
    barLineStyle.lineWidth = 0.5;
    // 3 - Add plots to graph
    graph = hostingView.hostedGraph;
    CGFloat barX = CPDBarInitialX;
    NSArray *plots = [NSArray arrayWithObjects:self.aaplPlot, nil];
    for (CPTBarPlot *plot in plots) {
        plot.dataSource = self;
        plot.delegate = self;
        plot.barWidth = [NSNumber numberWithFloat:CPDBarWidth];
        plot.barOffset = [NSNumber numberWithFloat:barX];
        plot.lineStyle = barLineStyle;
        [graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
        barX += CPDBarWidth;
    }

    // 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 = 2.0f;
    axisLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:1];
    // 2 - Get the graph's axis set
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) hostingView.hostedGraph.axisSet;
    // 3 - Configure the x-axis
    axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
    axisSet.xAxis.title = @"Days of Week (Mon - Fri)";
    axisSet.xAxis.titleTextStyle = axisTitleStyle;
    axisSet.xAxis.titleOffset = 10.0f;
    axisSet.xAxis.axisLineStyle = axisLineStyle;
    // 4 - Configure the y-axis
    axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
    axisSet.yAxis.title = @"Price";
    axisSet.yAxis.titleTextStyle = axisTitleStyle;
    axisSet.yAxis.titleOffset = 5.0f;      
    axisSet.yAxis.axisLineStyle = axisLineStyle;

}

#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return [self.dataSet count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {

    NSLog(@"DATA: %@",[self.dataSet objectAtIndex:index]);

    return [self.dataSet objectAtIndex:index];
}

#pragma mark - CPTBarPlotDelegate methods
-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index {
    // 1 - Is the plot hidden?
    if (plot.isHidden == YES) {
        return;
    }
    // 2 - Create style, if necessary
    static CPTMutableTextStyle *style = nil;
    if (!style) {
        style = [CPTMutableTextStyle textStyle];
        style.color= [CPTColor yellowColor];
        style.fontSize = 16.0f;
        style.fontName = @"Helvetica-Bold";
    }
    // 3 - Create annotation, if necessary
    NSNumber *price = [self numberForPlot:plot field:CPTBarPlotFieldBarTip recordIndex:index];
    if (!self.priceAnnotation) {
        NSNumber *x = [NSNumber numberWithInt:0];
        NSNumber *y = [NSNumber numberWithInt:0];
        NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
        self.priceAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:plot.plotSpace anchorPlotPoint:anchorPoint];
    }
    // 4 - Create number formatter, if needed
    static NSNumberFormatter *formatter = nil;
    if (!formatter) {
        formatter = [[NSNumberFormatter alloc] init];
        [formatter setMaximumFractionDigits:2];
    }
    // 5 - Create text layer for annotation
    NSString *priceValue = [formatter stringFromNumber:price];
    CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:priceValue style:style];
    self.priceAnnotation.contentLayer = textLayer;
    // 6 - Get plot index based on identifier
    NSInteger plotIndex = 0;
    if ([plot.identifier isEqual:CPDTickerSymbolAAPL] == YES) {
        plotIndex = 0;
    }
    // 7 - Get the anchor point for annotation
    CGFloat x = index + CPDBarInitialX + (plotIndex * CPDBarWidth);
    NSNumber *anchorX = [NSNumber numberWithFloat:x];
    CGFloat y = [price floatValue] + 40.0f;
    NSNumber *anchorY = [NSNumber numberWithFloat:y];
    self.priceAnnotation.anchorPlotPoint = [NSArray arrayWithObjects:anchorX, anchorY, nil]; 
    // 8 - Add the annotation 
    [plot.graph.plotAreaFrame.plotArea addAnnotation:self.priceAnnotation]; 
}


    @end