2015-11-08 51 views
0

我在我的项目中有一个CorePlot的实现,虽然我没有相关的错误或Xcode中的警告,但未能成功将图显示在图中。轴和图显示正常,但图中没有条。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 

回答

-1

剧情给出范围时作为起始位置和长度。在你的情况下,使起始位置的值为min,长度为max - min。现在可以,因为min为零,但如果您更改了min值,则长度将会错误。

-numberForPlot:field:recordIndex:数据源方法必须检查字段参数并返回字段的正确值。返回CPTBarPlotFieldBarLocation字段的index参数值。从dataSet阵列返回CPTBarPlotFieldBarTip字段的值。

+0

这是一个无法回答的问题。我很欣赏这个输入,但是这个内容类似于文档,并没有回答这个问题。现在,起始位置和长度应该起作用(您确认了这一点)。此外,我在numberForPlot方法中返回有效数据。一个适当的答案会指向一个特定的麻烦区域。 – arcade16

+0

答案告诉你如何修复你的数据源。我只是没有包含代码示例。 –

+0

无论在哪种情况下,解决核心阴谋问题的开发时间都是猥亵的。此时,任何CorePlot问题的正确答案都应该是“不要使用CorePlot”。 – arcade16