【问题标题】:Core Plot Bar Chart Bars Wont Show Up核心情节条形图条不会显示
【发布时间】:2012-07-23 11:44:16
【问题描述】:

尽管花费了无数个小时检查代码行,并且在创建了一个功能齐全的散点图后,我还是无法让条形图显示在我的条形图上。

我正在使用 Core Plot,我确信我已经导入了 core plot 库并正确设置了 core plot 环境,因为散点图完全正常工作。

有人能帮我拔掉头发,或许能告诉我哪里出错了吗?我有一种感觉,因为我的 #define BAR_POSITION @"POSITION" 和 #define BAR_HEIGHT @"HEIGHT" 没有正确设置/调用。

下面是我用于头文件和主文件的代码。

感谢任何和所有的帮助。

头文件:

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

@interface BarPlotViewController : UIViewController 
<CPTBarPlotDataSource, CPTBarPlotDelegate> {

}

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

- (void) generateBarPlot;

@end

主文件:

#import "BarPlotViewController.h"

@implementation BarPlotViewController


#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 graph;
@synthesize hostingView;




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

    self.data = [NSMutableArray array];

    int bar_heights[] = {20,30,10,40};
    NSLog(@"After heights initialised.");


    int bar_positions[] = {10,20,30,40};
    NSLog(@"After position initialised.");


    UIColor *colors[] = {
        [UIColor redColor],
        [UIColor blueColor],
        [UIColor orangeColor],
        [UIColor purpleColor]};
    NSLog(@"After colors initialised.");


    NSString *categories[] = {@"A", @"B", @"C", @"D"};
    NSLog(@"After categories initialised.");


    for (int i = 0; i < 4 ; i++){
        double position = i*10; //Bars will be 10 pts away from each other
        double height = bar_heights[i];
        //double position = bar_positions[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];

        NSLog(@"Data entered into bar dictionary.");

        //NSString *positionStringValue = [NSString stringWithFormat:@"%.02f", position];
        //NSString *log = [@"Position " stringByAppendingFormat: positionStringValue];
        //NSLog( log );

        //NSString *colorsStringValue = [NSString stringWithFormat:@"%.02f", position];
        //NSString *log2 = [@"Colors " stringByAppendingFormat: colors[i]];
        //NSLog( log2 );

    }
    [self generateBarPlot];
    NSLog(@"After generate bar plot.");
}
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.graph = [[CPTXYGraph alloc] initWithHostingView:_graphHostingView];

[self.hostingView setHostedGraph:self.graph];
//self.scatterPlot = [[TUTSimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:data];
//[self.scatterPlot initialisePlot];



//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.dataSource = self;
plot.delegate = self;
plot.barWidth = [[NSDecimalNumber decimalNumberWithString:@"10.0"]
                 decimalValue];
plot.barOffset = [[NSDecimalNumber decimalNumberWithString:@"10.0"]
                  decimalValue];
plot.barCornerRadius = 5.0;


// Remove bar outlines
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor greenColor];
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 == CPTBarPlotFieldBarLocation) {
        //return [bar valueForKey:BAR_POSITION];
        NSLog(@"bar position before");
        return [NSNumber numberWithDouble:30];
        NSLog(@"bar position after");
    }
    else if(fieldEnum ==CPTBarPlotFieldBarTip) {
        //return [bar valueForKey:BAR_HEIGHT];
        NSLog(@"bar height before");
        return [NSNumber numberWithDouble:20];
        NSLog(@"bar height before");
    }
}
NSLog(@"numberForPlot return before");
return [NSNumber numberWithFloat:0];
NSLog(@"numberForPlot return after");
}


-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
if ( [plot.identifier isEqual: @"chocoplot"] )
{
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.fontName = @"Helvetica";
    textStyle.fontSize = 14;
    textStyle.color = [CPTColor whiteColor];

    NSDictionary *bar = [self.data objectAtIndex:index];
    CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", [bar valueForKey:@"CATEGORY"]]];
    label.textStyle =textStyle;

    return label;
}

CPTTextLayer *defaultLabel = [[CPTTextLayer alloc] initWithText:[NSString stringWithString:@"Label"]];
return defaultLabel;

}



-(CPTFill *)barFillForBarPlot:(CPTBarPlot *)barPlot
              recordIndex:(NSUInteger)index
{
if ( [barPlot.identifier isEqual:@"chocoplot"] )
{
    NSDictionary *bar = [self.data objectAtIndex:index];
    CPTGradient *gradient = [CPTGradient gradientWithBeginningColor:[CPTColor whiteColor]
                                                        endingColor:[bar valueForKey:@"COLOR"]
                                                  beginningPosition:0.0 endingPosition:0.3 ];
    [gradient setGradientType:CPTGradientTypeAxial];
    [gradient setAngle:320.0]; 

    CPTFill *fill = [CPTFill fillWithGradient:gradient];

    return fill;

}
return [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.0 green:1.0 blue:1.0 alpha:1.0]];

}

@end

【问题讨论】:

  • 你试过用一些值代替 BAR_POSITION 和 BAR_HEIGHT 吗?
  • 喜欢#define BAR_POSITION 10.0
  • 您好,感谢您的回复!我试过了,只要应用程序尝试在模拟器中加载,我就会得到一个 THREAD 1 EXC_BAD_ACCESS。
  • 好的,现在我知道问题是这个 if 语句失败了。 if( fieldEnum == CPTBarPlotFieldBarLocation ) 我认为这是旧代码,需要更新?
  • 如果您使用的是最新版本的 Core Plot,那么这是正确的枚举值。枚举在这里定义:code.google.com/p/core-plot/source/browse/framework/Source/…

标签: iphone objective-c graph core-plot bar-chart


【解决方案1】:

您确定您的 xrange 设置正确吗?我没有调试过代码,但是很容易被忽略。

【讨论】:

  • 我想通了! if( fieldEnum == CPTBarPlotFieldBarLocation ) 不正确。一旦我将其更改为 if(fieldEnum == 2),它就进入了 if 语句,一切都到位了。感谢您的帮助!
【解决方案2】:

您需要提供#define AXIS_START -xx 以便您可以显示

【讨论】:

  • 我想通了! if( fieldEnum == CPTBarPlotFieldBarLocation ) 不正确。一旦我将其更改为 if(fieldEnum == 2),它就进入了 if 语句,一切都到位了。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
相关资源
最近更新 更多