2011-06-17 50 views
1

以及我得到这个代码:动态谷歌图表

NSLog(@"button pressed"); 
    NSString* [email protected]"http://chart.apis.google.com/chart?chf=bg,s,67676700&chs=300x225&cht=p&chd=s:Uf9a&chdl=30°|40°|50°|60°"; 
    NSString *theurl=[myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:theurl] cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:60.0];            

    NSURLResponse* response; 
    NSError* error; 
    NSData *imageData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; 
    NSLog(@"%@",error); 
    NSLog(@"%@",response); 
    NSLog(@"%@",imageData); 

    UIImage *myimage = [[UIImage alloc] initWithData:imageData]; 

    graphimage.image=myimage; 

真的没有什么特别的存在...... 但是当我请求URL:http://chart.apis.google.com/chart?chf=bg,s,67676700&chs=300x225&cht=p&chd=s:Uf9a&chdl=30°| 40°| 50°| 60°

它请求像度30/40/50 ...但我想要的价值观,而不是脱脂在那里,我从数据库中获得..,我怎么能做到这一点?

+0

嗨Emre,有没有一个地方有一些谷歌图表的目标c的例子,就像你在这里? – Luda

+0

Luda您可以查看https://developers.google.com/chart以获得一些示例!你可以在我的问题中使用上面的技巧来展示它 – Oblieapps

回答

2

您将需要从数据库中获取度数并将其存储在集合中。然后你将能够遍历集合并将它们添加到字符串中。

//Get Degrees from database, store them in an array of NSNumbers 
... 
//Pretend these are from a db 
NSArray *degreesFromDatabase = [NSArray arrayWithObjects: 
            [NSNumber numberWithInt:35], 
            [NSNumber numberWithInt:50], 
            [NSNumber numberWithInt:25], 
            [NSNumber numberWithInt:72], 
            nil]; 
    //URL with all static content 
    NSMutableString *myurl = [NSMutableString stringWithString:@"http://chart.apis.google.com/chart?chf=bg,s,67676700&chs=300x225&cht=p&chd=s:Uf9a&chdl="]; 

    int count = [degreesFromDatabase count]; 

    for(NSUInteger i = 0; i < count; ++i) 
    { 
     NSNumber *degree = [degreesFromDatabase objectAtIndex:i]; 
     [myurl appendFormat:@"%@%%C2%%B0", degree]; 
     if(i < count - 1) 
      [myurl appendString:@"|"]; 
    } 
+0

谢谢你的代码示例! – Oblieapps

2

我写了收到所有必要的参数的函数,创建一个谷歌的图表并将其转换为UIImage

enter image description here

这就是:

-(UIImage )produceGoogleChartImage:(NSString)title

      xAxis:(NSArray*)axisXLabels 
          yAxis:(NSArray*)axisYLabels 
         andData:(NSArray*)dataValues 
          color:(NSString*)lineColor 
        chartWidth:(NSNumber*)width 
        chartHight:(NSNumber*)hight 
        lagendLabel:(NSString*)legend 
         minScale:(NSNumber*)minScale 
         maxScale:(NSNumber*)maxScale{ 

NSMutableString *myurl = [NSMutableString stringWithString:@"http://chart.googleapis.com/chart?chxl=0:|"]; 


//axisXLabels 
int countAxisXLabels = [axisXLabels count]; 

for(NSUInteger i = 0; i < countAxisXLabels; ++i) 
{ 
    NSNumber *value = [axisXLabels objectAtIndex:i]; 
    [myurl appendFormat:@"%@", value]; 
    if(i < countAxisXLabels - 1) 
     [myurl appendString:@"|"]; 
} 

[myurl appendString:@"|1:|"]; 


//axisYLabels 
int countAxisYLabels = [axisYLabels count]; 
for(NSUInteger i = 0; i < countAxisYLabels; ++i) 
{ 
    NSNumber *value = [axisYLabels objectAtIndex:i]; 
    [myurl appendFormat:@"%@", value]; 
    if(i < countAxisYLabels - 1) 
     [myurl appendString:@"|"]; 
} 

[myurl appendString:@"&chxr=0,0,105|1,3.333,100&chxt=x,y&chs="]; 

//size 
[myurl appendFormat:@"%@x%@&cht=lc&chd=t:", width,hight]; 

//dataValues 
int countDataValues = [dataValues count]; 

for(NSUInteger i = 0; i < countDataValues; ++i) 
{ 
    NSNumber *value = [dataValues objectAtIndex:i]; 
    [myurl appendFormat:@"%@", value]; 
    if(i < countDataValues - 1) 
    [myurl appendString:@","]; 
} 

//legend 
[myurl appendFormat:@"&chdl=%@&chg=25,50&chls=2&",legend]; 

//color 
[myurl appendFormat:@"chm=o,%@,0,-2,8&chco=%@",lineColor,lineColor]; 

//title 
[myurl appendFormat:@"&chtt=+%@",title]; 

//scale 
[myurl appendFormat:@"&chds=%@,%@",minScale,maxScale]; 



NSString *theurl=[myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:theurl] 

cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSURLResponse* response; 
NSError* error; 
NSData *imageData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response 

error:&error]; NSLog(@"%@",error); NSLog(@"%@",response); NSLog(@"%@",imageData);

UIImage *myimage = [[UIImage alloc] initWithData:imageData]; 


return myimage; 

// Chart Wizard: https://developers.google.com/chart/image/docs/chart_wizard 
} 

这里是测试功能:

-(void)test{

NSString* title = @"Height History"; 
NSArray *axisXLabels = [NSArray arrayWithObjects: 
         @"Jan", 
         @"Feb", 
         @"Mar", 
         @"Jun", 
         @"Jul", 
         @"Aug", 
         nil]; 


NSArray *axisYLabels = [NSArray arrayWithObjects: 
         [NSNumber numberWithInt:0], 
         [NSNumber numberWithInt:50], 
         [NSNumber numberWithInt:150], 
         [NSNumber numberWithInt:200], 
         nil]; 

NSArray *dataValues = [NSArray arrayWithObjects: 
         [NSNumber numberWithInt:130], 
         [NSNumber numberWithInt:140], 
         [NSNumber numberWithInt:140], 
         [NSNumber numberWithInt:150], 
         [NSNumber numberWithInt:170], 
         [NSNumber numberWithInt:180], 
         nil]; 

NSString *lineColor = @"FF9900"; 

NSNumber *width = [NSNumber numberWithInt:300]; 

NSNumber *hight = [NSNumber numberWithInt:200]; 

NSNumber *minScale = [NSNumber numberWithInt:0]; 

NSNumber *maxScale = [NSNumber numberWithInt:200]; 

NSString *legendLabel = @"cm"; 

UIImage *myimage =[self produceGoogleChartImage:title xAxis:axisXLabels yAxis:axisYLabels andData:dataValues color:lineColor 

chartWidth:width chartHight:hight lagendLabel:legendLabel minScale:minScale maxScale:maxScale];

_chartImage.image=myimage; 

}

你应该得到的图像,像在的问题。

+0

很久以前我发现了我的问题的答案!但是这可以对其他人有用,thnx! – Oblieapps