动态谷歌图表

发布于 2024-11-15 23:12:58 字数 1292 浏览 2 评论 0 原文

好吧,我得到了这个代码:

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;   

那里没什么特别的...... 但是当我请求网址时: 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 这样的度数...但我想要值而不是我从数据库中获得的学位......我该怎么做?

well i got this code:

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;   

Nothing realy special there...
but when i request the url: http://chart.apis.google.com/chart?chf=bg,s,67676700&chs=300x225&cht=p&chd=s:Uf9a&chdl=30°|40°|50°|60°

it requests with degrees like 30/40/50 ... but i want values and not degreses in there which i get from the database.., how can i do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2024-11-22 23:12:58

您只需要从数据库中获取学位并将其存储在集合中。然后您将能够循环该集合并将它们添加到字符串中。

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

You will need to just get the degrees from a database and store them in a collection. Then you will be able to loop over the collection and add them to the string.

//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:@"|"];
    }
别靠近我心 2024-11-22 23:12:58

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

在此处输入图像描述

这里是:

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

 xAxis:(NSArray*)axisXLabels
                          yAxis:(NSArray*)axisYLabels
                        andData:(NSArray*)dataValues
                          颜色:(NSString*)线条颜色
                     图表宽度:(NSNumber*)宽度
                     图表高度:(NSNumber*)高度
                    lagendLabel:(NSString*)图例
                       minScale:(NSNumber*)minScale
                       maxScale:(NSNumber*)maxScale{

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


//axisX标签
int countAxisXLabels = [axisXLabels 计数];

for(NSUInteger i = 0; i < countAxisXLabels; ++i)
{
    NSNumber *value = [axisXLabels objectAtIndex:i];
    [myurl追加格式:@"%@",值];
    if(i < countAxisXLabels - 1)
        [myurl追加字符串:@“|”];
}

[myurlappendString:@"|1:|"];


//axisY标签
 int countAxisYLabels = [axisYLabels 计数];
for(NSUInteger i = 0; i < countAxisYLabels; ++i)
{
    NSNumber *value = [axisYLabels objectAtIndex:i];
    [myurl追加格式:@"%@",值];
    if(i < countAxisYLabels - 1)
        [myurl追加字符串:@“|”];
}

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

//尺寸
[myurl appendFormat:@"%@x%@&cht=lc&chd=t:", 宽度,高度];

//数据值
int countDataValues = [数据值计数];

for(NSUInteger i = 0; i < countDataValues; ++i)
{
    NSNumber *value = [dataValues objectAtIndex:i];
    [myurl追加格式:@"%@",值];
    if(i < countDataValues - 1)
    [myurl 附加字符串:@","];
}

//传奇
[myurl appendFormat:@"&chdl=%@&chg=25,50&chls=2&",图例];

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

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

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



NSString *theurl=[myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

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

缓存策略:NSURLRequestUseProtocolCachePolicy
超时间隔:60.0];

NSURLResponse* 响应;
NSError* 错误;
NSData *imageData=[NSURLConnection sendSynchronousRequest:theRequest returnedResponse:&response

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

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


返回我的图像;

// 图表向导:https://developers.google.com/chart/image/docs/chart_wizard
 }

这是测试函数:

-(无效)测试{

NSString* title = @"身高历史记录";
NSArray *axisXLabels = [NSArray arrayWithObjects:
                       @“一月”,
                       @“二月”,
                       @“三月”,
                       @“君”,
                       @“七月”,
                       @“八月”,
                       零];


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

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

NSString *lineColor = @"FF9900";

NSNumber *宽度 = [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;

}

,就像问题中的图像一样。

I wrote a function that receive all the necessary parameters, creates a google chart and converts it to a UIImage.

enter image description here

Here it is:

-(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
 }

And here is the test function:

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

}

You should get image, like the one in the question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文