如何使用CorePlot9动态更改iPhone中ScatterPlot图表中的值?

发布于 2024-12-23 15:43:06 字数 2336 浏览 5 评论 0原文

我已经从 google 下载了 CorePlot_0.9 并导入到我的 iPhone 项目中。我想在 iPhone 应用程序中画线(ScatterPlot)。我刚刚查看并复制了 'CPTTestApp-iPhone' CPTTestAppScatterPlotController 类中的代码,并运行了该项目,它工作得很好。但是,我想更改图表值。我尽了最大努力,但是我找不到需要在哪里给出值以及散点图值如何在图表中显示。这是代码,

    This is in ViewDidLoad
    {
// Create a green plot area
    CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineWidth = 5.f;
    lineStyle.miterLimit = 1.0f;
    lineStyle.lineWidth = 3.0f;
    lineStyle.lineColor = [CPTColor blackColor];
    lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.0f], nil];
    dataSourceLinePlot.dataLineStyle = lineStyle;
    dataSourceLinePlot.identifier = @"Green Plot";
    dataSourceLinePlot.dataSource = self;

     // Add some initial data
            NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
            NSUInteger i;
            for ( i = 0; i < 20; i++ )
            {
                id x = [NSNumber numberWithFloat:0+i*0.5];//1+i*0.05
                //NSLog(@"Rand_Max : %d", RAND_MAX);//Rand_Max : 2147483647
                id y = [NSNumber numberWithFloat:3.2*rand()/(float)RAND_MAX+0.2];//1.2*rand()/(float)RAND_MAX + 1.2
                [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
            }
            dataForPlot = contentArray;
    }

    #pragma mark -
    #pragma mark Plot Data Source Methods

    -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
    {
        return [dataForPlot count];
    }

    -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
    {
        NSLog(@"NumberForPlot calling");
        NSNumber *num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y")];
        // Green plot gets shifted above the blue
        if ([(NSString *)plot.identifier isEqualToString:@"Green Plot"])
        {
            if ( fieldEnum == CPTScatterPlotFieldY ) 
                num = [NSNumber numberWithDouble:[num doubleValue] + 0.0];
        }
        return num;
    }

请帮助我在哪里需要给出值来更改图中显示的值?还给我建议如何动态更改图表的值?提前致谢。请帮我。

I have downloaded CorePlot_0.9 from google and imported in my iPhone project. I want to draw line(ScatterPlot) in iphone app. I just reviewed and copy the code from 'CPTTestApp-iPhone' CPTTestAppScatterPlotController class and run the project it working very good. But, i want to change the chart values. I tried my level best but, i can't find where i need to give the values and how the scatterplot values show in the graph. Here is the code,

    This is in ViewDidLoad
    {
// Create a green plot area
    CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineWidth = 5.f;
    lineStyle.miterLimit = 1.0f;
    lineStyle.lineWidth = 3.0f;
    lineStyle.lineColor = [CPTColor blackColor];
    lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.0f], nil];
    dataSourceLinePlot.dataLineStyle = lineStyle;
    dataSourceLinePlot.identifier = @"Green Plot";
    dataSourceLinePlot.dataSource = self;

     // Add some initial data
            NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
            NSUInteger i;
            for ( i = 0; i < 20; i++ )
            {
                id x = [NSNumber numberWithFloat:0+i*0.5];//1+i*0.05
                //NSLog(@"Rand_Max : %d", RAND_MAX);//Rand_Max : 2147483647
                id y = [NSNumber numberWithFloat:3.2*rand()/(float)RAND_MAX+0.2];//1.2*rand()/(float)RAND_MAX + 1.2
                [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
            }
            dataForPlot = contentArray;
    }

    #pragma mark -
    #pragma mark Plot Data Source Methods

    -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
    {
        return [dataForPlot count];
    }

    -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
    {
        NSLog(@"NumberForPlot calling");
        NSNumber *num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y")];
        // Green plot gets shifted above the blue
        if ([(NSString *)plot.identifier isEqualToString:@"Green Plot"])
        {
            if ( fieldEnum == CPTScatterPlotFieldY ) 
                num = [NSNumber numberWithDouble:[num doubleValue] + 0.0];
        }
        return num;
    }

Please help where i need to give the values to change the values appear in graph? And also give me suggestion how can i change the values dynamically for the graph? Thanks in advance. Please help me.

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

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

发布评论

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

