GoogleCharts.st 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. Smalltalk current createPackage: 'GoogleCharts' properties: #{}!
  2. Object subclass: #ChartApp
  3. instanceVariableNames: ''
  4. package: 'GoogleCharts'!
  5. !ChartApp commentStamp!
  6. A chart app is an example App which loads the google JSAPI and visualization API.!
  7. !ChartApp methodsFor: 'not yet classified'!
  8. begin
  9. "Start the executiong of the ChartApp"
  10. ^self
  11. !
  12. initialize
  13. "Load my external JS"
  14. self class loadGoogleLoader:[self class loadVisualization:[self begin]]
  15. ! !
  16. !ChartApp class methodsFor: 'not yet classified'!
  17. loadGoogleLoader: callback
  18. "Load the Google JSAPI - Use JQuery.ajax() since that is available"
  19. <$.ajax({url:"https://www.google.com/jsapi",dataType:"script",success:callback});>
  20. !
  21. loadVisualization: callback
  22. "Use google.load() to load visualization and load the needed packages"
  23. |packages|
  24. packages := self neededVisualizationPackages.
  25. <google.load("visualization","1",{"callback" : callback , "packages":packages});>
  26. !
  27. neededVisualizationPackages
  28. "This is a hook for subclasses to define which visualization packages to load."
  29. ^{}
  30. ! !
  31. Object subclass: #ChartButton
  32. instanceVariableNames: 'element clickBlock'
  33. package: 'GoogleCharts'!
  34. !ChartButton methodsFor: 'not yet classified'!
  35. activate
  36. |button|
  37. button := self element asJQuery.
  38. button click:[self clickBlock value]
  39. !
  40. clickBlock
  41. ^clickBlock
  42. !
  43. clickBlock: aBlock
  44. clickBlock := aBlock
  45. !
  46. element
  47. ^element
  48. !
  49. element: aSymbol
  50. element := aSymbol
  51. ! !
  52. !ChartButton class methodsFor: 'not yet classified'!
  53. element: elementSymbol clickBlock: clickBlock
  54. ^self new element: elementSymbol; clickBlock: clickBlock; activate;yourself
  55. !
  56. popUpChart: chart atDom: element
  57. "Make the chart popup on click of an element"
  58. ^self element: element clickBlock:[chart drawChart]
  59. ! !
  60. Object subclass: #GoogleChart
  61. instanceVariableNames: 'chartId chartType'
  62. package: 'GoogleCharts'!
  63. !GoogleChart methodsFor: 'DOM'!
  64. getElementById: id
  65. "Find element by the id in the DOM"
  66. ^ <document.getElementById(id)>
  67. ! !
  68. !GoogleChart methodsFor: 'abstraction'!
  69. makeData
  70. "abstraction - return the data for a google chart"
  71. ^self subclassresponsibility
  72. !
  73. makeOptions
  74. "Abstract method - return options for a Google Chart"
  75. ^ self subclassresponsibility
  76. ! !
  77. !GoogleChart methodsFor: 'accessor'!
  78. chartId
  79. ^chartId
  80. !
  81. chartId: aString
  82. chartId := aString
  83. !
  84. chartType
  85. ^ chartType
  86. !
  87. chartType: aString
  88. chartType := aString
  89. ! !
  90. !GoogleChart methodsFor: 'chart'!
  91. drawChart
  92. | chart data options|
  93. data := self makeData.
  94. chart :=self makeChart:self chartId.
  95. options :=self makeOptions.
  96. <chart.draw(data,options)>
  97. !
  98. makeChart: id
  99. "build a chart at specific element id in the DOM and return"
  100. |e t|
  101. e := self getElementById:id.
  102. t := self chartType.
  103. ^ <new google.visualization[t](e)>
  104. ! !
  105. !GoogleChart methodsFor: 'data table'!
  106. arrayToDataTable: array
  107. ^ <google.visualization.arrayToDataTable(array)>
  108. ! !
  109. !GoogleChart methodsFor: 'init'!
  110. initialize
  111. ^self
  112. ! !
  113. !GoogleChart class methodsFor: 'not yet classified'!
  114. chartId: aString
  115. ^self new chartId:aString;yourself
  116. ! !
  117. GoogleChart subclass: #GaugeChart
  118. instanceVariableNames: ''
  119. package: 'GoogleCharts'!
  120. !GaugeChart methodsFor: 'not yet classified'!
  121. initialize
  122. " Create a Guage with the chartId that identifies the chart graphic placement and the chartType to be created at that id."
  123. super initialize.
  124. self chartType:'Gauge'.
  125. ^self
  126. ! !
  127. GoogleChart subclass: #GeoChart
  128. instanceVariableNames: ''
  129. package: 'GoogleCharts'!
  130. !GeoChart methodsFor: 'not yet classified'!
  131. initialize
  132. " Create a Geo Chart"
  133. super initialize.
  134. self chartType:'GeoChart'.
  135. ^self
  136. ! !
  137. GoogleChart subclass: #PieChart
  138. instanceVariableNames: ''
  139. package: 'GoogleCharts'!
  140. !PieChart methodsFor: 'not yet classified'!
  141. initialize
  142. super initialize.
  143. self chartType:'PieChart'.
  144. ^self
  145. ! !
  146. GoogleChart subclass: #ScatterChart
  147. instanceVariableNames: ''
  148. package: 'GoogleCharts'!
  149. !ScatterChart methodsFor: 'not yet classified'!
  150. initialize
  151. super initialize.
  152. self chartType:'ScatterChart'.
  153. ^self
  154. ! !