Version 3.18.1
Show:

File: charts/js/SeriesBase.js

  1. /**
  2. * Provides functionality for creating a chart series.
  3. *
  4. * @module charts
  5. * @submodule series-base
  6. */
  7. /**
  8. * An abstract class for creating series instances.
  9. * SeriesBase is used by the following classes:
  10. * <ul>
  11. * <li>{{#crossLink "CartesianSeries"}}{{/crossLink}}</li>
  12. * <li>{{#crossLink "PieSeries"}}{{/crossLink}}</li>
  13. * </ul>
  14. *
  15. * @class SeriesBase
  16. * @extends Base
  17. * @uses Renderer
  18. * @constructor
  19. * @param {Object} config (optional) Configuration parameters.
  20. * @submodule series-base
  21. */
  22. Y.SeriesBase = Y.Base.create("seriesBase", Y.Base, [Y.Renderer], {
  23. /**
  24. * @method render
  25. * @private
  26. */
  27. render: function()
  28. {
  29. this._setCanvas();
  30. this.addListeners();
  31. this.validate();
  32. },
  33. /**
  34. * Creates a `Graphic` instance.
  35. *
  36. * @method _setCanvas
  37. * @protected
  38. */
  39. _setCanvas: function()
  40. {
  41. var graph = this.get("graph"),
  42. graphic = graph.get("graphic");
  43. this.set("graphic", graphic);
  44. },
  45. /**
  46. * Returns a reference to the parent container to which all chart elements are contained.
  47. * When the series is bound to a `Chart` instance, the `Chart` instance is the reference.
  48. * If nothing is set as the `chart` attribute, the `_getChart` method will return a reference
  49. * to the `graphic` attribute.
  50. *
  51. * @method _getChart
  52. * @return {Object}
  53. * @private
  54. */
  55. _getChart:function() {
  56. var chart,
  57. graph = this.get("graph");
  58. if(graph)
  59. {
  60. chart = graph.get("chart");
  61. }
  62. if(!chart)
  63. {
  64. chart = this.get("graphic");
  65. }
  66. return chart;
  67. },
  68. /**
  69. * Returns the sum of all values for the series.
  70. *
  71. * @method getTotalValues
  72. * @return Number
  73. */
  74. getTotalValues: function()
  75. {
  76. var valueCoord = this.get("direction") === "vertical" ? "x" : "y",
  77. total = this.get(valueCoord + "Axis").getTotalByKey(this.get(valueCoord + "Key"));
  78. return total;
  79. },
  80. /**
  81. * Gets the default value for the `styles` attribute. Overrides
  82. * base implementation.
  83. *
  84. * @method _getDefaultStyles
  85. * @return Object
  86. * @protected
  87. */
  88. _getDefaultStyles: function()
  89. {
  90. return {padding:{
  91. top: 0,
  92. left: 0,
  93. right: 0,
  94. bottom: 0
  95. }};
  96. },
  97. /**
  98. * Shows/hides contents of the series.
  99. *
  100. * @method _handleVisibleChange
  101. * @param {Object} e Event object.
  102. * @protected
  103. */
  104. _handleVisibleChange: function()
  105. {
  106. this._toggleVisible(this.get("visible"));
  107. },
  108. /**
  109. * Destructor implementation for the CartesianSeries class. Calls destroy on all Graphic instances.
  110. *
  111. * @method destructor
  112. * @protected
  113. */
  114. destructor: function()
  115. {
  116. var marker,
  117. markers = this.get("markers");
  118. if(this.get("rendered"))
  119. {
  120. if(this._stylesChangeHandle)
  121. {
  122. this._stylesChangeHandle.detach();
  123. }
  124. if(this._widthChangeHandle)
  125. {
  126. this._widthChangeHandle.detach();
  127. }
  128. if(this._heightChangeHandle)
  129. {
  130. this._heightChangeHandle.detach();
  131. }
  132. if(this._visibleChangeHandle)
  133. {
  134. this._visibleChangeHandle.detach();
  135. }
  136. }
  137. while(markers && markers.length > 0)
  138. {
  139. marker = markers.shift();
  140. if(marker && marker instanceof Y.Shape)
  141. {
  142. marker.destroy();
  143. }
  144. }
  145. if(this._path)
  146. {
  147. this._path.destroy();
  148. this._path = null;
  149. }
  150. if(this._lineGraphic)
  151. {
  152. this._lineGraphic.destroy();
  153. this._lineGraphic = null;
  154. }
  155. if(this._groupMarker)
  156. {
  157. this._groupMarker.destroy();
  158. this._groupMarker = null;
  159. }
  160. },
  161. /**
  162. * Collection of default colors used for lines in a series when not specified by user.
  163. *
  164. * @property _defaultLineColors
  165. * @type Array
  166. * @protected
  167. */
  168. _defaultLineColors:[
  169. "#426ab3",
  170. "#d09b2c",
  171. "#000000",
  172. "#b82837",
  173. "#b384b5",
  174. "#ff7200",
  175. "#779de3",
  176. "#cbc8ba",
  177. "#7ed7a6",
  178. "#007a6c"
  179. ],
  180. /**
  181. * Collection of default colors used for marker fills in a series when not specified by user.
  182. *
  183. * @property _defaultFillColors
  184. * @type Array
  185. * @protected
  186. */
  187. _defaultFillColors:[
  188. "#6084d0",
  189. "#eeb647",
  190. "#6c6b5f",
  191. "#d6484f",
  192. "#ce9ed1",
  193. "#ff9f3b",
  194. "#93b7ff",
  195. "#e0ddd0",
  196. "#94ecba",
  197. "#309687"
  198. ],
  199. /**
  200. * Collection of default colors used for marker borders in a series when not specified by user.
  201. *
  202. * @property _defaultBorderColors
  203. * @type Array
  204. * @protected
  205. */
  206. _defaultBorderColors:[
  207. "#205096",
  208. "#b38206",
  209. "#000000",
  210. "#94001e",
  211. "#9d6fa0",
  212. "#e55b00",
  213. "#5e85c9",
  214. "#adab9e",
  215. "#6ac291",
  216. "#006457"
  217. ],
  218. /**
  219. * Collection of default colors used for area fills, histogram fills and pie fills in a series when not specified by user.
  220. *
  221. * @property _defaultSliceColors
  222. * @type Array
  223. * @protected
  224. */
  225. _defaultSliceColors: [
  226. "#66007f",
  227. "#a86f41",
  228. "#295454",
  229. "#996ab2",
  230. "#e8cdb7",
  231. "#90bdbd",
  232. "#000000",
  233. "#c3b8ca",
  234. "#968373",
  235. "#678585"
  236. ],
  237. /**
  238. * Parses a color based on a series order and type.
  239. *
  240. * @method _getDefaultColor
  241. * @param {Number} index Index indicating the series order.
  242. * @param {String} type Indicates which type of object needs the color.
  243. * @return String
  244. * @protected
  245. */
  246. _getDefaultColor: function(index, type)
  247. {
  248. var colors = {
  249. line: this._defaultLineColors,
  250. fill: this._defaultFillColors,
  251. border: this._defaultBorderColors,
  252. slice: this._defaultSliceColors
  253. },
  254. col = colors[type] || colors.fill,
  255. l = col.length;
  256. index = index || 0;
  257. if(index >= l)
  258. {
  259. index = index % l;
  260. }
  261. type = type || "fill";
  262. return colors[type][index];
  263. }
  264. }, {
  265. ATTRS: {
  266. /*
  267. * Returns the width of the parent graph
  268. *
  269. * @attribute width
  270. * @type Number
  271. */
  272. width: {
  273. readOnly: true,
  274. getter: function()
  275. {
  276. return this.get("graphic").get("width");
  277. }
  278. },
  279. /**
  280. * Returns the height of the parent graph
  281. *
  282. * @attribute height
  283. * @type Number
  284. */
  285. height: {
  286. readOnly: true,
  287. getter: function()
  288. {
  289. return this.get("graphic").get("height");
  290. }
  291. },
  292. /**
  293. * The graphic in which drawings will be rendered.
  294. *
  295. * @attribute graphic
  296. * @type Graphic
  297. */
  298. graphic: {
  299. lazyAdd: false,
  300. setter: function(val) {
  301. //woraround for Attribute order of operations bug
  302. if(!this.get("rendered")) {
  303. this.set("rendered", true);
  304. }
  305. return val;
  306. }
  307. },
  308. /**
  309. * Reference to the `Chart` application. If no `Chart` application is present,
  310. * a reference to the `Graphic` instance that the series is drawn into will be returned.
  311. *
  312. * @attribute chart
  313. * @type ChartBase
  314. */
  315. chart: {
  316. getter: function()
  317. {
  318. var chart,
  319. graph = this.get("graph");
  320. if(graph)
  321. {
  322. chart = graph.get("chart");
  323. }
  324. return chart;
  325. }
  326. },
  327. /**
  328. * Reference to the `Graph` in which the series is drawn into.
  329. *
  330. * @attribute graph
  331. * @type Graph
  332. */
  333. graph: {},
  334. /**
  335. * Indicates whether the Series has been through its initial set up.
  336. *
  337. * @attribute rendered
  338. * @type Boolean
  339. */
  340. rendered: {
  341. value: false
  342. },
  343. /**
  344. * Indicates whether to show the series
  345. *
  346. * @attribute visible
  347. * @type Boolean
  348. * @default true
  349. */
  350. visible: {
  351. value: true
  352. },
  353. /**
  354. * Indicates whether or not markers for a series will be grouped and rendered in a single complex shape instance.
  355. *
  356. * @attribute groupMarkers
  357. * @type Boolean
  358. */
  359. groupMarkers: {
  360. getter: function()
  361. {
  362. var graph,
  363. groupMarkers = this._groupMarkers;
  364. if(!groupMarkers) {
  365. graph = this.get("graph");
  366. if(graph)
  367. {
  368. groupMarkers = graph.get("groupMarkers");
  369. }
  370. }
  371. return groupMarkers;
  372. },
  373. setter: function(val)
  374. {
  375. this._groupMarkers = val;
  376. return val;
  377. }
  378. }
  379. }
  380. });