我正在尝试让Google的图表API在UIWebView上工作,但我就是无法得到它!
我创建了 UIWebView,将委托设置为自行处理任何加载错误(使用 didFailLoadWithError
方法,顺便说一句没有错误),以这种方式使用 html 字符串触发加载:
@interface GCAViewController ()
@property (weak, nonatomic) IBOutlet UIButton *buttonGo;
@property (weak, nonatomic) IBOutlet UIWebView *webViewChart;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *spinner;
@end
@implementation GCAViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.webViewChart.delegate = self;
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"%@", error.description);
}
- (IBAction)buttonGoClick:(id)sender {
NSString *str = @"<html>"
"<head>"
"<script type="text/javascript" src="https://www.google.com/jsapi"></script>"
"<script type="text/javascript">"
"alert('t1');"
"google.load('visualization', '1.0', {'packages':['corechart']});"
"google.setOnLoadCallback(drawChart);"
"function drawChart() {"
"var data = new google.visualization.DataTable();"
"data.addColumn('string', 'Topping');"
"data.addColumn('number', 'Slices');"
"data.addRows(["
"['Mushrooms', 3],"
"['Onions', 1],"
"['Olives', 1],"
"['Zucchini', 1],"
"['Pepperoni', 2]"
"]);"
"var options = {'title':'How Much Pizza I Ate Last Night',"
"'width':400px,"
"'height':300px};"
"var chart = new google.visualization.PieChart(document.getElementById('chart_div'));"
"chart.draw(data, options);"
"}"
"</script>"
"</head>"
"<body>"
"<div id="chart_div"></div>"
"</body>"
"</html>";
[self.webViewChart loadHTMLString:str baseURL:nil];
}
@end
什么也没发生!没有错误,没有消息,什么都没有!
谁能帮忙?
额外信息:这是一个测试应用程序,旨在仅执行此操作...
编辑1:经过一些调试,我得到了一个修复,即错误与函数DrawChart
有关。如果我删除函数声明,alert('t1')
发生得很好......
只需用引号将宽度/高度括起来
"'width':'400px',"
"'height':'300px'};"
我明白了...在函数中,它不是 300px/400px -> 它不能在末尾有 px...奇怪。。。