summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/geo/charting/widget/Legend.js
blob: f307d7750b649f1e49b358d2c2dec19f97f7d695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//>>built

define("dojox/geo/charting/widget/Legend", ["dojo/_base/kernel", "dojo/_base/lang","dojo/_base/array", "dojo/_base/declare","dojo/_base/html","dojo/dom",
		 "dojo/dom-construct","dojo/dom-class", "dojo/_base/window", "dijit/_Widget"],
							function(dojo, lang, arr, declare, html,dom,domConstruct,domClass, win, Widget) {

return declare("dojox.geo.charting.widget.Legend",Widget, {
	// summary:
	//		A legend widget displaying association between colors and Feature value ranges. 
	//
	//	description:
	//		This widget basically is a table comprising (icon,string) pairs, describing the color scheme
	//		used for the map and its associated text descriptions.	 
	// 
	
	//	example:
	// |	var legend = new dojox.geo.charting.widget.Legend({
	// |		map: map
	// |	});
	horizontal:true,
	legendBody:null,
	swatchSize:18,
	map:null,
	postCreate: function(){
		//	summary:
		//		inherited Dijit's postCreate function		
		//	tags:
		//		protected
		if(!this.map){return;}
		this.series = this.map.series;
		if (!this.domNode.parentNode) {
			// compatibility with older version : add to map domNode if not already attached to a parentNode.
			dom.byId(this.map.container).appendChild(this.domNode);
		}
		this.refresh();
	},
	buildRendering: function(){ 
		//	summary:
		//		Construct the UI for this widget, creates the underlying real dojox.geo.charting.Map object.		
		//	tags:
		//		protected
		this.domNode = domConstruct.create("table",   
					{role: "group", "class": "dojoxLegendNode"});  
		this.legendBody = domConstruct.create("tbody", null, this.domNode);  
		this.inherited(arguments);  
 	},  

	refresh:function(){
		//	summary:
		//		Refreshes this legend contents when Map series has changed.		
		// cleanup
		while(this.legendBody.lastChild){
			domConstruct.destroy(this.legendBody.lastChild);
		}
		
		if(this.horizontal){
			domClass.add(this.domNode,"dojoxLegendHorizontal");
			this._tr = win.doc.createElement("tr");
			this.legendBody.appendChild(this._tr);
		}
		
		var s = this.series;
		if(s.length == 0){return;}
		
		arr.forEach(s,function(x){
			this._addLabel(x.color, x.name);
		},this);
	},
	_addLabel:function(color,label){
		var icon = win.doc.createElement("td");
		var text = win.doc.createElement("td");
		var div = win.doc.createElement("div");
		domClass.add(icon, "dojoxLegendIcon");
		domClass.add(text, "dojoxLegendText");
		div.style.width  = this.swatchSize + "px";
		div.style.height = this.swatchSize + "px";
		icon.appendChild(div);
		
		if(this.horizontal){
			this._tr.appendChild(icon);
			this._tr.appendChild(text);
		}else{
			var tr = win.doc.createElement("tr");
			this.legendBody.appendChild(tr);
			tr.appendChild(icon);
			tr.appendChild(text);
		}
		
		div.style.background = color;
		text.innerHTML = String(label);
	}
});
});