0

我有 4 个文件,BarPlot.h 和 BarPlot.m,BarPlotViewController.h 和 BarPlotViewController.m,我的(对你们来说可能非常直接)是我无法让 BarplotViewController 显示我的图表。

我已经确保在情节提要中调用了正确的视图控制器。

我已经多次查看代码,但我无法弄清楚为什么当我在模拟器中运行应用程序时视图没有显示..

我已包含以下文件。

非常感谢任何和所有帮助。

BarPlotViewController.h

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "BarPlot.h"

@interface BarPlotViewController : UIViewController {
    CPTGraphHostingView *_graphHostingView;
}

@property (nonatomic, retain) BarPlot *barPlot;

@end

BarPlotViewController.m

#import "BarPlotViewController.h"

@implementation BarPlotViewController
@synthesize barPlot = _barPlot;


- (void)viewWillAppear:(BOOL)animated
{   
    self.barPlot = [[BarPlot alloc] initWithHostingView:_graphHostingView];
    [self.barPlot generateBarPlot];
}

@end

条形图.h

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@interface BarPlot : UIViewController <CPTBarPlotDataSource, CPTBarPlotDelegate> {
    CPTGraphHostingView *_hostingView;  
CPTXYGraph *_graph;
}

@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, retain) CPTGraphHostingView *hostingView;
@property (nonatomic, retain) CPTXYGraph *graph;


// Methods to create this object and attach it to it's hosting view.
-(id)initWithHostingView:(CPTGraphHostingView *)hostingView;


// Generate the bar plot
- (void) generateBarPlot;

@end

条形图.m

#import "BarPlot.h"

@implementation BarPlot

#define BAR_POSITION @"POSITION"
#define BAR_HEIGHT @"HEIGHT"
#define COLOR @"COLOR"
#define CATEGORY @"CATEGORY"

#define AXIS_START 0
#define AXIS_END 50

@synthesize data;
@synthesize hostingView = _hostingView;
@synthesize graph = _graph;


// Initialise the bar plot in the provided hosting
-(id)initWithHostingView:(CPTGraphHostingView *)hostingView
{
    self = [super init];

if ( self != nil ) {
    self.hostingView = hostingView;
    self.graph = nil;
}


NSLog(@"Bar Plot: I'm in the initWithHostingView (in the barplot.m file)");

return self;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

    self.data = [NSMutableArray array];

    int bar_heights[] = {20,30};


    UIColor *colors[] = {
        [UIColor redColor],
        [UIColor blueColor],
        [UIColor orangeColor],
        [UIColor purpleColor]};


    NSString *categories[] = {@"", @""};



    for (int i = 0; i < 2 ; i++){
        double position = i*20; //Bars will be 10 pts away from each other
        double height = bar_heights[i];

        NSDictionary *bar = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithDouble:position],BAR_POSITION,
                             [NSNumber numberWithDouble:height],BAR_HEIGHT,
                             colors[i],COLOR,
                             categories[i],CATEGORY,
                             nil];
        [self.data addObject:bar];

    }
    [self generateBarPlot];
}
return self;
}


- (void)generateBarPlot
{
//Create host view
/*self.hostingView = [[CPTGraphHostingView alloc] 
                    initWithFrame:[[UIScreen mainScreen]bounds]];
[self.view addSubview:self.hostingView];



//Create graph and set it as host view's graph
self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostingView.bounds];
[self.hostingView setHostedGraph:self.graph];*/



// Create a graph object which we will use to host just one scatter plot.
CGRect frame = [self.hostingView bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:frame];

// Tie the graph we've created with the hosting view.
self.hostingView.hostedGraph = self.graph;



//set graph padding and theme
self.graph.plotAreaFrame.paddingTop = 20.0f;
self.graph.plotAreaFrame.paddingRight = 20.0f;
self.graph.plotAreaFrame.paddingBottom = 70.0f;
self.graph.plotAreaFrame.paddingLeft = 70.0f;
[self.graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];



// Gets rid of decimal on years
NSNumberFormatter *labelFormatter = [[NSNumberFormatter alloc] init];
labelFormatter.maximumFractionDigits = 0;



//set axes ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:
                    CPTDecimalFromFloat(AXIS_START)
                                                length:CPTDecimalFromFloat((AXIS_END - AXIS_START))];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:
                    CPTDecimalFromFloat(AXIS_START)
                                                length:CPTDecimalFromFloat((AXIS_END - AXIS_START))];




CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
//set axes' title, labels and their text styles
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 14;
textStyle.color = [CPTColor whiteColor];



axisSet.xAxis.title = @"A";
axisSet.yAxis.title = @"B";
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.yAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 30.0f;
axisSet.yAxis.titleOffset = 40.0f;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.yAxis.labelOffset = 3.0f;



//set axes' line styles and interval ticks
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 3.0f;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(5.0f);
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(5.0f);
axisSet.xAxis.majorTickLength = 10.0f;
axisSet.yAxis.majorTickLength = 10.0f;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.xAxis.minorTickLength = .0f;
axisSet.yAxis.minorTickLength = .0f;
axisSet.xAxis.labelFormatter = labelFormatter;
axisSet.yAxis.labelFormatter = labelFormatter;



// Create bar plot and add it to the graph
CPTBarPlot *plot = [[CPTBarPlot alloc] init] ;
plot.delegate = self;
plot.dataSource = 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];
}



-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
if ( [plot.identifier isEqual:@"chocoplot"] ) 
{
    return [self.data count];
}

return 0;
}



-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
if ( [plot.identifier isEqual:@"chocoplot"] )
{

    NSDictionary *bar = [self.data objectAtIndex:index];


    if( fieldEnum == 2 ) {
        return [bar valueForKey:BAR_POSITION];
        //return [NSNumber numberWithDouble:20];
    }
    else if( fieldEnum == 3 ) {
        return [bar valueForKey:BAR_HEIGHT];
        //return [NSNumber numberWithDouble:20];
    }

}
NSLog(@"numberForPlot return before");
return [NSNumber numberWithDouble:15];
}

@end
4

1 回答 1

0

嗯,我注意到的第一件事是你从不调用 addSubview。那是实际上“添加视图”的代码,所以我将从那里开始。另外,请参阅此问题,第二个答案看起来可能会有所帮助:Adding CPTGraphHostingView as a subview

于 2012-07-24T12:22:52.353 回答