source: Dev/branches/rest-dojo-ui/client/dojox/charting/plot2d/StackedBars.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 4.2 KB
Line 
1define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "./Bars", "./common",
2        "dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/functional/sequence"],
3        function(lang, arr, declare, Bars, dc, df, dfr, dfs){
4
5        var     purgeGroup = dfr.lambda("item.purgeGroup()");
6/*=====
7var bars = dojox.charting.plot2d.Bars;
8=====*/
9        return declare("dojox.charting.plot2d.StackedBars", Bars, {
10                //      summary:
11                //              The plot object representing a stacked bar chart (horizontal bars).
12                getSeriesStats: function(){
13                        //      summary:
14                        //              Calculate the min/max on all attached series in both directions.
15                        //      returns: Object
16                        //              {hmin, hmax, vmin, vmax} min/max in both directions.
17                        var stats = dc.collectStackedStats(this.series), t;
18                        this._maxRunLength = stats.hmax;
19                        stats.hmin -= 0.5;
20                        stats.hmax += 0.5;
21                        t = stats.hmin, stats.hmin = stats.vmin, stats.vmin = t;
22                        t = stats.hmax, stats.hmax = stats.vmax, stats.vmax = t;
23                        return stats;
24                },
25                render: function(dim, offsets){
26                        //      summary:
27                        //              Run the calculations for any axes for this plot.
28                        //      dim: Object
29                        //              An object in the form of { width, height }
30                        //      offsets: Object
31                        //              An object of the form { l, r, t, b}.
32                        //      returns: dojox.charting.plot2d.StackedBars
33                        //              A reference to this plot for functional chaining.
34                        if(this._maxRunLength <= 0){
35                                return this;
36                        }
37
38                        // stack all values
39                        var acc = df.repeat(this._maxRunLength, "-> 0", 0);
40                        for(var i = 0; i < this.series.length; ++i){
41                                var run = this.series[i];
42                                for(var j = 0; j < run.data.length; ++j){
43                                        var value = run.data[j];
44                                        if(value !== null){
45                                                var v = typeof value == "number" ? value : value.y;
46                                                if(isNaN(v)){ v = 0; }
47                                                acc[j] += v;
48                                        }
49                                }
50                        }
51                        // draw runs in backwards
52                        if(this.zoom && !this.isDataDirty()){
53                                return this.performZoom(dim, offsets);
54                        }
55                        this.resetEvents();
56                        this.dirty = this.isDirty();
57                        if(this.dirty){
58                                arr.forEach(this.series, purgeGroup);
59                                this._eventSeries = {};
60                                this.cleanGroup();
61                                var s = this.group;
62                                df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
63                        }
64                        var t = this.chart.theme, f, gap, height,
65                                ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
66                                vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
67                                events = this.events();
68                        f = dc.calculateBarSize(this._vScaler.bounds.scale, this.opt);
69                        gap = f.gap;
70                        height = f.size;
71                        for(var i = this.series.length - 1; i >= 0; --i){
72                                var run = this.series[i];
73                                if(!this.dirty && !run.dirty){
74                                        t.skip();
75                                        this._reconnectEvents(run.name);
76                                        continue;
77                                }
78                                run.cleanGroup();
79                                var theme = t.next("bar", [this.opt, run]), s = run.group,
80                                        eventSeries = new Array(acc.length);
81                                for(var j = 0; j < acc.length; ++j){
82                                        var value = run.data[j];
83                                        if(value !== null){
84                                                var v = acc[j],
85                                                        width = ht(v),
86                                                        finalTheme = typeof value != "number" ?
87                                                                t.addMixin(theme, "bar", value, true) :
88                                                                t.post(theme, "bar");
89                                                if(width >= 0 && height >= 1){
90                                                        var rect = {
91                                                                x: offsets.l,
92                                                                y: dim.height - offsets.b - vt(j + 1.5) + gap,
93                                                                width: width, height: height
94                                                        };
95                                                        var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets);
96                                                        specialFill = this._shapeFill(specialFill, rect);
97                                                        var shape = s.createRect(rect).setFill(specialFill).setStroke(finalTheme.series.stroke);
98                                                        run.dyn.fill   = shape.getFill();
99                                                        run.dyn.stroke = shape.getStroke();
100                                                        if(events){
101                                                                var o = {
102                                                                        element: "bar",
103                                                                        index:   j,
104                                                                        run:     run,
105                                                                        shape:   shape,
106                                                                        x:       v,
107                                                                        y:       j + 1.5
108                                                                };
109                                                                this._connectEvents(o);
110                                                                eventSeries[j] = o;
111                                                        }
112                                                        if(this.animate){
113                                                                this._animateBar(shape, offsets.l, -width);
114                                                        }
115                                                }
116                                        }
117                                }
118                                this._eventSeries[run.name] = eventSeries;
119                                run.dirty = false;
120                                // update the accumulator
121                                for(var j = 0; j < run.data.length; ++j){
122                                        var value = run.data[j];
123                                        if(value !== null){
124                                                var v = typeof value == "number" ? value : value.y;
125                                                if(isNaN(v)){ v = 0; }
126                                                acc[j] -= v;
127                                        }
128                                }
129                        }
130                        this.dirty = false;
131                        return this;    //      dojox.charting.plot2d.StackedBars
132                }
133        });
134});
Note: See TracBrowser for help on using the repository browser.