Legend.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. define("dojox/geo/charting/widget/Legend", ["dojo/_base/kernel", "dojo/_base/lang","dojo/_base/array", "dojo/_base/declare","dojo/_base/html","dojo/dom",
  2. "dojo/dom-construct","dojo/dom-class", "dojo/_base/window", "dijit/_Widget"],
  3. function(dojo, lang, arr, declare, html,dom,domConstruct,domClass, win, Widget) {
  4. return declare("dojox.geo.charting.widget.Legend",Widget, {
  5. // summary:
  6. // A legend widget displaying association between colors and Feature value ranges.
  7. //
  8. // description:
  9. // This widget basically is a table comprising (icon,string) pairs, describing the color scheme
  10. // used for the map and its associated text descriptions.
  11. //
  12. // example:
  13. // | var legend = new dojox.geo.charting.widget.Legend({
  14. // | map: map
  15. // | });
  16. horizontal:true,
  17. legendBody:null,
  18. swatchSize:18,
  19. map:null,
  20. postCreate: function(){
  21. // summary:
  22. // inherited Dijit's postCreate function
  23. // tags:
  24. // protected
  25. if(!this.map){return;}
  26. this.series = this.map.series;
  27. if (!this.domNode.parentNode) {
  28. // compatibility with older version : add to map domNode if not already attached to a parentNode.
  29. dom.byId(this.map.container).appendChild(this.domNode);
  30. }
  31. this.refresh();
  32. },
  33. buildRendering: function(){
  34. // summary:
  35. // Construct the UI for this widget, creates the underlying real dojox.geo.charting.Map object.
  36. // tags:
  37. // protected
  38. this.domNode = domConstruct.create("table",
  39. {role: "group", "class": "dojoxLegendNode"});
  40. this.legendBody = domConstruct.create("tbody", null, this.domNode);
  41. this.inherited(arguments);
  42. },
  43. refresh:function(){
  44. // summary:
  45. // Refreshes this legend contents when Map series has changed.
  46. // cleanup
  47. while(this.legendBody.lastChild){
  48. domConstruct.destroy(this.legendBody.lastChild);
  49. }
  50. if(this.horizontal){
  51. domClass.add(this.domNode,"dojoxLegendHorizontal");
  52. this._tr = win.doc.createElement("tr");
  53. this.legendBody.appendChild(this._tr);
  54. }
  55. var s = this.series;
  56. if(s.length == 0){return;}
  57. arr.forEach(s,function(x){
  58. this._addLabel(x.color, x.name);
  59. },this);
  60. },
  61. _addLabel:function(color,label){
  62. var icon = win.doc.createElement("td");
  63. var text = win.doc.createElement("td");
  64. var div = win.doc.createElement("div");
  65. domClass.add(icon, "dojoxLegendIcon");
  66. domClass.add(text, "dojoxLegendText");
  67. div.style.width = this.swatchSize + "px";
  68. div.style.height = this.swatchSize + "px";
  69. icon.appendChild(div);
  70. if(this.horizontal){
  71. this._tr.appendChild(icon);
  72. this._tr.appendChild(text);
  73. }else{
  74. var tr = win.doc.createElement("tr");
  75. this.legendBody.appendChild(tr);
  76. tr.appendChild(icon);
  77. tr.appendChild(text);
  78. }
  79. div.style.background = color;
  80. text.innerHTML = String(label);
  81. }
  82. });
  83. });