calling Google Chart

Please share your functions/code-snips

calling Google Chart

Postby udo.killermann » Sun Mar 06, 2011 10:57 am

Board,

Google offers services to draw charts from data you provide in an URL or Post to their server:
googleChart.jpg
googleChart.jpg (34.05 KiB) Viewed 2489 times


You have to build the URL based on Google's documentation: http://code.google.com/intl/de-DE/apis/chart/docs/making_charts.html

I've created a little ObjB project to draw a pie chart (left side) based on data you enter in a table (right side):
googleChart.zip
(77.87 KiB) Downloaded 255 times


One central element of the application lies within the nib file. It's an array controller and helps to interact with tables:
arrayController.jpg
arrayController.jpg (43.1 KiB) Viewed 2489 times


If you look at its inlets and outlets, you can see that I have wired two buttons to the controller, so I can easily add and remove table items:
(see below)
Last edited by udo.killermann on Sun Mar 06, 2011 3:11 pm, edited 1 time in total.
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Re: calling Google Chart

Postby udo.killermann » Sun Mar 06, 2011 11:06 am

acConnections.jpg
acConnections.jpg (43.11 KiB) Viewed 2488 times


The buttons are wired to the add and remove actions of the array controller respectively.

Now let's look at the table columns:
columnBinding.jpg
columnBinding.jpg (38.33 KiB) Viewed 2488 times


The column we are looking at is bound to the array controller's arrangedObjects and specifically to the label key of each line within the array. So actually we are dealing with an array and every element is a dictionary with the keys label and value.

At last here comes the code:
Code: Select all

' The following event sub is automatically called when your app starts.
' Why? The global.NSObject is part of the MainMenu.nib file, which
' is automatically loaded and created during start up. After that its event sub AwakeFromNib() is executed.


Declare Function "NSData" - (id)initWithContentsOfURL:(NSURL *)aURL
Declare Function "NSData" + (id)dataWithData:(NSData *)aData

Declare Function "NSData" + (id)dataWithContentsOfURL:(id)aURL
Declare Function "NSData" - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

Declare Class "NSURL"
Declare Function "NSURL" + (NSURL *)URLWithString:(NSString *)URLString
Declare Function "NSURL" - (BOOL)isFileURL
Declare Function "NSURL" - (NSString *)relativePath
Declare Function "NSURL" - (NSArray *)pathComponents

Declare Function "NSImage" - (id)initWithData:(NSData *)td
Declare Function "NSImage" - (id)initWithContentsOfFile:(NSString *)FileName
   
Declare Function "NSString" - (NSString *)stringByAddingPercentEscapesUsingEncoding:(int)encoding
     
Declare Class "NSArrayController"

Declare Sub "NSArrayController" - (void)insert:(id)sender
Declare Sub "NSArrayController" - (void)addObject:(id)object
Declare Sub "NSArrayController" - (void)addObjects:(NSArray *)objects
Declare Function "NSArrayController" - (id)arrangedObjects

Declare Function "NSMutableArray" - (void)addObjectsFromArray:(NSArray *)otherArray

IBOutlet ac As NSArrayController
IBOutlet theImageView As NSImageView
 
Function encodeArray(inArray As NSArray) As Dictionary
   Dim lineDic As Dictionary
   Dim valueStr As String
   Dim labelStr As String
   Dim index As Integer
   
   For index = 0 To inArray.Count() - 1
    lineDic = inArray.Object(index)
    valueStr = valueStr & "," & lineDic.Object("value")
    labelStr = labelStr & "|" & lineDic.Object("label")
   Next
 
  labelStr = Right(labelStr, Len(labelStr) - 1)
  valueStr = Right(valueStr, Len(valueStr) - 1)
 
  Return(Dictionary("labels":=labelStr, "values":=valueStr))
     
End Function

Sub drawChart()
      Dim argDictionary As Dictionary
      Dim myURL As NSURL
     
      Dim URLString As NSString
      Dim encodedURL As NSString
     
      Dim myData As NSData
     
      Dim myImage As NSImage
     
      argDictionary = encodeArray(ac.arrangedObjects())
     
      URLString = "https://chart.googleapis.com/chart?chs=375x150&chd=t:" & argDictionary.Object("values") & "&cht=p3&chl=" & argDictionary.Object("labels") & "&chf=bg,s,0000FF00" & "&chdls=ff00ff,14"
     
      encodedURL= URLString.stringByAddingPercentEscapesUsingEncoding(1)
      myURL = NSURL.URLWithString(encodedURL)
     
      myData = NSData.dataWithContentsOfURL(myURL)
     
      If (myData = Nil) Then
        Alert("No connection to the internet!")
      Else
        myData.writeToFile(NSBundle.ResourcePath()&"/chart.png", atomically := No)
       
        myImage = NSImage(NSBundle.ResourcePath()&"/chart.png")
 
        theImageView.Image = myImage
      End If
End Sub
 

IBAction redrawChart(sender As id)
  drawChart()
End IBAction
 

Event AwakeFromNib()
   Dim inArray As Array(Dictionary("value" := "15", "label" := "fritz"), Dictionary("value" := "15", "label" := "peter"), Dictionary("value" := "15", "label" := "udo"))

   ac.addObjects(inArray)
   ac.addObject(Dictionary("value":=30,"label":="karl"))
     
   drawChart()

End Event
   


If you have questions: please ask!

Regards
Udo
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Re: calling Google Chart

Postby berndnoetscher » Mon Mar 07, 2011 12:36 pm

Udo,

great work :-) Thanks.
berndnoetscher
Site Admin
 
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am


Return to Code examples (Xcode 3)

Who is online

Users browsing this forum: No registered users and 2 guests