Archive

Posts Tagged ‘Google Chart API’

Using Google Chart in Objective-C

April 14, 2012 1 comment

Google chart is a very nice tool where you can generate some very nice figures for your data. By calling their API, you could simply embed it into your web page or download the image for whatever you use. At their website, google includes some most commonly used ones, but some are missing such as meter bar. You could probably find others that fit your goal by just ‘googling’ 🙂

Here’s a code snip for how to use it in Objective-C. I use chf=bg,s,65432100 to make it a clear/transparent background.


- (UIImage *) getGoogleChartImage {

NSString* url=@"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World&chf=bg,s,65432100";
NSString *myurl=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

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

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

NSLog(@"%@",error);
NSLog(@"%@",response);
NSLog(@"%@",googleImageData);

UIImage *googleImage = [[UIImage alloc] initWithData:googleImageData];
NSLog(@"Retrived Google Image Width: %f", googleImage.size.width);

return googleImage;
}

Advertisements