iOS中画图表 使用HighChart

来源:互联网 发布:杭州橙群网络怎么样 编辑:程序博客网 时间:2024/05/29 13:52


首先,要将HighChart的包导入到工程中。这个包在下面的例子中是有的。

代码例子下载: http://pan.baidu.com/s/1eQIG68y


为WebView添加一个类别方便方法调用

UIWebView+UIWebView_LoadHighChart.h

////  UIWebView+UIWebView_LoadHighChart.h//  HighChartDemo////  Created by 杜甲 on 14-1-7.//  Copyright (c) 2014年 杜甲. All rights reserved.//#import <UIKit/UIKit.h>@interface UIWebView (UIWebView_LoadHighChart)-(void)loadHightChart;-(void)loadDocument:(NSString *)docName;@end


UIWebView+UIWebView_LoadHighChart.m

////  UIWebView+UIWebView_LoadHighChart.m//  HighChartDemo////  Created by 杜甲 on 14-1-7.//  Copyright (c) 2014年 杜甲. All rights reserved.//#import "UIWebView+UIWebView_LoadHighChart.h"#define ChartHtml @"demo.bundle/adaperHighChart.html"@implementation UIWebView (UIWebView_LoadHighChart)-(void)loadHightChart{    // [self performSelectorInBackground:@selector(loadDocument:) withObject:ChartHtml];    [self performSelectorOnMainThread:@selector(loadDocument:) withObject:ChartHtml waitUntilDone:NO];    //  [self loadDocument:ChartHtml];}-(void)loadDocument:(NSString *)docName{    NSString *mainBundleDirectory=[[NSBundle mainBundle] bundlePath];    NSString *path=[mainBundleDirectory stringByAppendingPathComponent:docName];        NSURL *url=[NSURL fileURLWithPath:path];    NSURLRequest *request=[NSURLRequest requestWithURL:url];        [self loadRequest:request];        }@end

ViewController.h

////  ViewController.h//  NewHighChartDemo////  Created by 杜甲 on 14-1-7.//  Copyright (c) 2014年 杜甲. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIWebViewDelegate>@end


ViewController.m

////  ViewController.m//  NewHighChartDemo////  Created by 杜甲 on 14-1-7.//  Copyright (c) 2014年 杜甲. All rights reserved.//#import "ViewController.h"#import "UIWebView+UIWebView_LoadHighChart.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.        UIWebView* web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];        web.delegate = self;            [self.view addSubview:web];    [web loadHightChart];            }- (void)webViewDidFinishLoad:(UIWebView *)webView{    //showChart($('#container'),'',['吉林','大连','辽宁','黑龙江','上海','江苏','宁波','杭州','浙江','厦门','福建','江西','山东','青岛','湖南','湖北','河南'],'',[{type:'line' ,data:[['吉林',9.840000] ,['大连',7.270000] ,['辽宁',2.660000] ,['黑龙江',1.780000] ,['上海',8.040000] ,['江苏',2.600000] ,['宁波',3.960000] ,['杭州',1.900000] ,['浙江',1.280000] ,['厦门',3.450000] ,['福建',5.430000] ,['江西',0.850000] ,['山东',3.000000] ,['青岛',3.650000] ,['湖南',1.840000] ,['湖北',4.930000] ,['河南',0.140000] ] , name:'应收率'}],'line',335.000000,230.000000)            NSArray* cityArr = @[@"吉林",@"大连",@"辽宁",@"黑龙江",@"上海",@"江苏",@"宁波",@"杭州",@"浙江",@"厦门",@"福建",@"江西",@"山东",@"青岛",@"湖南",@"湖北",@"河南"];        NSData* jsonData = [NSJSONSerialization dataWithJSONObject:cityArr options:NSJSONWritingPrettyPrinted error:nil];    NSString* json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];        NSLog(@"%@",json);       [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"showChart($('#container'),'',%@,'',[{type:'line' ,data:[['吉林',9.840000] ,['大连',7.270000] ,['辽宁',2.660000] ,['黑龙江',1.780000] ,['上海',8.040000] ,['江苏',2.600000] ,['宁波',3.960000] ,['杭州',1.900000] ,['浙江',1.280000] ,['厦门',3.450000] ,['福建',5.430000] ,['江西',0.850000] ,['山东',3.000000] ,['青岛',3.650000] ,['湖南',1.840000] ,['湖北',4.930000] ,['河南',0.140000] ] , name:'应收率'}],'line',335.000000,230.000000)",json]];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end













0 0
原创粉丝点击