0

I'm trying to get Googles Chart Api to work on a UIWebView but i just can't get it!

I created the UIWebView, set the delegate to self get my hands on any load error (using the didFailLoadWithError method, no errors btw), fire the load using a html string this way:

@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

and nothing happens! No errors, no messages, nothing!

can anyone help?

Extra info: this is a test app designed to do only this...

EDIT 1: after some debug i got a fix that the error is related to the function DrawChart. If i remove the function declaration the alert('t1') happens just fine...

Leonardo
  • 10,737
  • 10
  • 62
  • 155

2 Answers2

0

I got it... in the function, its not 300px/400px -> It cannot have the px at the end... weird...

Leonardo
  • 10,737
  • 10
  • 62
  • 155
  • I had to `NSLog` your str and run the output in a browser to find the bug. You were a few seconds faster than me in figuring it out but thanks for accepting the answer :) – Rami Enbashi Jan 18 '14 at 00:58
0

just enclose the width/height with quotes

"'width':'400px',"
"'height':'300px'};"
Rami Enbashi
  • 3,526
  • 1
  • 19
  • 21