Legend.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.geo.charting.widget.Legend"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.geo.charting.widget.Legend"] = true;
  8. dojo.provide("dojox.geo.charting.widget.Legend");
  9. dojo.require("dijit._Widget");
  10. dojo.require("dijit._Templated");
  11. dojo.require("dojox.lang.functional.array");
  12. dojo.require("dojox.lang.functional.fold");
  13. dojo.declare("dojox.geo.charting.widget.Legend",[dijit._Widget, dijit._Templated], {
  14. templateString: "<table dojoAttachPoint='legendNode' class='dojoxLegendNode'><tbody dojoAttachPoint='legendBody'></tbody></table>",
  15. horizontal:true,
  16. legendNode:null,
  17. legendBody:null,
  18. swatchSize:18,
  19. postCreate: function(){
  20. if(!this.map){return;}
  21. this.series = this.map.series;
  22. dojo.byId(this.map.container).appendChild(this.domNode);
  23. this.refresh();
  24. },
  25. refresh:function(){
  26. // cleanup
  27. while(this.legendBody.lastChild){
  28. dojo.destroy(this.legendBody.lastChild);
  29. }
  30. if(this.horizontal){
  31. dojo.addClass(this.legendNode,"dojoxLegendHorizontal");
  32. this._tr = dojo.doc.createElement("tr");
  33. this.legendBody.appendChild(this._tr);
  34. }
  35. var s = this.series;
  36. if(s.length == 0){return;}
  37. dojo.forEach(s,function(x){
  38. this._addLabel(x.color, x.name);
  39. },this);
  40. },
  41. _addLabel:function(color,label){
  42. var icon = dojo.doc.createElement("td");
  43. var text = dojo.doc.createElement("td");
  44. var div = dojo.doc.createElement("div");
  45. dojo.addClass(icon, "dojoxLegendIcon");
  46. dojo.addClass(text, "dojoxLegendText");
  47. div.style.width = this.swatchSize + "px";
  48. div.style.height = this.swatchSize + "px";
  49. icon.appendChild(div);
  50. if(this.horizontal){
  51. this._tr.appendChild(icon);
  52. this._tr.appendChild(text);
  53. }else{
  54. var tr = dojo.doc.createElement("tr");
  55. this.legendBody.appendChild(tr);
  56. tr.appendChild(icon);
  57. tr.appendChild(text);
  58. }
  59. div.style.background = color;
  60. text.innerHTML = String(label);
  61. }
  62. });
  63. }