Version 3.18.1
Show:

File: charts/js/Renderer.js

  1. /**
  2. * The Charts widget provides an api for displaying data
  3. * graphically.
  4. *
  5. * @module charts
  6. * @main charts
  7. */
  8. /**
  9. * Provides functionality for the handling of axis data in a chart.
  10. *
  11. * @module charts
  12. * @submodule axis-base
  13. */
  14. var Y_Lang = Y.Lang;
  15. /**
  16. * The Renderer class is a base class for chart components that use the `styles`
  17. * attribute.
  18. *
  19. * @module charts
  20. * @class Renderer
  21. * @constructor
  22. */
  23. function Renderer(){}
  24. Renderer.ATTRS = {
  25. /**
  26. * Style properties for class
  27. *
  28. * @attribute styles
  29. * @type Object
  30. */
  31. styles:
  32. {
  33. getter: function()
  34. {
  35. this._styles = this._styles || this._getDefaultStyles();
  36. return this._styles;
  37. },
  38. setter: function(val)
  39. {
  40. this._styles = this._setStyles(val);
  41. }
  42. },
  43. /**
  44. * The graphic in which drawings will be rendered.
  45. *
  46. * @attribute graphic
  47. * @type Graphic
  48. */
  49. graphic: {}
  50. };
  51. Renderer.NAME = "renderer";
  52. Renderer.prototype = {
  53. /**
  54. * Storage for `styles` attribute.
  55. *
  56. * @property _styles
  57. * @type Object
  58. * @private
  59. */
  60. _styles: null,
  61. /**
  62. * Method used by `styles` setter.
  63. *
  64. * @method _setStyles
  65. * @param {Object} newStyles Hash of properties to update.
  66. * @return Object
  67. * @protected
  68. */
  69. _setStyles: function(newstyles)
  70. {
  71. var styles = this.get("styles");
  72. return this._mergeStyles(newstyles, styles);
  73. },
  74. /**
  75. * Merges to object literals so that only specified properties are
  76. * overwritten.
  77. *
  78. * @method _mergeStyles
  79. * @param {Object} a Hash of new styles
  80. * @param {Object} b Hash of original styles
  81. * @return Object
  82. * @protected
  83. */
  84. _mergeStyles: function(a, b)
  85. {
  86. if(!b)
  87. {
  88. b = {};
  89. }
  90. var newstyles = Y.merge(b, {});
  91. Y.Object.each(a, function(value, key)
  92. {
  93. if(b.hasOwnProperty(key) && Y_Lang.isObject(value) && !Y_Lang.isFunction(value) && !Y_Lang.isArray(value))
  94. {
  95. newstyles[key] = this._mergeStyles(value, b[key]);
  96. }
  97. else
  98. {
  99. newstyles[key] = value;
  100. }
  101. }, this);
  102. return newstyles;
  103. },
  104. /**
  105. * Copies an object literal.
  106. *
  107. * @method _copyObject
  108. * @param {Object} obj Object literal to be copied.
  109. * @return Object
  110. * @private
  111. */
  112. _copyObject: function(obj) {
  113. var newObj = {},
  114. key,
  115. val;
  116. for(key in obj)
  117. {
  118. if(obj.hasOwnProperty(key))
  119. {
  120. val = obj[key];
  121. if(typeof val === "object" && !Y_Lang.isArray(val))
  122. {
  123. newObj[key] = this._copyObject(val);
  124. }
  125. else
  126. {
  127. newObj[key] = val;
  128. }
  129. }
  130. }
  131. return newObj;
  132. },
  133. /**
  134. * Gets the default value for the `styles` attribute.
  135. *
  136. * @method _getDefaultStyles
  137. * @return Object
  138. * @protected
  139. */
  140. _getDefaultStyles: function()
  141. {
  142. return {padding:{
  143. top:0,
  144. right: 0,
  145. bottom: 0,
  146. left: 0
  147. }};
  148. }
  149. };
  150. Y.augment(Renderer, Y.Attribute);
  151. Y.Renderer = Renderer;