source: [view]
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 var run = series[i];
if(!run.data.length){ continue; }
var old_vmin = stats.vmin, old_vmax = stats.vmax;
if(!("ymin" in run) || !("ymax" in run)){
dojo.forEach(run.data, function(val, idx){
if(val !== null){
var x = val.x || idx + 1;
stats.hmin = Math.min(stats.hmin, x);
stats.hmax = Math.max(stats.hmax, x);
stats.vmin = Math.min(stats.vmin, val.open, val.close, val.high, val.low);
stats.vmax = Math.max(stats.vmax, val.open, val.close, val.high, val.low);
}
});
}
if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
}
return stats; // Object