| 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 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
return declare("dojox.charting.plot2d.OHLC", [CartesianBase, _PlotEvents], { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
defaultParams: { |
| 18 |
gap: 2, |
| 19 |
animate: null |
| 20 |
}, |
| 21 |
optionalParams: { |
| 22 |
minBarSize: 1, |
| 23 |
maxBarSize: 1, |
| 24 |
|
| 25 |
stroke: {}, |
| 26 |
outline: {}, |
| 27 |
shadow: {}, |
| 28 |
fill: {}, |
| 29 |
font: "", |
| 30 |
fontColor: "" |
| 31 |
}, |
| 32 |
|
| 33 |
constructor: function(chart, kwArgs){ |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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; |
| 78 |
}, |
| 79 |
|
| 80 |
getSeriesStats: function(){ |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
var stats = this.collectStats(this.series); |
| 86 |
stats.hmin -= 0.5; |
| 87 |
stats.hmax += 0.5; |
| 88 |
return stats; |
| 89 |
}, |
| 90 |
|
| 91 |
render: function(dim, offsets){ |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 188 |
if(has("dojo-bidi")){ |
| 189 |
this._checkOrientation(this.group, dim, offsets); |
| 190 |
} |
| 191 |
|
| 192 |
return this; |
| 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 |
}); |