gpt4 book ai didi

ios - 核心图 - 带平均水平线的条形图

转载 作者:可可西里 更新时间:2023-11-01 03:50:01 24 4
gpt4 key购买 nike

有没有办法使用核心绘图框架将平均水平线(或任何单线)添加到条形图?

谢谢。

最佳答案

一种方法是使用 CPTScatterPlot:

在初始化条形图(或任何实际数据图)并将其添加到图表后,将以下行添加到代码中。

// Before following code, initialize your data, actual data plot and add plot to graph

CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
CPTMutableLineStyle * lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 3.f;
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:3.0f], [NSNumber numberWithFloat:3.0f], nil];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.identifier = @"horizontalLineForAverage";
dataSourceLinePlot.dataSource = self;
[barChart addPlot:dataSourceLinePlot toPlotSpace:plotSpace];

然后添加数据源方法,在我的例子中,我将上面代码中的数据源设置为 self,所以我在同一个文件中定义数据源方法:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
// Note this method will return number of records for both my actual plot, and for scattered plot which is used to draw horizontal average line. For latter, this will decide the horizontal length of your line
return [myDataArray count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSDecimalNumber *num = nil;

// If method is called to fetch data about drawing horizontal average line, then return your generated average value.
if( plot.identifier==@"horizontalLineForAverage")
{
if(fieldEnum == CPTScatterPlotFieldX )
{
// this line will remain as it is
num =(NSDecimalNumber *)[NSDecimalNumber numberWithDouble:index];
}
else
{
num = (NSDecimalNumber *) myDataAverageValue;// Here you generate average value for location of horizontal line. You should edit this line only;
}
}
// handle other cases and return data for other plots
return num;
}

关于ios - 核心图 - 带平均水平线的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com