【问题标题】:core-plot bar chart bound to a NSArrayController绑定到 NSArrayController 的 core-plot 条形图
【发布时间】:2012-01-29 03:27:56
【问题描述】:

我正在尝试设计一个条形图,该条形图将从本身绑定到另一个数据源的 NSArrayController 获取数据。

这里的正确方法是什么?

  • 我的 Graph 控制器是 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];  }

这是正确的方法吗?

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

谢谢!

【问题讨论】:

    标签: cocoa core-plot nsarraycontroller


    【解决方案1】:

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

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

    【讨论】:

    • 我查看了散点图的示例应用程序,但是,我不确定我应该如何将安排的对象(来自 NSArrayController)绑定到图表?我的arrangedObjects 应该有哪些字段? (x,y?) 以及图侧的绑定常数是什么?
    • 绑定常量是CPTBarPlotBindingBarLocationsCPTBarPlotBindingBarTips。您可以控制arrangedObjects 中的名称。您可以通过在 Interface Builder 中选择“XY 值控制器”并打开右侧的 Attributes Inspector 窗格,在 CPTTestApp 中看到这一点。
    • 明白,但 CPTBarPlotBindingBarLocationsCPTBarPlotBindingBarTips 到底是什么意思?假设在我的 X 轴上我有很多公司,Y 轴是每家公司的员工人数。我的排列对象是一个字典数组,其中 .name = 公司名称 .employees = 员工人数。我将如何设置绑定?
    • 位置和小费值都必须是数字——目前还没有对分类数据的内置支持。我的建议是只绑定小费值。然后,该图将使用位置的简单索引(0、1、2 等)。制作标签时,您可以使用轴委托将位置转换为公司名称。
    • @Eric 您说不要为绑定设置数据源,但当前的 CPTTestApp 正在使用 CPTScatterPlot 的数据源方法,该方法正在使用绑定。这些设置是获取绑定以使用巴特图所需的数据源吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多