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

时间:2012-01-03 07:42:58

标签: iphone charts core-plot scatter-plot

我从谷歌下载了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;
    }

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

2 个答案:

答案 0 :(得分:1)

我创建了一个方法,如 - (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;
 }

当我获得新的实时值时,我会重新加载此方法。

感谢。

答案 1 :(得分:0)

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