Debian Codegraph @

dojox/charting/plot2d/Candlesticks.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has", "./CartesianBase", "./_PlotEvents", "./common",
2 "dojox/lang/functional", "dojox/lang/utils", "dojox/gfx/fx"],
3 function(lang, declare, arr, has, CartesianBase, _PlotEvents, dc, df, du, fx){
4
5 // Candlesticks are based on the Bars plot type; we expect the following passed
6 // as values in a series:
7 // { x?, open, close, high, low, mid? }
8 // if x is not provided, the array index is used.
9 // failing to provide the OHLC values will throw an error.
10 return declare("dojox.charting.plot2d.Candlesticks", [CartesianBase, _PlotEvents], {
11 // summary:
12 // A plot that represents typical candlesticks (financial reporting, primarily).
13 // Unlike most charts, the Candlestick expects data points to be represented by
14 // an object of the form { x?, open, close, high, low, mid? }, where both
15 // x and mid are optional parameters. If x is not provided, the index of the
16 // data array is used.
17 defaultParams: {
18 gap: 2, // gap between columns in pixels
19 animate: null // animate bars into place
20 },
21 optionalParams: {
22 minBarSize: 1, // minimal candle width in pixels
23 maxBarSize: 1, // maximal candle width in pixels
24 // theme component
25 stroke: {},
26 outline: {},
27 shadow: {},
28 fill: {},
29 font: "",
30 fontColor: ""
31 },
32
33 constructor: function(chart, kwArgs){
34 // summary:
35 // The constructor for a candlestick chart.
36 // chart: dojox/charting/Chart
37 // The chart this plot belongs to.
38 // kwArgs: dojox.charting.plot2d.__BarCtorArgs?
39 // An optional keyword arguments object to help define the plot.
40 this.opt = lang.clone(this.defaultParams);
41 du.updateWithObject(this.opt, kwArgs);
42 du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
43 this.animate = this.opt.animate;
44 },
45
46 collectStats: function(series){
47 // summary:
48 // Collect all statistics for drawing this chart. Since the common
49 // functionality only assumes x and y, Candlesticks must create it's own
50 // stats (since data has no y value, but open/close/high/low instead).
51 // series: dojox.charting.Series[]
52 // The data series array to be drawn on this plot.
53 // returns: Object
54 // Returns an object in the form of { hmin, hmax, vmin, vmax }.
55
56 // we have to roll our own, since we need to use all four passed
57 // values to figure out our stats, and common only assumes x and y.
58 var stats = lang.delegate(dc.defaultStats);
59 for(var i=0; i<series.length; i++){
60 var run = series[i];
61 if(!run.data.length){ continue; }
62 var old_vmin = stats.vmin, old_vmax = stats.vmax;
63 if(!("ymin" in run) || !("ymax" in run)){
64 arr.forEach(run.data, function(val, idx){
65 if(!this.isNullValue(val)){
66 var x = val.x || idx + 1;
67 stats.hmin = Math.min(stats.hmin, x);
68 stats.hmax = Math.max(stats.hmax, x);
69 stats.vmin = Math.min(stats.vmin, val.open, val.close, val.high, val.low);
70 stats.vmax = Math.max(stats.vmax, val.open, val.close, val.high, val.low);
71 }
72 }, this);
73 }
74 if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
75 if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
76 }
77 return stats; // Object
78 },
79
80 getSeriesStats: function(){
81 // summary:
82 // Calculate the min/max on all attached series in both directions.
83 // returns: Object
84 // {hmin, hmax, vmin, vmax} min/max in both directions.
85 var stats = this.collectStats(this.series);
86 stats.hmin -= 0.5;
87 stats.hmax += 0.5;
88 return stats; // Object
89 },
90
91 render: function(dim, offsets){
92 // summary:
93 // Run the calculations for any axes for this plot.
94 // dim: Object
95 // An object in the form of { width, height }
96 // offsets: Object
97 // An object of the form { l, r, t, b}.
98 // returns: dojox/charting/plot2d/Candlesticks
99 // A reference to this plot for functional chaining.
100 if(this.zoom && !this.isDataDirty()){
101 return this.performZoom(dim, offsets);
102 }
103 this.resetEvents();
104 this.dirty = this.isDirty();
105 var s;
106 if(this.dirty){
107 arr.forEach(this.series, dc.purgeGroup);
108 this._eventSeries = {};
109 this.cleanGroup();
110 s = this.getGroup();
111 df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
112 }
113 var t = this.chart.theme, f, gap, width,
114 ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
115 vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
116 events = this.events();
117 f = dc.calculateBarSize(this._hScaler.bounds.scale, this.opt);
118 gap = f.gap;
119 width = f.size;
120 for(var i = this.series.length - 1; i >= 0; --i){
121 var run = this.series[i];
122 if(!this.dirty && !run.dirty){
123 t.skip();
124 this._reconnectEvents(run.name);
125 continue;
126 }
127 run.cleanGroup();
128 var theme = t.next("candlestick", [this.opt, run]),
129 eventSeries = new Array(run.data.length);
130
131 if(run.hidden){
132 run.dyn.fill = theme.series.fill;
133 run.dyn.stroke = theme.series.stroke;
134 continue;
135 }
136 s = run.group;
137
138 for(var j = 0; j < run.data.length; ++j){
139 var v = run.data[j];
140 if(!this.isNullValue(v)){
141 var finalTheme = t.addMixin(theme, "candlestick", v, true);
142
143 // calculate the points we need for OHLC
144 var x = ht(v.x || (j+0.5)) + offsets.l + gap,
145 y = dim.height - offsets.b,
146 open = vt(v.open),
147 close = vt(v.close),
148 high = vt(v.high),
149 low = vt(v.low);
150 if("mid" in v){
151 var mid = vt(v.mid);
152 }
153 if(low > high){
154 var tmp = high;
155 high = low;
156 low = tmp;
157 }
158
159 if(width >= 1){
160 // draw the line and rect, set up as a group and pass that to the events.
161 var doFill = open > close;
162 var line = { x1: width/2, x2: width/2, y1: y - high, y2: y - low },
163 rect = {
164 x: 0, y: y-Math.max(open, close),
165 width: width, height: Math.max(doFill ? open-close : close-open, 1)
166 };
167 var shape = s.createGroup();
168 shape.setTransform({dx: x, dy: 0 });
169 var inner = shape.createGroup();
170 inner.createLine(line).setStroke(finalTheme.series.stroke);
171 inner.createRect(rect).setStroke(finalTheme.series.stroke).
172 setFill(doFill ? finalTheme.series.fill : "white");
173 if("mid" in v){
174 // add the mid line.
175 inner.createLine({
176 x1: (finalTheme.series.stroke ? finalTheme.series.stroke.width || 1 : 1), x2: width - (finalTheme.series.stroke ? finalTheme.series.stroke.width || 1 : 1),
177 y1: y - mid, y2: y - mid
178 }).setStroke(doFill ? "white" : finalTheme.series.stroke);
179 }
180
181 // TODO: double check this.
182 run.dyn.fill = finalTheme.series.fill;
183 run.dyn.stroke = finalTheme.series.stroke;
184 if(events){
185 var o = {
186 element: "candlestick",
187 index: j,
188 run: run,
189 shape: inner,
190 x: x,
191 y: y-Math.max(open, close),
192 cx: width/2,
193 cy: (y-Math.max(open, close)) + (Math.max(doFill ? open-close : close-open, 1)/2),
194 width: width,
195 height: Math.max(doFill ? open-close : close-open, 1),
196 data: v
197 };
198 this._connectEvents(o);
199 eventSeries[j] = o;
200 }
201 }
202 if(this.animate){
203 this._animateCandlesticks(shape, y - low, high - low);
204 }
205 }
206 }
207 this._eventSeries[run.name] = eventSeries;
208 run.dirty = false;
209 }
210 this.dirty = false;
211 // chart mirroring starts
212 if(has("dojo-bidi")){
213 this._checkOrientation(this.group, dim, offsets);
214 }
215 // chart mirroring ends
216 return this; // dojox/charting/plot2d/Candlesticks
217 },
218
219 tooltipFunc: function(o){
220 return '<table cellpadding="1" cellspacing="0" border="0" style="font-size:0.9em;">'
221 + '<tr><td>Open:</td><td align="right"><strong>' + o.data.open + '</strong></td></tr>'
222 + '<tr><td>High:</td><td align="right"><strong>' + o.data.high + '</strong></td></tr>'
223 + '<tr><td>Low:</td><td align="right"><strong>' + o.data.low + '</strong></td></tr>'
224 + '<tr><td>Close:</td><td align="right"><strong>' + o.data.close + '</strong></td></tr>'
225 + (o.data.mid !== undefined ? '<tr><td>Mid:</td><td align="right"><strong>' + o.data.mid + '</strong></td></tr>' : '')
226 + '</table>';
227 },
228
229 _animateCandlesticks: function(shape, voffset, vsize){
230 fx.animateTransform(lang.delegate({
231 shape: shape,
232 duration: 1200,
233 transform: [
234 {name: "translate", start: [0, voffset - (voffset/vsize)], end: [0, 0]},
235 {name: "scale", start: [1, 1/vsize], end: [1, 1]},
236 {name: "original"}
237 ]
238 }, this.animate)).play();
239 }
240 });
241 });