| 1 |
define(["dojo/_base/declare", "dijit/_WidgetBase", "dojox/gfx","dojo/_base/array", "dojo/has", "dojo/has!dojo-bidi?../bidi/widget/Legend", |
| 2 |
"dojox/lang/functional", "dojo/dom", "dojo/dom-construct", "dojo/dom-class","dijit/registry"], |
| 3 |
function(declare, _WidgetBase, gfx, arr, has, BidiLegend, df, |
| 4 |
dom, domConstruct, domClass, registry){ |
| 5 |
|
| 6 |
var Legend = declare(has("dojo-bidi")? "dojox.charting.widget.NonBidiLegend" : "dojox.charting.widget.Legend", _WidgetBase, { |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
chartRef: "", |
| 22 |
horizontal: true, |
| 23 |
swatchSize: 18, |
| 24 |
|
| 25 |
legendBody: null, |
| 26 |
|
| 27 |
postCreate: function(){ |
| 28 |
if(!this.chart && this.chartRef){ |
| 29 |
this.chart = registry.byId(this.chartRef) || registry.byNode(dom.byId(this.chartRef)); |
| 30 |
if(!this.chart){ |
| 31 |
console.log("Could not find chart instance with id: " + this.chartRef); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
this.chart = this.chart.chart || this.chart; |
| 36 |
this.refresh(); |
| 37 |
}, |
| 38 |
buildRendering: function(){ |
| 39 |
this.domNode = domConstruct.create("table", |
| 40 |
{role: "group", "aria-label": "chart legend", "class": "dojoxLegendNode"}); |
| 41 |
this.legendBody = domConstruct.create("tbody", null, this.domNode); |
| 42 |
this.inherited(arguments); |
| 43 |
}, |
| 44 |
destroy: function(){ |
| 45 |
if(this._surfaces){ |
| 46 |
arr.forEach(this._surfaces, function(surface){ |
| 47 |
surface.destroy(); |
| 48 |
}); |
| 49 |
} |
| 50 |
this.inherited(arguments); |
| 51 |
}, |
| 52 |
refresh: function(){ |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
if(this._surfaces){ |
| 58 |
arr.forEach(this._surfaces, function(surface){ |
| 59 |
surface.destroy(); |
| 60 |
}); |
| 61 |
} |
| 62 |
this._surfaces = []; |
| 63 |
while(this.legendBody.lastChild){ |
| 64 |
domConstruct.destroy(this.legendBody.lastChild); |
| 65 |
} |
| 66 |
|
| 67 |
if(this.horizontal){ |
| 68 |
domClass.add(this.domNode, "dojoxLegendHorizontal"); |
| 69 |
|
| 70 |
this._tr = domConstruct.create("tr", null, this.legendBody); |
| 71 |
this._inrow = 0; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
var s = this.series || this.chart.series; |
| 76 |
if(s.length == 0){ |
| 77 |
return; |
| 78 |
} |
| 79 |
if(s[0].chart.stack[0].declaredClass == "dojox.charting.plot2d.Pie"){ |
| 80 |
var t = s[0].chart.stack[0]; |
| 81 |
if(typeof t.run.data[0] == "number"){ |
| 82 |
var filteredRun = df.map(t.run.data, "Math.max(x, 0)"); |
| 83 |
var slices = df.map(filteredRun, "/this", df.foldl(filteredRun, "+", 0)); |
| 84 |
arr.forEach(slices, function(x, i){ |
| 85 |
this._addLabel(t.dyn[i], t._getLabel(x * 100) + "%"); |
| 86 |
}, this); |
| 87 |
}else{ |
| 88 |
arr.forEach(t.run.data, function(x, i){ |
| 89 |
this._addLabel(t.dyn[i], x.legend || x.text || x.y); |
| 90 |
}, this); |
| 91 |
} |
| 92 |
}else{ |
| 93 |
arr.forEach(s, function(x){ |
| 94 |
this._addLabel(x.dyn, x.legend || x.name); |
| 95 |
}, this); |
| 96 |
} |
| 97 |
}, |
| 98 |
_addLabel: function(dyn, label){ |
| 99 |
|
| 100 |
var wrapper = domConstruct.create("td"), |
| 101 |
icon = domConstruct.create("div", null, wrapper), |
| 102 |
text = domConstruct.create("label", null, wrapper), |
| 103 |
div = domConstruct.create("div", { |
| 104 |
style: { |
| 105 |
"width": this.swatchSize + "px", |
| 106 |
"height":this.swatchSize + "px", |
| 107 |
"float": "left" |
| 108 |
} |
| 109 |
}, icon); |
| 110 |
domClass.add(icon, "dojoxLegendIcon dijitInline"); |
| 111 |
domClass.add(text, "dojoxLegendText"); |
| 112 |
|
| 113 |
if(this._tr){ |
| 114 |
|
| 115 |
this._tr.appendChild(wrapper); |
| 116 |
if(++this._inrow === this.horizontal){ |
| 117 |
|
| 118 |
this._tr = domConstruct.create("tr", null, this.legendBody); |
| 119 |
this._inrow = 0; |
| 120 |
} |
| 121 |
}else{ |
| 122 |
|
| 123 |
var tr = domConstruct.create("tr", null, this.legendBody); |
| 124 |
tr.appendChild(wrapper); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
this._makeIcon(div, dyn); |
| 129 |
text.innerHTML = String(label); |
| 130 |
if(has("dojo-bidi")){ |
| 131 |
text.dir = this.getTextDir(label, text.dir); |
| 132 |
} |
| 133 |
}, |
| 134 |
_makeIcon: function(div, dyn){ |
| 135 |
var mb = { h: this.swatchSize, w: this.swatchSize }; |
| 136 |
var surface = gfx.createSurface(div, mb.w, mb.h); |
| 137 |
this._surfaces.push(surface); |
| 138 |
if(dyn.fill){ |
| 139 |
|
| 140 |
surface.createRect({x: 2, y: 2, width: mb.w - 4, height: mb.h - 4}). |
| 141 |
setFill(dyn.fill).setStroke(dyn.stroke); |
| 142 |
}else if(dyn.stroke || dyn.marker){ |
| 143 |
|
| 144 |
var line = {x1: 0, y1: mb.h / 2, x2: mb.w, y2: mb.h / 2}; |
| 145 |
if(dyn.stroke){ |
| 146 |
surface.createLine(line).setStroke(dyn.stroke); |
| 147 |
} |
| 148 |
if(dyn.marker){ |
| 149 |
|
| 150 |
var c = {x: mb.w / 2, y: mb.h / 2}; |
| 151 |
surface.createPath({path: "M" + c.x + " " + c.y + " " + dyn.marker}). |
| 152 |
setFill(dyn.markerFill).setStroke(dyn.markerStroke); |
| 153 |
} |
| 154 |
}else{ |
| 155 |
|
| 156 |
surface.createRect({x: 2, y: 2, width: mb.w - 4, height: mb.h - 4}). |
| 157 |
setStroke("black"); |
| 158 |
surface.createLine({x1: 2, y1: 2, x2: mb.w - 2, y2: mb.h - 2}).setStroke("black"); |
| 159 |
surface.createLine({x1: 2, y1: mb.h - 2, x2: mb.w - 2, y2: 2}).setStroke("black"); |
| 160 |
} |
| 161 |
} |
| 162 |
}); |
| 163 |
return has("dojo-bidi")? declare("dojox.charting.widget.Legend", [Legend, BidiLegend]) : Legend; |
| 164 |
|
| 165 |
}); |