/* Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved. Available via Academic Free License >= 2.1 OR the modified BSD license. see: http://dojotoolkit.org/license for details */ if(!dojo._hasResource["dojox.charting.plot2d.Candlesticks"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. dojo._hasResource["dojox.charting.plot2d.Candlesticks"] = true; dojo.provide("dojox.charting.plot2d.Candlesticks"); dojo.require("dojox.charting.plot2d.common"); dojo.require("dojox.charting.plot2d.Base"); dojo.require("dojox.lang.utils"); dojo.require("dojox.lang.functional"); dojo.require("dojox.lang.functional.reversed"); (function(){ var df = dojox.lang.functional, du = dojox.lang.utils, dc = dojox.charting.plot2d.common, purgeGroup = df.lambda("item.purgeGroup()"); // Candlesticks are based on the Bars plot type; we expect the following passed // as values in a series: // { x?, open, close, high, low, mid? } // if x is not provided, the array index is used. // failing to provide the OHLC values will throw an error. dojo.declare("dojox.charting.plot2d.Candlesticks", dojox.charting.plot2d.Base, { // summary: // A plot that represents typical candlesticks (financial reporting, primarily). // Unlike most charts, the Candlestick expects data points to be represented by // an object of the form { x?, open, close, high, low, mid? }, where both // x and mid are optional parameters. If x is not provided, the index of the // data array is used. defaultParams: { hAxis: "x", // use a horizontal axis named "x" vAxis: "y", // use a vertical axis named "y" gap: 2, // gap between columns in pixels animate: null // animate bars into place }, optionalParams: { minBarSize: 1, // minimal candle width in pixels maxBarSize: 1, // maximal candle width in pixels // theme component stroke: {}, outline: {}, shadow: {}, fill: {}, font: "", fontColor: "" }, constructor: function(chart, kwArgs){ // summary: // The constructor for a candlestick chart. // chart: dojox.charting.Chart2D // The chart this plot belongs to. // kwArgs: dojox.charting.plot2d.__BarCtorArgs? // An optional keyword arguments object to help define the plot. this.opt = dojo.clone(this.defaultParams); du.updateWithObject(this.opt, kwArgs); du.updateWithPattern(this.opt, kwArgs, this.optionalParams); this.series = []; this.hAxis = this.opt.hAxis; this.vAxis = this.opt.vAxis; this.animate = this.opt.animate; }, collectStats: function(series){ // summary: // Collect all statistics for drawing this chart. Since the common // functionality only assumes x and y, Candlesticks must create it's own // stats (since data has no y value, but open/close/high/low instead). // series: dojox.charting.Series[] // The data series array to be drawn on this plot. // returns: Object // Returns an object in the form of { hmin, hmax, vmin, vmax }. // we have to roll our own, since we need to use all four passed // values to figure out our stats, and common only assumes x and y. var stats = dojo.delegate(dc.defaultStats); for(var i=0; i= 0; --i){ var run = this.series[i]; if(!this.dirty && !run.dirty){ t.skip(); this._reconnectEvents(run.name); continue; } run.cleanGroup(); var theme = t.next("candlestick", [this.opt, run]), s = run.group, eventSeries = new Array(run.data.length); for(var j = 0; j < run.data.length; ++j){ var v = run.data[j]; if(v !== null){ var finalTheme = t.addMixin(theme, "candlestick", v, true); // calculate the points we need for OHLC var x = ht(v.x || (j+0.5)) + offsets.l + gap, y = dim.height - offsets.b, open = vt(v.open), close = vt(v.close), high = vt(v.high), low = vt(v.low); if("mid" in v){ var mid = vt(v.mid); } if(low > high){ var tmp = high; high = low; low = tmp; } if(width >= 1){ // draw the line and rect, set up as a group and pass that to the events. var doFill = open > close; var line = { x1: width/2, x2: width/2, y1: y - high, y2: y - low }, rect = { x: 0, y: y-Math.max(open, close), width: width, height: Math.max(doFill ? open-close : close-open, 1) }; shape = s.createGroup(); shape.setTransform({dx: x, dy: 0 }); var inner = shape.createGroup(); inner.createLine(line).setStroke(finalTheme.series.stroke); inner.createRect(rect).setStroke(finalTheme.series.stroke). setFill(doFill ? finalTheme.series.fill : "white"); if("mid" in v){ // add the mid line. inner.createLine({ x1: (finalTheme.series.stroke.width||1), x2: width - (finalTheme.series.stroke.width || 1), y1: y - mid, y2: y - mid }).setStroke(doFill ? "white" : finalTheme.series.stroke); } // TODO: double check this. run.dyn.fill = finalTheme.series.fill; run.dyn.stroke = finalTheme.series.stroke; if(events){ var o = { element: "candlestick", index: j, run: run, shape: inner, x: x, y: y-Math.max(open, close), cx: width/2, cy: (y-Math.max(open, close)) + (Math.max(doFill ? open-close : close-open, 1)/2), width: width, height: Math.max(doFill ? open-close : close-open, 1), data: v }; this._connectEvents(o); eventSeries[j] = o; } } if(this.animate){ this._animateCandlesticks(shape, y - low, high - low); } } } this._eventSeries[run.name] = eventSeries; run.dirty = false; } this.dirty = false; return this; // dojox.charting.plot2d.Candlesticks }, _animateCandlesticks: function(shape, voffset, vsize){ dojox.gfx.fx.animateTransform(dojo.delegate({ shape: shape, duration: 1200, transform: [ {name: "translate", start: [0, voffset - (voffset/vsize)], end: [0, 0]}, {name: "scale", start: [1, 1/vsize], end: [1, 1]}, {name: "original"} ] }, this.animate)).play(); } }); })(); }