【问题标题】:CorePlot: creating bands with mutable widths (changing based on data)CorePlot:创建宽度可变的波段(根据数据变化)
【发布时间】:2015-08-26 16:53:40
【问题描述】:

我想创建一个带有动态波段的 CorePlot 图(随着时间的推移而变化),有点像这里的这张丑陋的图画(请原谅我质量):

基本上,波段颜色应根据某些值/标准而改变。黄色带可以更宽更细,白色和绿色带也可以。 换句话说,这些波段需要具有动态宽度

这是我设置 y 图表带着色的当前方式,但是如果我动态更改它,它不起作用,因为它会全部更改(我只想在图表的特定区域动态更改带,而不是全部其中)。

知道如何实现这一目标吗?

这是与乐队相关的代码

CPTFill *whitebandFill = [CPTFill fillWithColor:[[CPTColor whiteColor] colorWithAlphaComponent:0.5]];
CPTFill *greenbandFill = [CPTFill fillWithColor:[[CPTColor greenColor] colorWithAlphaComponent:0.5]];
CPTFill *redbandFill = [CPTFill fillWithColor:[[CPTColor redColor] colorWithAlphaComponent:0.75]];

 [y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocationDecimal:CPTDecimalFromDouble(0.0) lengthDecimal:CPTDecimalFromDouble(500.0)] fill:whitebandFill]];
[y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocationDecimal:CPTDecimalFromDouble(500.0) lengthDecimal:CPTDecimalFromDouble(750.0)] fill:greenbandFill]];

 [y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocationDecimal:CPTDecimalFromDouble(750.0) lengthDecimal:CPTDecimalFromDouble(1024.0)] fill:redbandFill]];

这是我的完整当前源代码:

self.data = [[NSMutableArray alloc] initWithCapacity:100];
self.allData = [[NSMutableArray alloc] init];

// Create a CPTGraph object and add to hostView
self.graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostView.bounds];
self.graphHostView.hostedGraph = self.graph;

CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor blackColor];
titleStyle.fontName = @"Futura";
titleStyle.fontSize = 12.0f;

//////////
// Axes
// Label x axis with a fixed interval policy
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis *x          = axisSet.xAxis;
x.separateLayers              = NO;
x.title                       = @"X Axis";
x.titleTextStyle              = titleStyle;

colorWithAlphaComponent:CPTFloat(0.1)], [[CPTColor greenColor] colorWithAlphaComponent:CPTFloat(0.1)]]; x.delegate = 自我;

CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy        = CPTAxisLabelingPolicyAutomatic;
y.separateLayers        = YES;
y.majorTickLocations = [NSSet setWithObjects:
                        [NSDecimalNumber numberWithDouble:0],
                        [NSDecimalNumber numberWithDouble:300],
                        nil];

y.title                 = @"Y Axis";
y.titleTextStyle        = titleStyle;

colorWithAlphaComponent:CPTFloat(0.3)], [NSNull null]]; y.delegate = self;

CPTFill *whitebandFill = [CPTFill fillWithColor:[[CPTColor whiteColor] colorWithAlphaComponent:0.5]];
CPTFill *greenbandFill = [CPTFill fillWithColor:[[CPTColor greenColor] colorWithAlphaComponent:0.5]];
CPTFill *redbandFill = [CPTFill fillWithColor:[[CPTColor redColor] colorWithAlphaComponent:0.75]];

 [y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocationDecimal:CPTDecimalFromDouble(0.0) lengthDecimal:CPTDecimalFromDouble(500.0)] fill:whitebandFill]];
[y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocationDecimal:CPTDecimalFromDouble(500.0) lengthDecimal:CPTDecimalFromDouble(750.0)] fill:greenbandFill]];

 [y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocationDecimal:CPTDecimalFromDouble(750.0) lengthDecimal:CPTDecimalFromDouble(1024.0)] fill:redbandFill]];

self.graph.axisSet.axes = @[x, y];

【问题讨论】:

  • @ErickSkroch 你能看看这个问题吗?

标签: ios graph customization core-plot cptxygraph


【解决方案1】:

限制带总是在地块上以直线延伸。您可以使用其他绘图来绘制更复杂的形状。设置areaFill 在绘图线和areaBaseValue 之间绘制填充。

【讨论】:

  • 您好 Eric,我已尝试使用您的建议修改 CandleStick 示例,但无法创建超过 2 个色带。我需要能够用 3 种不同的颜色重新创建我的问题中的示例。关于如何实现这一目标的任何想法?一个散点图只有两个 areaFill 和两个 areaBase。当我们想要有两个分散的地块时会发生什么?我怎样才能确保它们不重叠?我需要一个情节在另一个情节开始时完成..基本上想象上面的图像有 5 个不同的色带..你将如何更新烛台以支持这一点
  • 您可以使用多个绘图作为背景。一个用于黄色部分,一个用于绿色部分。绘图区域填充将显示在它们之间。现在,没有简单的方法可以在两个地块之间填充(参见issue #99)。如果需要填充该空间,您可以尝试使用第三个散点图绘制闭合形状。如果您想绘制彼此相邻的绘图,只需在公共部分提供相同的数据值即可。
猜你喜欢
  • 2016-08-21
  • 2011-06-21
  • 2019-09-19
  • 2021-12-28
  • 2015-06-04
  • 2018-06-09
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
相关资源
最近更新 更多