SelectableLegend.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. define("dojox/charting/widget/SelectableLegend", ["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/query", "dojo/_base/html",
  2. "dojo/_base/connect", "dojo/_base/Color", "./Legend", "dijit/form/CheckBox", "../action2d/Highlight",
  3. "dojox/lang/functional", "dojox/gfx/fx", "dojo/keys", "dojo/_base/event", "dojo/dom-construct",
  4. "dojo/dom-prop"],
  5. function(lang, arrayUtil, declare, query, html, hub, Color, Legend, CheckBox,
  6. Highlight, df, fx, keys, event, dom, domProp){
  7. /*=====
  8. var Legend = dojox.charting.widget.Legend;
  9. =====*/
  10. var FocusManager = declare(null, {
  11. // summary:
  12. // It will take legend as a tab stop, and using
  13. // cursor keys to navigate labels within the legend.
  14. constructor: function(legend){
  15. this.legend = legend;
  16. this.index = 0;
  17. this.horizontalLength = this._getHrizontalLength();
  18. arrayUtil.forEach(legend.legends, function(item, i){
  19. if(i > 0){
  20. query("input", item).attr("tabindex", -1);
  21. }
  22. });
  23. this.firstLabel = query("input", legend.legends[0])[0];
  24. hub.connect(this.firstLabel, "focus", this, function(){this.legend.active = true;});
  25. hub.connect(this.legend.domNode, "keydown", this, "_onKeyEvent");
  26. },
  27. _getHrizontalLength: function(){
  28. var horizontal = this.legend.horizontal;
  29. if(typeof horizontal == "number"){
  30. return Math.min(horizontal, this.legend.legends.length);
  31. }else if(!horizontal){
  32. return 1;
  33. }else{
  34. return this.legend.legends.length;
  35. }
  36. },
  37. _onKeyEvent: function(e){
  38. // if not focused
  39. if(!this.legend.active){
  40. return;
  41. }
  42. // lose focus
  43. if(e.keyCode == keys.TAB){
  44. this.legend.active = false;
  45. return;
  46. }
  47. // handle with arrow keys
  48. var max = this.legend.legends.length;
  49. switch(e.keyCode){
  50. case keys.LEFT_ARROW:
  51. this.index--;
  52. if(this.index < 0){
  53. this.index += max;
  54. }
  55. break;
  56. case keys.RIGHT_ARROW:
  57. this.index++;
  58. if(this.index >= max){
  59. this.index -= max;
  60. }
  61. break;
  62. case keys.UP_ARROW:
  63. if(this.index - this.horizontalLength >= 0){
  64. this.index -= this.horizontalLength;
  65. }
  66. break;
  67. case keys.DOWN_ARROW:
  68. if(this.index + this.horizontalLength < max){
  69. this.index += this.horizontalLength;
  70. }
  71. break;
  72. default:
  73. return;
  74. }
  75. this._moveToFocus();
  76. Event.stop(e);
  77. },
  78. _moveToFocus: function(){
  79. query("input", this.legend.legends[this.index])[0].focus();
  80. }
  81. });
  82. declare("dojox.charting.widget.SelectableLegend", Legend, {
  83. // summary:
  84. // An enhanced chart legend supporting interactive events on data series
  85. // theme component
  86. outline: false, // outline of vanished data series
  87. transitionFill: null, // fill of deselected data series
  88. transitionStroke: null, // stroke of deselected data series
  89. postCreate: function(){
  90. this.legends = [];
  91. this.legendAnim = {};
  92. this.inherited(arguments);
  93. },
  94. refresh: function(){
  95. this.legends = [];
  96. this.inherited(arguments);
  97. this._applyEvents();
  98. new FocusManager(this);
  99. },
  100. _addLabel: function(dyn, label){
  101. this.inherited(arguments);
  102. // create checkbox
  103. var legendNodes = query("td", this.legendBody);
  104. var currentLegendNode = legendNodes[legendNodes.length - 1];
  105. this.legends.push(currentLegendNode);
  106. var checkbox = new CheckBox({checked: true});
  107. dom.place(checkbox.domNode, currentLegendNode, "first");
  108. // connect checkbox and existed label
  109. var label = query("label", currentLegendNode)[0];
  110. domProp.set(label, "for", checkbox.id);
  111. },
  112. _applyEvents: function(){
  113. // summary:
  114. // Apply click-event on checkbox and hover-event on legend icon,
  115. // highlight data series or toggle it.
  116. // if the chart has not yet been refreshed it will crash here (targetData.group == null)
  117. if(this.chart.dirty){
  118. return;
  119. }
  120. arrayUtil.forEach(this.legends, function(legend, i){
  121. var targetData, shapes = [], plotName, seriesName;
  122. if(this._isPie()){
  123. targetData = this.chart.stack[0];
  124. shapes.push(targetData.group.children[i]);
  125. plotName = targetData.name;
  126. seriesName = this.chart.series[0].name;
  127. }else{
  128. targetData = this.chart.series[i];
  129. shapes = targetData.group.children;
  130. plotName = targetData.plot;
  131. seriesName = targetData.name;
  132. }
  133. var originalDyn = {
  134. fills : df.map(shapes, "x.getFill()"),
  135. strokes: df.map(shapes, "x.getStroke()")
  136. };
  137. // toggle action
  138. var legendCheckBox = query(".dijitCheckBox", legend)[0];
  139. hub.connect(legendCheckBox, "onclick", this, function(e){
  140. this._toggle(shapes, i, legend.vanished, originalDyn, seriesName, plotName);
  141. legend.vanished = !legend.vanished;
  142. e.stopPropagation();
  143. });
  144. // highlight action
  145. var legendIcon = query(".dojoxLegendIcon", legend)[0],
  146. iconShape = this._getFilledShape(this._surfaces[i].children);
  147. arrayUtil.forEach(["onmouseenter", "onmouseleave"], function(event){
  148. hub.connect(legendIcon, event, this, function(e){
  149. this._highlight(e, iconShape, shapes, i, legend.vanished, originalDyn, seriesName, plotName);
  150. });
  151. }, this);
  152. },this);
  153. },
  154. _toggle: function(shapes, index, isOff, dyn, seriesName, plotName){
  155. arrayUtil.forEach(shapes, function(shape, i){
  156. var startFill = dyn.fills[i],
  157. endFill = this._getTransitionFill(plotName),
  158. startStroke = dyn.strokes[i],
  159. endStroke = this.transitionStroke;
  160. if(startFill){
  161. if(endFill && (typeof startFill == "string" || startFill instanceof Color)){
  162. fx.animateFill({
  163. shape: shape,
  164. color: {
  165. start: isOff ? endFill : startFill,
  166. end: isOff ? startFill : endFill
  167. }
  168. }).play();
  169. }else{
  170. shape.setFill(isOff ? startFill : endFill);
  171. }
  172. }
  173. if(startStroke && !this.outline){
  174. shape.setStroke(isOff ? startStroke : endStroke);
  175. }
  176. }, this);
  177. },
  178. _highlight: function(e, iconShape, shapes, index, isOff, dyn, seriesName, plotName){
  179. if(!isOff){
  180. var anim = this._getAnim(plotName),
  181. isPie = this._isPie(),
  182. type = formatEventType(e.type);
  183. // highlight the label icon,
  184. var label = {
  185. shape: iconShape,
  186. index: isPie ? "legend" + index : "legend",
  187. run: {name: seriesName},
  188. type: type
  189. };
  190. anim.process(label);
  191. // highlight the data items
  192. arrayUtil.forEach(shapes, function(shape, i){
  193. shape.setFill(dyn.fills[i]);
  194. var o = {
  195. shape: shape,
  196. index: isPie ? index : i,
  197. run: {name: seriesName},
  198. type: type
  199. };
  200. anim.duration = 100;
  201. anim.process(o);
  202. });
  203. }
  204. },
  205. _getAnim: function(plotName){
  206. if(!this.legendAnim[plotName]){
  207. this.legendAnim[plotName] = new Highlight(this.chart, plotName);
  208. }
  209. return this.legendAnim[plotName];
  210. },
  211. _getTransitionFill: function(plotName){
  212. // Since series of stacked charts all start from the base line,
  213. // fill the "front" series with plotarea color to make it disappear .
  214. if(this.chart.stack[this.chart.plots[plotName]].declaredClass.indexOf("dojox.charting.plot2d.Stacked") != -1){
  215. return this.chart.theme.plotarea.fill;
  216. }
  217. return null;
  218. },
  219. _getFilledShape: function(shapes){
  220. // summary:
  221. // Get filled shape in legend icon which would be highlighted when hovered
  222. var i = 0;
  223. while(shapes[i]){
  224. if(shapes[i].getFill())return shapes[i];
  225. i++;
  226. }
  227. },
  228. _isPie: function(){
  229. return this.chart.stack[0].declaredClass == "dojox.charting.plot2d.Pie";
  230. }
  231. });
  232. function formatEventType(type){
  233. if(type == "mouseenter")return "onmouseover";
  234. if(type == "mouseleave")return "onmouseout";
  235. return "on" + type;
  236. }
  237. return dojox.charting.widget.SelectableLegend;
  238. });