Динамические диаграммы Google - PullRequest
1 голос
/ 17 июня 2011

хорошо, я получил этот код:

NSLog(@"button pressed");
    NSString* myurl=@"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 ... но я хочу значения, а не градусы, которые я получаю из базы данных .., как я могу это сделать?

Ответы [ 2 ]

2 голосов
/ 07 ноября 2012

Я написал функцию, которая получает все необходимые параметры, создает диаграмму Google и преобразует ее в UIImage.

enter image description here

Вот оно:

- (UIImage ) yieldGoogleChartImage: (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

ошибка: & ошибка]; NSLog (@ "% @", ошибка); 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: ширина chartHight: высота lagendLabel: legendLabel minScale: minScale maxScale: maxScale];

_chartImage.image=myimage;

}

Вы должны получить изображение, подобное изображенному в вопросе.

2 голосов
/ 17 июня 2011

Вам нужно будет просто получить степени из базы данных и сохранить их в коллекции. Тогда вы сможете перебрать коллекцию и добавить их в строку.

//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:@"|"];
    }
...