Debian Codegraph @

dojox/charting/plot2d/OHLC.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has", "./CartesianBase", "./_PlotEvents", "./common",
2 "dojox/lang/functional", "dojox/lang/utils", "dojox/gfx/fx"],
3 function(lang, arr, declare, 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 }
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.OHLC", [CartesianBase, _PlotEvents], {
11 // summary:
12 // A plot that represents typical open/high/low/close (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 chart to place
20 },
21 optionalParams: {
22 minBarSize: 1, // minimal bar size in pixels
23 maxBarSize: 1, // maximal bar size 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, OHLC 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/OHLC
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 if(this.dirty){
106 arr.forEach(this.series, dc.purgeGroup);
107 this._eventSeries = {};
108 this.cleanGroup();
109 var s = this.getGroup();
110 df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
111 }
112 var t = this.chart.theme, f, gap, width,
113 ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
114 vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
115 events = this.events();
116 f = dc.calculateBarSize(this._hScaler.bounds.scale, this.opt);
117 gap = f.gap;
118 width = f.size;
119 for(var i = 0; i < this.series.length; i++){
120 var run = this.series[i];
121 if(!this.dirty && !run.dirty){
122 t.skip();
123 this._reconnectEvents(run.name);
124 continue;
125 }
126 run.cleanGroup();
127 var theme = t.next("candlestick", [this.opt, run]), s = run.group,
128 eventSeries = new Array(run.data.length);
129 for(var j = 0; j < run.data.length; ++j){
130 var v = run.data[j];
131 if(!this.isNullValue(v)){
132 var finalTheme = t.addMixin(theme, "candlestick", v, true);
133
134 // calculate the points we need for OHLC
135 var x = ht(v.x || (j+0.5)) + offsets.l + gap,
136 y = dim.height - offsets.b,
137 open = vt(v.open),
138 close = vt(v.close),
139 high = vt(v.high),
140 low = vt(v.low);
141 if(low > high){
142 var tmp = high;
143 high = low;
144 low = tmp;
145 }
146
147 if(width >= 1){
148 var hl = {x1: width/2, x2: width/2, y1: y - high, y2: y - low},
149 op = {x1: 0, x2: ((width/2) + ((finalTheme.series.stroke ? finalTheme.series.stroke.width || 1 : 1)/2)), y1: y-open, y2: y-open},
150 cl = {x1: ((width/2) - ((finalTheme.series.stroke ? finalTheme.series.stroke.width || 1 : 1)/2)), x2: width, y1: y-close, y2: y-close};
151 var shape = s.createGroup();
152 shape.setTransform({dx: x, dy: 0});
153 var inner = shape.createGroup();
154 inner.createLine(hl).setStroke(finalTheme.series.stroke);
155 inner.createLine(op).setStroke(finalTheme.series.stroke);
156 inner.createLine(cl).setStroke(finalTheme.series.stroke);
157
158 // TODO: double check this.
159 run.dyn.stroke = finalTheme.series.stroke;
160 if(events){
161 var o = {
162 element: "candlestick",
163 index: j,
164 run: run,
165 shape: inner,
166 x: x,
167 y: y-Math.max(open, close),
168 cx: width/2,
169 cy: (y-Math.max(open, close)) + (Math.max(open > close ? open-close : close-open, 1)/2),
170 width: width,
171 height: Math.max(open > close ? open-close : close-open, 1),
172 data: v
173 };
174 this._connectEvents(o);
175 eventSeries[j] = o;
176 }
177 }
178 if(this.animate){
179 this._animateOHLC(shape, y - low, high - low);
180 }
181 }
182 }
183 this._eventSeries[run.name] = eventSeries;
184 run.dirty = false;
185 }
186 this.dirty = false;
187 // chart mirroring starts
188 if(has("dojo-bidi")){
189 this._checkOrientation(this.group, dim, offsets);
190 }
191 // chart mirroring ends
192 return this; // dojox/charting/plot2d/OHLC
193 },
194 _animateOHLC: function(shape, voffset, vsize){
195 fx.animateTransform(lang.delegate({
196 shape: shape,
197 duration: 1200,
198 transform: [
199 {name: "translate", start: [0, voffset - (voffset/vsize)], end: [0, 0]},
200 {name: "scale", start: [1, 1/vsize], end: [1, 1]},
201 {name: "original"}
202 ]
203 }, this.animate)).play();
204 }
205 });
206 });