我想知道我需要如何准备好数据,这样才能为核心图做好准备。
上的x轴
我的x轴有366点(一年中的每一天).现在我有一本像这样的字典
2009 (year) = {
151 (dayofyear) = 5 (value);
192 = 25;
206 = 5;
234 = 20;
235 = 20;
255 = 20;
262 = 10;
276 = 10;
290 = 10;
298 = 7;
310 = 1;
338 = 3;
354 = 5;
362 = 5;
};
2010 = {
114 = 7;
119 = 3;
144 = 7;
17 = 5;
187 = 10;
198 = 7;
205 = 10;
212 = 10;
213 = 20;
215 = 5;
247 = 10;
248 = 10;
256 = 10;
262 = 7;
264 = 10;
277 = 10;
282 = 3;
284 = 7;
47 = 5;
75 = 7;
99 = 7;
};
2011 = {
260 = 10;
};我想核心情节需要一个数组,不是吗?你怎么把这个包装成最有效率的?
发布于 2011-09-22 06:31:35
实际上,我已经改变了结构。以年份为键的字典,以及包含日期和值的每个点的数组。
2009 = (
(354,5),
(338,3),
(234,20),
(298,7),
(192,25)
)这样的实现是非常容易的。
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
return [[[self.data objectForKey:plot.identifier] objectAtIndex: index] objectAtIndex: fieldEnum];
}发布于 2011-09-22 00:50:16
数据结构的选择完全取决于您。字典中已经有数据了,所以保留它吧。在数据源中实现以下方法:
-(NSNumber *)numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index;假设您每年都有一个单独的地块,那么使用plot参数从数据字典中选择合适的年份字典。使用fieldEnum参数确定绘图是要求x值还是y值,并使用index参数来决定列表中要返回的值。
例如(假设字典中的所有值都存储为NSNumber对象,并且使用散点图):
-(NSNumber *)numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index
{
NSDictionary *year = // retrieve the year dictionary based on the plot parameter
NSDictionary *yearData = [year objectAtIndex:index];
NSNumber *num = nil;
switch ( fieldEnum ) {
case CPTScatterPlotFieldX:
num = [yearData objectForKey:@"dayofyear"];
break;
case CPTScatterPlotFieldY:
num = [yearData objectForKey:@"value"];
break;
default:
break;
}
return num;
}https://stackoverflow.com/questions/7499048
复制相似问题