LegendItem.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-chart-LegendItem'>/**
  19. </span> * @class Ext.chart.LegendItem
  20. * A single item of a legend (marker plus label)
  21. */
  22. Ext.define('Ext.chart.LegendItem', {
  23. /* Begin Definitions */
  24. extend: 'Ext.draw.CompositeSprite',
  25. requires: ['Ext.chart.Shape'],
  26. /* End Definitions */
  27. // Position of the item, relative to the upper-left corner of the legend box
  28. x: 0,
  29. y: 0,
  30. zIndex: 500,
  31. // checks to make sure that a unit size follows the bold keyword in the font style value
  32. boldRe: /bold\s\d{1,}.*/i,
  33. constructor: function(config) {
  34. this.callParent(arguments);
  35. this.createLegend(config);
  36. },
  37. <span id='Ext-chart-LegendItem-method-createLegend'> /**
  38. </span> * Creates all the individual sprites for this legend item
  39. */
  40. createLegend: function(config) {
  41. var me = this,
  42. index = config.yFieldIndex,
  43. series = me.series,
  44. seriesType = series.type,
  45. idx = me.yFieldIndex,
  46. legend = me.legend,
  47. surface = me.surface,
  48. refX = legend.x + me.x,
  49. refY = legend.y + me.y,
  50. bbox, z = me.zIndex,
  51. markerConfig, label, mask,
  52. radius, toggle = false,
  53. seriesStyle = Ext.apply(series.seriesStyle, series.style);
  54. function getSeriesProp(name) {
  55. var val = series[name];
  56. return (Ext.isArray(val) ? val[idx] : val);
  57. }
  58. label = me.add('label', surface.add({
  59. type: 'text',
  60. x: 20,
  61. y: 0,
  62. zIndex: (z || 0) + 2,
  63. fill: legend.labelColor,
  64. font: legend.labelFont,
  65. text: getSeriesProp('title') || getSeriesProp('yField'),
  66. style: {
  67. 'cursor': 'pointer'
  68. }
  69. }));
  70. // Line series - display as short line with optional marker in the middle
  71. if (seriesType === 'line' || seriesType === 'scatter') {
  72. if(seriesType === 'line') {
  73. me.add('line', surface.add({
  74. type: 'path',
  75. path: 'M0.5,0.5L16.5,0.5',
  76. zIndex: (z || 0) + 2,
  77. &quot;stroke-width&quot;: series.lineWidth,
  78. &quot;stroke-linejoin&quot;: &quot;round&quot;,
  79. &quot;stroke-dasharray&quot;: series.dash,
  80. stroke: seriesStyle.stroke || series.getLegendColor(index) || '#000',
  81. style: {
  82. cursor: 'pointer'
  83. }
  84. }));
  85. }
  86. if (series.showMarkers || seriesType === 'scatter') {
  87. markerConfig = Ext.apply(series.markerStyle, series.markerConfig || {}, {
  88. fill: series.getLegendColor(index)
  89. });
  90. me.add('marker', Ext.chart.Shape[markerConfig.type](surface, {
  91. fill: markerConfig.fill,
  92. x: 8.5,
  93. y: 0.5,
  94. zIndex: (z || 0) + 2,
  95. radius: markerConfig.radius || markerConfig.size,
  96. style: {
  97. cursor: 'pointer'
  98. }
  99. }));
  100. }
  101. }
  102. // All other series types - display as filled box
  103. else {
  104. me.add('box', surface.add({
  105. type: 'rect',
  106. zIndex: (z || 0) + 2,
  107. x: 0,
  108. y: 0,
  109. width: 12,
  110. height: 12,
  111. fill: series.getLegendColor(index),
  112. style: {
  113. cursor: 'pointer'
  114. }
  115. }));
  116. }
  117. me.setAttributes({
  118. hidden: false
  119. }, true);
  120. bbox = me.getBBox();
  121. mask = me.add('mask', surface.add({
  122. type: 'rect',
  123. x: bbox.x,
  124. y: bbox.y,
  125. width: bbox.width || 20,
  126. height: bbox.height || 20,
  127. zIndex: (z || 0) + 1,
  128. fill: me.legend.boxFill,
  129. style: {
  130. 'cursor': 'pointer'
  131. }
  132. }));
  133. //add toggle listener
  134. me.on('mouseover', function() {
  135. label.setStyle({
  136. 'font-weight': 'bold'
  137. });
  138. mask.setStyle({
  139. 'cursor': 'pointer'
  140. });
  141. series._index = index;
  142. series.highlightItem();
  143. }, me);
  144. me.on('mouseout', function() {
  145. label.setStyle({
  146. 'font-weight': legend.labelFont &amp;&amp; me.boldRe.test(legend.labelFont) ? 'bold' : 'normal'
  147. });
  148. series._index = index;
  149. series.unHighlightItem();
  150. }, me);
  151. if (!series.visibleInLegend(index)) {
  152. toggle = true;
  153. label.setAttributes({
  154. opacity: 0.5
  155. }, true);
  156. }
  157. me.on('mousedown', function() {
  158. if (!toggle) {
  159. series.hideAll(index);
  160. label.setAttributes({
  161. opacity: 0.5
  162. }, true);
  163. } else {
  164. series.showAll(index);
  165. label.setAttributes({
  166. opacity: 1
  167. }, true);
  168. }
  169. toggle = !toggle;
  170. me.legend.chart.redraw();
  171. }, me);
  172. me.updatePosition({x:0, y:0}); //Relative to 0,0 at first so that the bbox is calculated correctly
  173. },
  174. <span id='Ext-chart-LegendItem-method-updatePosition'> /**
  175. </span> * Update the positions of all this item's sprites to match the root position
  176. * of the legend box.
  177. * @param {Object} relativeTo (optional) If specified, this object's 'x' and 'y' values will be used
  178. * as the reference point for the relative positioning. Defaults to the Legend.
  179. */
  180. updatePosition: function(relativeTo) {
  181. var me = this,
  182. items = me.items,
  183. ln = items.length,
  184. i = 0,
  185. item;
  186. if (!relativeTo) {
  187. relativeTo = me.legend;
  188. }
  189. for (; i &lt; ln; i++) {
  190. item = items[i];
  191. switch (item.type) {
  192. case 'text':
  193. item.setAttributes({
  194. x: 20 + relativeTo.x + me.x,
  195. y: relativeTo.y + me.y
  196. }, true);
  197. break;
  198. case 'rect':
  199. item.setAttributes({
  200. translate: {
  201. x: relativeTo.x + me.x,
  202. y: relativeTo.y + me.y - 6
  203. }
  204. }, true);
  205. break;
  206. default:
  207. item.setAttributes({
  208. translate: {
  209. x: relativeTo.x + me.x,
  210. y: relativeTo.y + me.y
  211. }
  212. }, true);
  213. }
  214. }
  215. }
  216. });
  217. </pre>
  218. </body>
  219. </html>