0

我正在寻找一些现成的代码,这样我就可以将我的数据、标签等插入到一个函数中并接收一个像这样的现成的谷歌图表。

在此处输入图像描述

但我没有找到。有任何想法吗?

4

1 回答 1

1

同时,我编写了一个函数来接收所有必要的参数,创建一个谷歌图表并转换它UIImage。这里是:

-(UIImage )产生GoogleChartImage:(NSString)标题

                           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

错误:&错误]; NSLog(@"%@",error); NSLog(@"%@",响应); NSLog(@"%@",imageData);

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


return myimage;

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

这是测试功能:

-(无效)测试{

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;

}

你应该得到图像,就像问题中的那个一样。

于 2012-11-07T16:21:04.370 回答