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