核心图条形图绑定到NSArrayController

时间:2012-01-29 03:27:57

标签: cocoa core-plot nsarraycontroller

我试图设计一个条形图,它将从NSArrayController获取其数据,NSArrayController本身也绑定到另一个数据源。

这里有什么正确的方法?

  • 我的图形控制器是NSArrayController的子类
  • 在控制器的awakeFromNib中,我绘制图形并设置绑定:

- (void)awakeFromNib {

[super awakeFromNib];

// Add plot space for horizontal bar charts
CPTXYPlotSpace *barPlotSpace = [[CPTXYPlotSpace alloc] init];
barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-20.0f) length:CPTDecimalFromFloat(200.0f)];
barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-7.0f) length:CPTDecimalFromFloat(15.0f)];
[graph addPlotSpace:barPlotSpace];
//[barPlotSpace release];

// First bar plot
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
CPTBarPlot *barPlot = [[CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:YES] retain];
barPlot.baseValue = CPTDecimalFromFloat(20.0f);
barPlot.dataSource = self;
barPlot.barOffset = CPTDecimalFromFloat(-0.25f);
barPlot.identifier = barPlot1;
barPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(7.0)];
barPlot.labelTextStyle = whiteTextStyle;
[graph addPlot:barPlot toPlotSpace:barPlotSpace];

/*
// Second bar plot
barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:YES];
barPlot.dataSource = self;
barPlot.baseValue = CPTDecimalFromFloat(20.0f);
barPlot.barOffset = CPTDecimalFromFloat(0.25f);
barPlot.cornerRadius = 2.0;
barPlot.identifier = barPlot2;
barPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(7.0)];
barPlot.delegate = self;
[graph addPlot:barPlot toPlotSpace:barPlotSpace];  
 */

[barPlot bind:CPTBarPlotBindingBarLocations
                toObject:self withKeyPath:@"arrangedObjects" options:nil];  }

这是正确的方法吗?

我的arrangeObjects是一组托管对象,它们具有我需要绘制的名称和时间字段。

谢谢!

1 个答案:

答案 0 :(得分:0)

使用绑定时,请勿设置数据源。不要忘记将字段绑定到CPTBarPlotBindingBarTips

CPTTestApp示例应用程序演示了如何将散点图绑定到数组控制器。除了绑定常量具有不同的名称外,条形图的过程是相同的。