/sandbox/chart/as/com/yahoo/infographics/series/PlotSeries.as

https://github.com/nzakas/yui3-1 · ActionScript · 338 lines · 216 code · 26 blank · 96 comment · 6 complexity · 6323e4176392051ed623e89452bd104e MD5 · raw file

  1. package com.yahoo.infographics.series {
  2. import com.yahoo.infographics.data.AxisData;
  3. import flash.display.DisplayObject;
  4. import com.yahoo.renderers.Renderer;
  5. import com.yahoo.infographics.styles.PlotStyles;
  6. import flash.display.Sprite;
  7. import flash.display.DisplayObject;
  8. import flash.display.InteractiveObject;
  9. import flash.events.MouseEvent;
  10. import com.yahoo.display.*;
  11. import com.yahoo.infographics.events.MarkerEvent;
  12. public class PlotSeries extends Cartesian
  13. {
  14. /**
  15. * Constructor
  16. */
  17. public function PlotSeries(series:Object)
  18. {
  19. super(series);
  20. }
  21. /**
  22. * @private
  23. * Style class for the graph.
  24. */
  25. private static var _styleClass:Class = PlotStyles;
  26. /**
  27. * @inheritDoc
  28. */
  29. override public function getStyleClass():Class
  30. {
  31. return _styleClass;
  32. }
  33. /**
  34. * @private (protected)
  35. */
  36. private var _type:String = "plot";
  37. /**
  38. * Indicates the type of graph.
  39. */
  40. override public function get type():String
  41. {
  42. return this._type;
  43. }
  44. /**
  45. * @private (setter)
  46. */
  47. override public function set type(value:String):void
  48. {
  49. this._type = value;
  50. }
  51. /**
  52. * @private (protected)
  53. * Storage for markers
  54. */
  55. protected var _markers:Vector.<SeriesMarker> = new Vector.<SeriesMarker>();
  56. /**
  57. * Collection or markers to be displayed.
  58. */
  59. public function get markers():Vector.<SeriesMarker>
  60. {
  61. return this._markers;
  62. }
  63. /**
  64. * @private (protected)
  65. * Collection of markers to be displayed.
  66. */
  67. protected var _markerCache:Vector.<SeriesMarker> = new Vector.<SeriesMarker>();
  68. /**
  69. * @private
  70. */
  71. protected var _markerClass:Class;
  72. /**
  73. * @private
  74. */
  75. protected var _markerStyles:Object;
  76. /**
  77. * @private (protected)
  78. * Factory for creating <code>SeriesMarker</code> instances.
  79. */
  80. protected var _markerFactory:ISkinFactory;
  81. /**
  82. * @private (protected)
  83. * Hash ISkinFactory classes.
  84. */
  85. protected var _skinFactoryHash:Object = {
  86. bitmap:BitmapSkinFactory
  87. };
  88. /**
  89. * @private (protected)
  90. */
  91. protected function getSkinFactoryClass(value:String):Class
  92. {
  93. return this._skinFactoryHash[value] as Class;
  94. }
  95. /**
  96. * @private
  97. */
  98. override protected function initializeStyleProps():void
  99. {
  100. super.initializeStyleProps();
  101. this.updateMarker();
  102. }
  103. /**
  104. * @private (override)
  105. */
  106. override protected function drawGraph():void
  107. {
  108. this.drawMarkers();
  109. }
  110. /**
  111. * @private (override)
  112. */
  113. override protected function updateStyleProps():void
  114. {
  115. if(this.checkFlag("marker"))
  116. {
  117. this.updateMarker();
  118. }
  119. }
  120. /**
  121. * @private (protected)
  122. * Creates a <code>InstanceFactory</code> for creating markers.
  123. */
  124. protected function updateMarker():void
  125. {
  126. var i:int,
  127. len:int = this._markers.length,
  128. marker:Object = this.getStyle("marker"),
  129. markerClass:Class = this.getSkinFactoryClass(marker.skin);
  130. this.removeMarkers();
  131. if(!(this._markerClass is markerClass))
  132. {
  133. this._markerFactory = new markerClass(marker.props, marker.styles);
  134. this._markerClass = markerClass;
  135. }
  136. else
  137. {
  138. this._markerFactory.createTemplate(marker.props, marker.styles);
  139. }
  140. }
  141. /**
  142. * @private (protected)
  143. * Creates and positions markers for the series.
  144. */
  145. protected function drawMarkers():void
  146. {
  147. var xcoords:Vector.<int> = this._xcoords,
  148. ycoords:Vector.<int> = this._ycoords,
  149. len:int = xcoords.length,
  150. i:int,
  151. marker:SeriesMarker;
  152. this.createMarkerCache();
  153. for(i = 0;i < len; i = ++i)
  154. {
  155. marker = this.getMarker();
  156. marker.index = i;
  157. marker.x = Number(xcoords[i] - marker.width/2);
  158. marker.y = Number(ycoords[i] - marker.height/2);
  159. marker.x = Number(xcoords[i] - marker.width/2);
  160. marker.y = Number(ycoords[i] - marker.height/2);
  161. this._markers.push(marker);
  162. }
  163. this.clearMarkerCache();
  164. }
  165. /**
  166. * @private (protected)
  167. * Movers markers from the marker collection to the cache collection
  168. * and clears the marker collection.
  169. */
  170. protected function createMarkerCache():void
  171. {
  172. this._markerCache = this._markers.concat();
  173. this._markers = new Vector.<SeriesMarker>();
  174. }
  175. /**
  176. * @private (protected)
  177. * Removes markers in the cache from the cache collection and
  178. * the display list.
  179. */
  180. protected function clearMarkerCache():void
  181. {
  182. var cache:Vector.<SeriesMarker> = this._markerCache,
  183. len:int = cache.length,
  184. marker:SeriesMarker;
  185. while(len > 0)
  186. {
  187. marker = SeriesMarker(cache.shift());
  188. marker.removeEventListener(MouseEvent.ROLL_OVER, this._graph.markerRollOverHandler);
  189. marker.removeEventListener(MouseEvent.ROLL_OUT, this._graph.markerRollOutHandler);
  190. marker.removeEventListener(MouseEvent.CLICK, this._graph.markerClickHandler);
  191. marker.removeEventListener(MouseEvent.DOUBLE_CLICK, this._graph.markerDoubleClickHandler);
  192. this.removeChild(marker);
  193. --len;
  194. }
  195. }
  196. /**
  197. * @private (protected)
  198. * Returns a marker. If one does not exist in the cache
  199. * collection, one is created.
  200. */
  201. protected function getMarker():SeriesMarker
  202. {
  203. var marker:SeriesMarker,
  204. cache:Vector.<SeriesMarker> = this._markerCache,
  205. len:int = cache.length,
  206. styles:Object = this._markerStyles;
  207. if(len > 0)
  208. {
  209. marker = SeriesMarker(cache.shift());
  210. }
  211. else
  212. {
  213. marker = new SeriesMarker();
  214. if(this._graph && this._graph.hotSpot)
  215. {
  216. marker.delegateListener(this._graph.hotSpot, this._hitTest);
  217. }
  218. marker.series = this;
  219. marker.skin = this._markerFactory.getSkinInstance();
  220. InteractiveObject(marker).doubleClickEnabled = true;
  221. marker.addEventListener(MouseEvent.ROLL_OVER, this._graph.markerRollOverHandler, false, 0, true);
  222. marker.addEventListener(MouseEvent.ROLL_OUT, this._graph.markerRollOutHandler, false, 0, true);
  223. marker.addEventListener(MouseEvent.CLICK, this._graph.markerClickHandler, false, 0, true);
  224. marker.addEventListener(MouseEvent.DOUBLE_CLICK, this._graph.markerDoubleClickHandler, false, 0, true);
  225. }
  226. this.addChild(marker);
  227. return marker;
  228. }
  229. /**
  230. * @private (protected)
  231. * Removes all markers from the display list and clears the marker and cache
  232. * collections.
  233. */
  234. protected function removeMarkers():void
  235. {
  236. var cache:Vector.<SeriesMarker> = this._markers,
  237. len:int = cache.length,
  238. marker:SeriesMarker;
  239. while(len > 0)
  240. {
  241. marker = SeriesMarker(cache.shift());
  242. marker.removeEventListener(MouseEvent.ROLL_OVER, this._graph.markerRollOverHandler);
  243. marker.removeEventListener(MouseEvent.ROLL_OUT, this._graph.markerRollOutHandler);
  244. marker.removeEventListener(MouseEvent.CLICK, this._graph.markerClickHandler);
  245. marker.removeEventListener(MouseEvent.DOUBLE_CLICK, this._graph.markerDoubleClickHandler);
  246. this.removeChild(marker);
  247. --len;
  248. }
  249. this.clearMarkerCache();
  250. }
  251. /**
  252. * @private (protected)
  253. */
  254. protected function markerRollOverHandler(event:MouseEvent):void
  255. {
  256. var marker:SeriesMarker = SeriesMarker(event.target),
  257. type:String = event.type,
  258. series:ISeries = marker.series,
  259. xkey:String = this.xKey,
  260. ykey:String = this.yKey,
  261. index:int = marker.index,
  262. xvalue:String = this._xAxisMode.getLabelByIndex(xkey, index),
  263. yvalue:String = this._yAxisMode.getLabelByIndex(ykey, index);
  264. this.dispatchEvent(new MarkerEvent(MarkerEvent.ITEM_ROLL_OVER, series, index, marker));
  265. }
  266. /**
  267. * @private (protected)
  268. */
  269. protected function markerRollOutHandler(event:MouseEvent):void
  270. {
  271. var marker:SeriesMarker = SeriesMarker(event.target),
  272. type:String = event.type,
  273. series:ISeries = marker.series,
  274. xkey:String = this.xKey,
  275. ykey:String = this.yKey,
  276. index:int = marker.index,
  277. xvalue:String = this._xAxisMode.getLabelByIndex(xkey, index),
  278. yvalue:String = this._yAxisMode.getLabelByIndex(ykey, index);
  279. this.dispatchEvent(new MarkerEvent(MarkerEvent.ITEM_ROLL_OUT, series, index, marker));
  280. }
  281. /**
  282. * @private (protected)
  283. */
  284. protected function markerClickHandler(event:MouseEvent):void
  285. {
  286. var marker:SeriesMarker = SeriesMarker(event.target),
  287. type:String = event.type,
  288. series:ISeries = marker.series,
  289. xkey:String = this.xKey,
  290. ykey:String = this.yKey,
  291. index:int = marker.index,
  292. xvalue:String = this._xAxisMode.getLabelByIndex(xkey, index),
  293. yvalue:String = this._yAxisMode.getLabelByIndex(ykey, index);
  294. this.dispatchEvent(new MarkerEvent(MarkerEvent.ITEM_CLICK, series, index, marker));
  295. }
  296. /**
  297. * @private (protected)
  298. */
  299. protected function markerDoubleClickHandler(event:MouseEvent):void
  300. {
  301. var marker:SeriesMarker = SeriesMarker(event.target),
  302. type:String = event.type,
  303. series:ISeries = marker.series,
  304. xkey:String = this.xKey,
  305. ykey:String = this.yKey,
  306. index:int = marker.index,
  307. xvalue:String = this._xAxisMode.getLabelByIndex(xkey, index),
  308. yvalue:String = this._yAxisMode.getLabelByIndex(ykey, index);
  309. this.dispatchEvent(new MarkerEvent(MarkerEvent.ITEM_DOUBLE_CLICK, series, index, marker));
  310. }
  311. }
  312. }