Core-Plot 如何打包数据

发布于 2024-12-05 14:17:36 字数 933 浏览 0 评论 0原文

我想知道我需要如何准备数据以便为核心图做好准备。

  • 每年一行
  • x 轴上的 dayOfYear
  • y 轴上的值

我的 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;
    };

我认为核心图需要一个数组,不是吗?您将如何打包才能最有效?

I was wondering how I need to prepare the data so it is ready for core-plot.

  • one line per year
  • dayOfYear in the x-axis
  • value on the y-axis

My x-axis has 366 points (Each day of the year). At the moment I have a dictionary that looks like this

 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;
    };

I think core-plot needs an array doesn't it? How would you pack this to be the most efficient?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角卖回忆 2024-12-12 14:17:36

数据结构的选择完全取决于您。您已经将数据存储在字典中,因此请保留它。在数据源中实现以下方法:

-(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;
}

The choice of data structure is completely up to you. You already have the data in a dictionary, so keep that. Implement the following method in your datasource:

-(NSNumber *)numberForPlot:(CPTPlot *)plot
                     field:(NSUInteger)fieldEnum
               recordIndex:(NSUInteger)index;

Assuming you have a separate plot for each year, use the plot parameter to select the appropriate year dictionary from the data dictionary. Use the fieldEnum parameter to determine whether the plot is asking for the x or y value and the index parameter to decide which value in the list to return.

For instance (assuming all values in the dictionaries are stored as NSNumber objects and you're using a scatter plot):

-(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;
}
独守阴晴ぅ圆缺 2024-12-12 14:17:36

我实际上已经将结构更改为这样。
以年份为键的字典,每个点都有一个包含年份和值的数组。

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];
}

I have actually changed the structure to this.
A dictionary with the year as the key and an array for each point containing the dayofyear and the value.

2009 =     (
                (354,5),
                (338,3),
                (234,20),
                (298,7),
                (192,25)
)

That way the implementation was very easy

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
    return [[[self.data objectForKey:plot.identifier] objectAtIndex: index] objectAtIndex: fieldEnum];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文