root / trunk / web / dojo / dojox / charting / widget / Legend.js
History | View | Annotate | Download (3.53 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, 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 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.charting.widget.Legend"]){ |
||
9 | dojo._hasResource["dojox.charting.widget.Legend"]=true; |
||
10 | dojo.provide("dojox.charting.widget.Legend");
|
||
11 | dojo.require("dijit._Widget");
|
||
12 | dojo.require("dijit._Templated");
|
||
13 | dojo.require("dojox.lang.functional.array");
|
||
14 | dojo.require("dojox.lang.functional.fold");
|
||
15 | dojo.declare("dojox.charting.widget.Legend",[dijit._Widget,dijit._Templated],{chartRef:"",horizontal:true,swatchSize:18,templateString:"<table dojoAttachPoint='legendNode' class='dojoxLegendNode'><tbody dojoAttachPoint='legendBody'></tbody></table>",legendNode:null,legendBody:null,postCreate:function(){ |
||
16 | if(!this.chart){ |
||
17 | if(!this.chartRef){ |
||
18 | return;
|
||
19 | } |
||
20 | this.chart=dijit.byId(this.chartRef); |
||
21 | if(!this.chart){ |
||
22 | var _1=dojo.byId(this.chartRef); |
||
23 | if(_1){
|
||
24 | this.chart=dijit.byNode(_1);
|
||
25 | }else{
|
||
26 | return;
|
||
27 | } |
||
28 | } |
||
29 | this.series=this.chart.chart.series; |
||
30 | }else{
|
||
31 | this.series=this.chart.series; |
||
32 | } |
||
33 | this.refresh();
|
||
34 | },refresh:function(){ |
||
35 | var df=dojox.lang.functional;
|
||
36 | if(this._surfaces){ |
||
37 | dojo.forEach(this._surfaces,function(_2){ |
||
38 | _2.destroy(); |
||
39 | }); |
||
40 | } |
||
41 | this._surfaces=[];
|
||
42 | while(this.legendBody.lastChild){ |
||
43 | dojo.destroy(this.legendBody.lastChild);
|
||
44 | } |
||
45 | if(this.horizontal){ |
||
46 | dojo.addClass(this.legendNode,"dojoxLegendHorizontal"); |
||
47 | this._tr=dojo.doc.createElement("tr"); |
||
48 | this.legendBody.appendChild(this._tr); |
||
49 | this._inrow=0; |
||
50 | } |
||
51 | var s=this.series; |
||
52 | if(s.length==0){ |
||
53 | return;
|
||
54 | } |
||
55 | if(s[0].chart.stack[0].declaredClass=="dojox.charting.plot2d.Pie"){ |
||
56 | var t=s[0].chart.stack[0]; |
||
57 | if(typeof t.run.data[0]=="number"){ |
||
58 | var _3=df.map(t.run.data,"Math.max(x, 0)"); |
||
59 | if(df.every(_3,"<= 0")){ |
||
60 | return;
|
||
61 | } |
||
62 | var _4=df.map(_3,"/this",df.foldl(_3,"+",0)); |
||
63 | dojo.forEach(_4,function(x,i){
|
||
64 | this._addLabel(t.dyn[i],t._getLabel(x*100)+"%"); |
||
65 | },this);
|
||
66 | }else{
|
||
67 | dojo.forEach(t.run.data,function(x,i){
|
||
68 | this._addLabel(t.dyn[i],x.legend||x.text||x.y);
|
||
69 | },this);
|
||
70 | } |
||
71 | }else{
|
||
72 | dojo.forEach(s,function(x){
|
||
73 | this._addLabel(x.dyn,x.legend||x.name);
|
||
74 | },this);
|
||
75 | } |
||
76 | },_addLabel:function(_5,_6){ |
||
77 | var _7=dojo.doc.createElement("td"),_8=dojo.doc.createElement("td"),_9=dojo.doc.createElement("div"); |
||
78 | dojo.addClass(_7,"dojoxLegendIcon");
|
||
79 | dojo.addClass(_8,"dojoxLegendText");
|
||
80 | _9.style.width=this.swatchSize+"px"; |
||
81 | _9.style.height=this.swatchSize+"px"; |
||
82 | _7.appendChild(_9); |
||
83 | if(this._tr){ |
||
84 | this._tr.appendChild(_7);
|
||
85 | this._tr.appendChild(_8);
|
||
86 | if(++this._inrow===this.horizontal){ |
||
87 | this._tr=dojo.doc.createElement("tr"); |
||
88 | this.legendBody.appendChild(this._tr); |
||
89 | this._inrow=0; |
||
90 | } |
||
91 | }else{
|
||
92 | var tr=dojo.doc.createElement("tr"); |
||
93 | this.legendBody.appendChild(tr);
|
||
94 | tr.appendChild(_7); |
||
95 | tr.appendChild(_8); |
||
96 | } |
||
97 | this._makeIcon(_9,_5);
|
||
98 | _8.innerHTML=String(_6); |
||
99 | },_makeIcon:function(_a,_b){ |
||
100 | var mb={h:this.swatchSize,w:this.swatchSize}; |
||
101 | var _c=dojox.gfx.createSurface(_a,mb.w,mb.h);
|
||
102 | this._surfaces.push(_c);
|
||
103 | if(_b.fill){
|
||
104 | _c.createRect({x:2,y:2,width:mb.w-4,height:mb.h-4}).setFill(_b.fill).setStroke(_b.stroke); |
||
105 | }else{
|
||
106 | if(_b.stroke||_b.marker){
|
||
107 | var _d={x1:0,y1:mb.h/2,x2:mb.w,y2:mb.h/2}; |
||
108 | if(_b.stroke){
|
||
109 | _c.createLine(_d).setStroke(_b.stroke); |
||
110 | } |
||
111 | if(_b.marker){
|
||
112 | var c={x:mb.w/2,y:mb.h/2}; |
||
113 | if(_b.stroke){
|
||
114 | _c.createPath({path:"M"+c.x+" "+c.y+" "+_b.marker}).setFill(_b.stroke.color).setStroke(_b.stroke); |
||
115 | }else{
|
||
116 | _c.createPath({path:"M"+c.x+" "+c.y+" "+_b.marker}).setFill(_b.color).setStroke(_b.color); |
||
117 | } |
||
118 | } |
||
119 | }else{
|
||
120 | _c.createRect({x:2,y:2,width:mb.w-4,height:mb.h-4}).setStroke("black"); |
||
121 | _c.createLine({x1:2,y1:2,x2:mb.w-2,y2:mb.h-2}).setStroke("black"); |
||
122 | _c.createLine({x1:2,y1:mb.h-2,x2:mb.w-2,y2:2}).setStroke("black"); |
||
123 | } |
||
124 | } |
||
125 | }}); |
||
126 | } |