source: Dev/branches/rest-dojo-ui/client/dojox/charting/plot2d/Bars.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: 6.2 KB
Line 
1define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "./Base", "./common",
2        "dojox/gfx/fx", "dojox/lang/utils", "dojox/lang/functional", "dojox/lang/functional/reversed"],
3        function(dojo, lang, arr, declare, Base, dc, fx, du, df, dfr){
4               
5        /*=====
6        dojo.declare("dojox.charting.plot2d.__BarCtorArgs", dojox.charting.plot2d.__DefaultCtorArgs, {
7                //      summary:
8                //              Additional keyword arguments for bar charts.
9       
10                //      minBarSize: Number?
11                //              The minimum size for a bar in pixels.  Default is 1.
12                minBarSize: 1,
13       
14                //      maxBarSize: Number?
15                //              The maximum size for a bar in pixels.  Default is 1.
16                maxBarSize: 1,
17               
18                //      enableCache: Boolean?
19                //              Whether the bars rect are cached from one rendering to another. This improves the rendering performance of
20                //              successive rendering but penalize the first rendering.  Default false.
21                enableCache: false
22        });
23        var Base = dojox.charting.plot2d.Base;
24        =====*/
25        var purgeGroup = dfr.lambda("item.purgeGroup()");
26
27        return declare("dojox.charting.plot2d.Bars", Base, {
28                //      summary:
29                //              The plot object representing a bar chart (horizontal bars).
30                defaultParams: {
31                        hAxis: "x",             // use a horizontal axis named "x"
32                        vAxis: "y",             // use a vertical axis named "y"
33                        gap:    0,              // gap between columns in pixels
34                        animate: null,   // animate bars into place
35                        enableCache: false
36                },
37                optionalParams: {
38                        minBarSize:     1,      // minimal bar width in pixels
39                        maxBarSize:     1,      // maximal bar width in pixels
40                        // theme component
41                        stroke:         {},
42                        outline:        {},
43                        shadow:         {},
44                        fill:           {},
45                        font:           "",
46                        fontColor:      ""
47                },
48
49                constructor: function(chart, kwArgs){
50                        //      summary:
51                        //              The constructor for a bar chart.
52                        //      chart: dojox.charting.Chart
53                        //              The chart this plot belongs to.
54                        //      kwArgs: dojox.charting.plot2d.__BarCtorArgs?
55                        //              An optional keyword arguments object to help define the plot.
56                        this.opt = lang.clone(this.defaultParams);
57                        du.updateWithObject(this.opt, kwArgs);
58                        du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
59                        this.series = [];
60                        this.hAxis = this.opt.hAxis;
61                        this.vAxis = this.opt.vAxis;
62                        this.animate = this.opt.animate;
63                },
64
65                getSeriesStats: function(){
66                        //      summary:
67                        //              Calculate the min/max on all attached series in both directions.
68                        //      returns: Object
69                        //              {hmin, hmax, vmin, vmax} min/max in both directions.
70                        var stats = dc.collectSimpleStats(this.series), t;
71                        stats.hmin -= 0.5;
72                        stats.hmax += 0.5;
73                        t = stats.hmin, stats.hmin = stats.vmin, stats.vmin = t;
74                        t = stats.hmax, stats.hmax = stats.vmax, stats.vmax = t;
75                        return stats;
76                },
77               
78                createRect: function(run, creator, params){
79                        var rect;
80                        if(this.opt.enableCache && run._rectFreePool.length > 0){
81                                rect = run._rectFreePool.pop();
82                                rect.setShape(params);
83                                // was cleared, add it back
84                                creator.add(rect);
85                        }else{
86                                rect = creator.createRect(params);
87                        }
88                        if(this.opt.enableCache){
89                                run._rectUsePool.push(rect);
90                        }
91                        return rect;
92                },
93
94                render: function(dim, offsets){
95                        //      summary:
96                        //              Run the calculations for any axes for this plot.
97                        //      dim: Object
98                        //              An object in the form of { width, height }
99                        //      offsets: Object
100                        //              An object of the form { l, r, t, b}.
101                        //      returns: dojox.charting.plot2d.Bars
102                        //              A reference to this plot for functional chaining.
103                        if(this.zoom && !this.isDataDirty()){
104                                return this.performZoom(dim, offsets);
105                        }
106                        this.dirty = this.isDirty();
107                        this.resetEvents();
108                        if(this.dirty){
109                                arr.forEach(this.series, purgeGroup);
110                                this._eventSeries = {};
111                                this.cleanGroup();
112                                var s = this.group;
113                                df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
114                        }
115                        var t = this.chart.theme, f, gap, height,
116                                ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
117                                vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
118                                baseline = Math.max(0, this._hScaler.bounds.lower),
119                                baselineWidth = ht(baseline),
120                                events = this.events();
121                        f = dc.calculateBarSize(this._vScaler.bounds.scale, this.opt);
122                        gap = f.gap;
123                        height = f.size;
124                        for(var i = this.series.length - 1; i >= 0; --i){
125                                var run = this.series[i];
126                                if(!this.dirty && !run.dirty){
127                                        t.skip();
128                                        this._reconnectEvents(run.name);
129                                        continue;
130                                }
131                                run.cleanGroup();
132                                if(this.opt.enableCache){
133                                        run._rectFreePool = (run._rectFreePool?run._rectFreePool:[]).concat(run._rectUsePool?run._rectUsePool:[]);
134                                        run._rectUsePool = [];
135                                }
136                                var theme = t.next("bar", [this.opt, run]), s = run.group,
137                                        eventSeries = new Array(run.data.length);
138                                for(var j = 0; j < run.data.length; ++j){
139                                        var value = run.data[j];
140                                        if(value !== null){
141                                                var v = typeof value == "number" ? value : value.y,
142                                                        hv = ht(v),
143                                                        width = hv - baselineWidth,
144                                                        w = Math.abs(width),
145                                                        finalTheme = typeof value != "number" ?
146                                                                t.addMixin(theme, "bar", value, true) :
147                                                                t.post(theme, "bar");
148                                                if(w >= 0 && height >= 1){
149                                                        var rect = {
150                                                                x: offsets.l + (v < baseline ? hv : baselineWidth),
151                                                                y: dim.height - offsets.b - vt(j + 1.5) + gap,
152                                                                width: w, height: height
153                                                        };
154                                                        var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets);
155                                                        specialFill = this._shapeFill(specialFill, rect);
156                                                        var shape = this.createRect(run, s, rect).setFill(specialFill).setStroke(finalTheme.series.stroke);
157                                                        run.dyn.fill   = shape.getFill();
158                                                        run.dyn.stroke = shape.getStroke();
159                                                        if(events){
160                                                                var o = {
161                                                                        element: "bar",
162                                                                        index:   j,
163                                                                        run:     run,
164                                                                        shape:   shape,
165                                                                        x:       v,
166                                                                        y:       j + 1.5
167                                                                };
168                                                                this._connectEvents(o);
169                                                                eventSeries[j] = o;
170                                                        }
171                                                        if(this.animate){
172                                                                this._animateBar(shape, offsets.l + baselineWidth, -w);
173                                                        }
174                                                }
175                                        }
176                                }
177                                this._eventSeries[run.name] = eventSeries;
178                                run.dirty = false;
179                        }
180                        this.dirty = false;
181                        return this;    //      dojox.charting.plot2d.Bars
182                },
183                _animateBar: function(shape, hoffset, hsize){
184                        fx.animateTransform(lang.delegate({
185                                shape: shape,
186                                duration: 1200,
187                                transform: [
188                                        {name: "translate", start: [hoffset - (hoffset/hsize), 0], end: [0, 0]},
189                                        {name: "scale", start: [1/hsize, 1], end: [1, 1]},
190                                        {name: "original"}
191                                ]
192                        }, this.animate)).play();
193                }
194        });
195});
Note: See TracBrowser for help on using the repository browser.