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

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