1 | define(["dojo/_base/lang", "dojo/_base/html", "dojo/_base/declare", "dijit/_Widget", "dojox/gfx","dojo/_base/array", |
---|
2 | "dojox/lang/functional", "dojox/lang/functional/array", "dojox/lang/functional/fold", |
---|
3 | "dojo/dom", "dojo/dom-construct", "dojo/dom-class","dijit/_base/manager"], |
---|
4 | function(lang, html, declare, Widget, gfx, arrayUtil, df, dfa, dff, |
---|
5 | dom, domFactory, domClass, widgetManager){ |
---|
6 | /*===== |
---|
7 | var Widget = dijit._Widget; |
---|
8 | =====*/ |
---|
9 | |
---|
10 | var REVERSED_SERIES = /\.(StackedColumns|StackedAreas|ClusteredBars)$/; |
---|
11 | |
---|
12 | return declare("dojox.charting.widget.Legend", Widget, { |
---|
13 | // summary: A legend for a chart. A legend contains summary labels for |
---|
14 | // each series of data contained in the chart. |
---|
15 | // |
---|
16 | // Set the horizontal attribute to boolean false to layout legend labels vertically. |
---|
17 | // Set the horizontal attribute to a number to layout legend labels in horizontal |
---|
18 | // rows each containing that number of labels (except possibly the last row). |
---|
19 | // |
---|
20 | // (Line or Scatter charts (colored lines with shape symbols) ) |
---|
21 | // -o- Series1 -X- Series2 -v- Series3 |
---|
22 | // |
---|
23 | // (Area/Bar/Pie charts (letters represent colors)) |
---|
24 | // [a] Series1 [b] Series2 [c] Series3 |
---|
25 | |
---|
26 | chartRef: "", |
---|
27 | horizontal: true, |
---|
28 | swatchSize: 18, |
---|
29 | |
---|
30 | legendBody: null, |
---|
31 | |
---|
32 | postCreate: function(){ |
---|
33 | if(!this.chart){ |
---|
34 | if(!this.chartRef){ return; } |
---|
35 | this.chart = widgetManager.byId(this.chartRef); |
---|
36 | if(!this.chart){ |
---|
37 | var node = dom.byId(this.chartRef); |
---|
38 | if(node){ |
---|
39 | this.chart = widgetManager.byNode(node); |
---|
40 | }else{ |
---|
41 | console.log("Could not find chart instance with id: " + this.chartRef); |
---|
42 | return; |
---|
43 | } |
---|
44 | } |
---|
45 | this.series = this.chart.chart.series; |
---|
46 | }else{ |
---|
47 | this.series = this.chart.series; |
---|
48 | } |
---|
49 | |
---|
50 | this.refresh(); |
---|
51 | }, |
---|
52 | buildRendering: function(){ |
---|
53 | this.domNode = domFactory.create("table", |
---|
54 | {role: "group", "aria-label": "chart legend", "class": "dojoxLegendNode"}); |
---|
55 | this.legendBody = domFactory.create("tbody", null, this.domNode); |
---|
56 | this.inherited(arguments); |
---|
57 | }, |
---|
58 | refresh: function(){ |
---|
59 | // summary: regenerates the legend to reflect changes to the chart |
---|
60 | |
---|
61 | // cleanup |
---|
62 | if(this._surfaces){ |
---|
63 | arrayUtil.forEach(this._surfaces, function(surface){ |
---|
64 | surface.destroy(); |
---|
65 | }); |
---|
66 | } |
---|
67 | this._surfaces = []; |
---|
68 | while(this.legendBody.lastChild){ |
---|
69 | domFactory.destroy(this.legendBody.lastChild); |
---|
70 | } |
---|
71 | |
---|
72 | if(this.horizontal){ |
---|
73 | domClass.add(this.domNode, "dojoxLegendHorizontal"); |
---|
74 | // make a container <tr> |
---|
75 | this._tr = domFactory.create("tr", null, this.legendBody); |
---|
76 | this._inrow = 0; |
---|
77 | } |
---|
78 | |
---|
79 | var s = this.series; |
---|
80 | if(s.length == 0){ |
---|
81 | return; |
---|
82 | } |
---|
83 | if(s[0].chart.stack[0].declaredClass == "dojox.charting.plot2d.Pie"){ |
---|
84 | var t = s[0].chart.stack[0]; |
---|
85 | if(typeof t.run.data[0] == "number"){ |
---|
86 | var filteredRun = df.map(t.run.data, "Math.max(x, 0)"); |
---|
87 | if(df.every(filteredRun, "<= 0")){ |
---|
88 | return; |
---|
89 | } |
---|
90 | var slices = df.map(filteredRun, "/this", df.foldl(filteredRun, "+", 0)); |
---|
91 | arrayUtil.forEach(slices, function(x, i){ |
---|
92 | this._addLabel(t.dyn[i], t._getLabel(x * 100) + "%"); |
---|
93 | }, this); |
---|
94 | }else{ |
---|
95 | arrayUtil.forEach(t.run.data, function(x, i){ |
---|
96 | this._addLabel(t.dyn[i], x.legend || x.text || x.y); |
---|
97 | }, this); |
---|
98 | } |
---|
99 | }else{ |
---|
100 | if(this._isReversal()){ |
---|
101 | s = s.slice(0).reverse(); |
---|
102 | } |
---|
103 | arrayUtil.forEach(s, function(x){ |
---|
104 | this._addLabel(x.dyn, x.legend || x.name); |
---|
105 | }, this); |
---|
106 | } |
---|
107 | }, |
---|
108 | _addLabel: function(dyn, label){ |
---|
109 | // create necessary elements |
---|
110 | var wrapper = domFactory.create("td"), |
---|
111 | icon = domFactory.create("div", null, wrapper), |
---|
112 | text = domFactory.create("label", null, wrapper), |
---|
113 | div = domFactory.create("div", { |
---|
114 | style: { |
---|
115 | "width": this.swatchSize + "px", |
---|
116 | "height":this.swatchSize + "px", |
---|
117 | "float": "left" |
---|
118 | } |
---|
119 | }, icon); |
---|
120 | domClass.add(icon, "dojoxLegendIcon dijitInline"); |
---|
121 | domClass.add(text, "dojoxLegendText"); |
---|
122 | // create a skeleton |
---|
123 | if(this._tr){ |
---|
124 | // horizontal |
---|
125 | this._tr.appendChild(wrapper); |
---|
126 | if(++this._inrow === this.horizontal){ |
---|
127 | // make a fresh container <tr> |
---|
128 | this._tr = domFactory.create("tr", null, this.legendBody); |
---|
129 | this._inrow = 0; |
---|
130 | } |
---|
131 | }else{ |
---|
132 | // vertical |
---|
133 | var tr = domFactory.create("tr", null, this.legendBody); |
---|
134 | tr.appendChild(wrapper); |
---|
135 | } |
---|
136 | |
---|
137 | // populate the skeleton |
---|
138 | this._makeIcon(div, dyn); |
---|
139 | text.innerHTML = String(label); |
---|
140 | text.dir = this.getTextDir(label, text.dir); |
---|
141 | }, |
---|
142 | _makeIcon: function(div, dyn){ |
---|
143 | var mb = { h: this.swatchSize, w: this.swatchSize }; |
---|
144 | var surface = gfx.createSurface(div, mb.w, mb.h); |
---|
145 | this._surfaces.push(surface); |
---|
146 | if(dyn.fill){ |
---|
147 | // regions |
---|
148 | surface.createRect({x: 2, y: 2, width: mb.w - 4, height: mb.h - 4}). |
---|
149 | setFill(dyn.fill).setStroke(dyn.stroke); |
---|
150 | }else if(dyn.stroke || dyn.marker){ |
---|
151 | // draw line |
---|
152 | var line = {x1: 0, y1: mb.h / 2, x2: mb.w, y2: mb.h / 2}; |
---|
153 | if(dyn.stroke){ |
---|
154 | surface.createLine(line).setStroke(dyn.stroke); |
---|
155 | } |
---|
156 | if(dyn.marker){ |
---|
157 | // draw marker on top |
---|
158 | var c = {x: mb.w / 2, y: mb.h / 2}; |
---|
159 | if(dyn.stroke){ |
---|
160 | surface.createPath({path: "M" + c.x + " " + c.y + " " + dyn.marker}). |
---|
161 | setFill(dyn.stroke.color).setStroke(dyn.stroke); |
---|
162 | }else{ |
---|
163 | surface.createPath({path: "M" + c.x + " " + c.y + " " + dyn.marker}). |
---|
164 | setFill(dyn.color).setStroke(dyn.color); |
---|
165 | } |
---|
166 | } |
---|
167 | }else{ |
---|
168 | // nothing |
---|
169 | surface.createRect({x: 2, y: 2, width: mb.w - 4, height: mb.h - 4}). |
---|
170 | setStroke("black"); |
---|
171 | surface.createLine({x1: 2, y1: 2, x2: mb.w - 2, y2: mb.h - 2}).setStroke("black"); |
---|
172 | surface.createLine({x1: 2, y1: mb.h - 2, x2: mb.w - 2, y2: 2}).setStroke("black"); |
---|
173 | } |
---|
174 | }, |
---|
175 | _isReversal: function(){ |
---|
176 | return (!this.horizontal) && arrayUtil.some(this.chart.stack, function(item){ |
---|
177 | return REVERSED_SERIES.test(item.declaredClass); |
---|
178 | }); |
---|
179 | } |
---|
180 | }); |
---|
181 | }); |
---|