评论(2

囚你心 2024-12-30 15:43:06

我创建了一个类似 -(void) drawScatterPlot; 的方法并编写了代码来在该方法中绘制散点图,如下所示,

{
// Create a green plot area
    CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineWidth = 5.f;
    lineStyle.miterLimit = 1.0f;
    lineStyle.lineWidth = 3.0f;
    lineStyle.lineColor = [CPTColor blackColor];
    lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.0f], nil];
    dataSourceLinePlot.dataLineStyle = lineStyle;
    dataSourceLinePlot.identifier = @"Green Plot";
    dataSourceLinePlot.dataSource = self;

     NSMutableArray *contentArray = [[NSMutableArray alloc] init]; 
   NSUInteger i;
   for ( i = 0; i < 4; i++ )
     {
    int xVal = [[customTickLocations objectAtIndex:i] intValue];
    id x = [NSNumber numberWithInt:xVal];

    int yVal = [[scatterValuesArray objectAtIndex:i] intValue];
    id y = [NSNumber numberWithInt:yVal];

    NSLog(@"X and Y in content Array Loop : %@ , %@", x , y);
    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
      }
            dataForPlot = contentArray;
 }

当我获取新的实时值时,我重新加载该方法。

谢谢。

I have created a method like -(void) drawScatterPlot; and wrote the code to draw the scatter plots in that method like this below,

{
// Create a green plot area
    CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineWidth = 5.f;
    lineStyle.miterLimit = 1.0f;
    lineStyle.lineWidth = 3.0f;
    lineStyle.lineColor = [CPTColor blackColor];
    lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.0f], nil];
    dataSourceLinePlot.dataLineStyle = lineStyle;
    dataSourceLinePlot.identifier = @"Green Plot";
    dataSourceLinePlot.dataSource = self;

     NSMutableArray *contentArray = [[NSMutableArray alloc] init]; 
   NSUInteger i;
   for ( i = 0; i < 4; i++ )
     {
    int xVal = [[customTickLocations objectAtIndex:i] intValue];
    id x = [NSNumber numberWithInt:xVal];

    int yVal = [[scatterValuesArray objectAtIndex:i] intValue];
    id y = [NSNumber numberWithInt:yVal];

    NSLog(@"X and Y in content Array Loop : %@ , %@", x , y);
    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
      }
            dataForPlot = contentArray;
 }

And i reload this method while when am getting new real time values.

Thanks.

内心荒芜 2024-12-30 15:43:06
arrDate = [[NSMutableArray alloc]init];
arrVar = [[NSMutableArray alloc]init];

NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
NSLog(@"%@",contentArray);

for (i =0; i<[appDelegate.arrayHistoryWeight count]; i++)
{

    objHistory = [appDelegate.arrayHistoryWeight objectAtIndex:i];

    substr = [NSString stringWithFormat:@"%@",objHistory.datetime];
    trim =  [substr  stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSLog(@"trim=%@==",trim);

    onlyDate = [trim substringWithRange:NSMakeRange(3, 2)];
    NSLog(@"date=%@==",onlyDate);

    date = [onlyDate doubleValue];
    var = [[objHistory.bmi stringByTrimmingCharactersInSet:nonDigits]doubleValue];


    NSLog(@"double date = %0.2f",date);
    NSLog(@"double var =%0.2f",var);

    [arrDate addObject:[NSString stringWithFormat:@"%0.0f",date]];
    [arrVar addObject:[NSString stringWithFormat:@"%0.2f",var]];


    id x = [arrDate objectAtIndex:i];
    id y = [arrVar objectAtIndex:i];

    NSLog(@"x=%@ and y=%@",x,y);

    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"x",y, @"y", nil]];

}

NSLog(@"arrOfXdata = %@",arrDate);
NSLog(@"arrOfYdata =%@",arrVar);
self.arrDataForPlot = contentArray;

NSLog(@"data = %@",arrDataForPlot);
[graph reloadData];
arrDate = [[NSMutableArray alloc]init];
arrVar = [[NSMutableArray alloc]init];

NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
NSLog(@"%@",contentArray);

for (i =0; i<[appDelegate.arrayHistoryWeight count]; i++)
{

    objHistory = [appDelegate.arrayHistoryWeight objectAtIndex:i];

    substr = [NSString stringWithFormat:@"%@",objHistory.datetime];
    trim =  [substr  stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSLog(@"trim=%@==",trim);

    onlyDate = [trim substringWithRange:NSMakeRange(3, 2)];
    NSLog(@"date=%@==",onlyDate);

    date = [onlyDate doubleValue];
    var = [[objHistory.bmi stringByTrimmingCharactersInSet:nonDigits]doubleValue];


    NSLog(@"double date = %0.2f",date);
    NSLog(@"double var =%0.2f",var);

    [arrDate addObject:[NSString stringWithFormat:@"%0.0f",date]];
    [arrVar addObject:[NSString stringWithFormat:@"%0.2f",var]];


    id x = [arrDate objectAtIndex:i];
    id y = [arrVar objectAtIndex:i];

    NSLog(@"x=%@ and y=%@",x,y);

    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"x",y, @"y", nil]];

}

NSLog(@"arrOfXdata = %@",arrDate);
NSLog(@"arrOfYdata =%@",arrVar);
self.arrDataForPlot = contentArray;

NSLog(@"data = %@",arrDataForPlot);
[graph reloadData];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文