123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- (function($) {
-
- $.jqplot.OHLCRenderer = function(){
-
- $.jqplot.LineRenderer.call(this);
-
-
-
- this.candleStick = false;
-
-
-
-
- this.tickLength = 'auto';
-
-
-
- this.bodyWidth = 'auto';
-
-
- this.openColor = null;
-
-
- this.closeColor = null;
-
-
-
- this.wickColor = null;
-
-
-
- this.fillUpBody = false;
-
-
-
- this.fillDownBody = true;
-
-
- this.upBodyColor = null;
-
-
- this.downBodyColor = null;
-
-
-
- this.hlc = false;
-
-
-
- this.lineWidth = 1.5;
- this._tickLength;
- this._bodyWidth;
- };
-
- $.jqplot.OHLCRenderer.prototype = new $.jqplot.LineRenderer();
- $.jqplot.OHLCRenderer.prototype.constructor = $.jqplot.OHLCRenderer;
-
-
- $.jqplot.OHLCRenderer.prototype.init = function(options) {
- options = options || {};
-
-
-
- this.lineWidth = options.lineWidth || 1.5;
- $.jqplot.LineRenderer.prototype.init.call(this, options);
- this._type = 'ohlc';
-
- var db = this._yaxis._dataBounds;
- var d = this._plotData;
-
- if (d[0].length < 5) {
- this.renderer.hlc = true;
- for (var j=0; j<d.length; j++) {
- if (d[j][2] < db.min || db.min == null) {
- db.min = d[j][2];
- }
- if (d[j][1] > db.max || db.max == null) {
- db.max = d[j][1];
- }
- }
- }
- else {
- for (var j=0; j<d.length; j++) {
- if (d[j][3] < db.min || db.min == null) {
- db.min = d[j][3];
- }
- if (d[j][2] > db.max || db.max == null) {
- db.max = d[j][2];
- }
- }
- }
-
- };
-
-
- $.jqplot.OHLCRenderer.prototype.draw = function(ctx, gd, options) {
- var d = this.data;
- var xmin = this._xaxis.min;
- var xmax = this._xaxis.max;
-
- var xminidx = 0;
-
- var xmaxidx = d.length;
- var xp = this._xaxis.series_u2p;
- var yp = this._yaxis.series_u2p;
- var i, prevColor, ops, b, h, w, a, points;
- var o;
- var r = this.renderer;
- var opts = (options != undefined) ? options : {};
- var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
- var fill = (opts.fill != undefined) ? opts.fill : this.fill;
- var fillAndStroke = (opts.fillAndStroke != undefined) ? opts.fillAndStroke : this.fillAndStroke;
- r.bodyWidth = (opts.bodyWidth != undefined) ? opts.bodyWidth : r.bodyWidth;
- r.tickLength = (opts.tickLength != undefined) ? opts.tickLength : r.tickLength;
- ctx.save();
- if (this.show) {
- var x, open, hi, low, close;
-
-
-
- for (var i=0; i<d.length; i++) {
- if (d[i][0] < xmin) {
- xminidx = i;
- }
- else if (d[i][0] < xmax) {
- xmaxidx = i+1;
- }
- }
- var dwidth = this.gridData[xmaxidx-1][0] - this.gridData[xminidx][0];
- var nvisiblePoints = xmaxidx - xminidx;
- try {
- var dinterval = Math.abs(this._xaxis.series_u2p(parseInt(this._xaxis._intervalStats[0].sortedIntervals[0].interval, 10)) - this._xaxis.series_u2p(0));
- }
- catch (e) {
- var dinterval = dwidth / nvisiblePoints;
- }
-
- if (r.candleStick) {
- if (typeof(r.bodyWidth) == 'number') {
- r._bodyWidth = r.bodyWidth;
- }
- else {
- r._bodyWidth = Math.min(20, dinterval/1.65);
- }
- }
- else {
- if (typeof(r.tickLength) == 'number') {
- r._tickLength = r.tickLength;
- }
- else {
- r._tickLength = Math.min(10, dinterval/3.5);
- }
- }
-
- for (var i=xminidx; i<xmaxidx; i++) {
- x = xp(d[i][0]);
- if (r.hlc) {
- open = null;
- hi = yp(d[i][1]);
- low = yp(d[i][2]);
- close = yp(d[i][3]);
- }
- else {
- open = yp(d[i][1]);
- hi = yp(d[i][2]);
- low = yp(d[i][3]);
- close = yp(d[i][4]);
- }
- o = {};
- if (r.candleStick && !r.hlc) {
- w = r._bodyWidth;
- a = x - w/2;
-
-
-
- if (close < open) {
-
- if (r.wickColor) {
- o.color = r.wickColor;
- }
- else if (r.downBodyColor) {
- o.color = r.upBodyColor;
- }
- ops = $.extend(true, {}, opts, o);
- r.shapeRenderer.draw(ctx, [[x, hi], [x, close]], ops);
- r.shapeRenderer.draw(ctx, [[x, open], [x, low]], ops);
- o = {};
- b = close;
- h = open - close;
-
- if (r.fillUpBody) {
- o.fillRect = true;
- }
- else {
- o.strokeRect = true;
- w = w - this.lineWidth;
- a = x - w/2;
- }
- if (r.upBodyColor) {
- o.color = r.upBodyColor;
- o.fillStyle = r.upBodyColor;
- }
- points = [a, b, w, h];
- }
-
- else if (close > open) {
-
- if (r.wickColor) {
- o.color = r.wickColor;
- }
- else if (r.downBodyColor) {
- o.color = r.downBodyColor;
- }
- ops = $.extend(true, {}, opts, o);
- r.shapeRenderer.draw(ctx, [[x, hi], [x, open]], ops);
- r.shapeRenderer.draw(ctx, [[x, close], [x, low]], ops);
-
- o = {};
-
- b = open;
- h = close - open;
-
- if (r.fillDownBody) {
- o.fillRect = true;
- }
- else {
- o.strokeRect = true;
- w = w - this.lineWidth;
- a = x - w/2;
- }
- if (r.downBodyColor) {
- o.color = r.downBodyColor;
- o.fillStyle = r.downBodyColor;
- }
- points = [a, b, w, h];
- }
-
- else {
-
- if (r.wickColor) {
- o.color = r.wickColor;
- }
- ops = $.extend(true, {}, opts, o);
- r.shapeRenderer.draw(ctx, [[x, hi], [x, low]], ops);
- o = {};
- o.fillRect = false;
- o.strokeRect = false;
- a = [x - w/2, open];
- b = [x + w/2, close];
- w = null;
- h = null;
- points = [a, b];
- }
- ops = $.extend(true, {}, opts, o);
- r.shapeRenderer.draw(ctx, points, ops);
- }
- else {
- prevColor = opts.color;
- if (r.openColor) {
- opts.color = r.openColor;
- }
-
- if (!r.hlc) {
- r.shapeRenderer.draw(ctx, [[x-r._tickLength, open], [x, open]], opts);
- }
- opts.color = prevColor;
-
- if (r.wickColor) {
- opts.color = r.wickColor;
- }
- r.shapeRenderer.draw(ctx, [[x, hi], [x, low]], opts);
- opts.color = prevColor;
-
- if (r.closeColor) {
- opts.color = r.closeColor;
- }
- r.shapeRenderer.draw(ctx, [[x, close], [x+r._tickLength, close]], opts);
- opts.color = prevColor;
- }
- }
- }
-
- ctx.restore();
- };
-
- $.jqplot.OHLCRenderer.prototype.drawShadow = function(ctx, gd, options) {
-
- };
-
-
- $.jqplot.OHLCRenderer.checkOptions = function(target, data, options) {
-
-
- if (!options.highlighter) {
- options.highlighter = {
- showMarker:false,
- tooltipAxes: 'y',
- yvalues: 4,
- formatString:'<table class="jqplot-highlighter"><tr><td>date:</td><td>%s</td></tr><tr><td>open:</td><td>%s</td></tr><tr><td>hi:</td><td>%s</td></tr><tr><td>low:</td><td>%s</td></tr><tr><td>close:</td><td>%s</td></tr></table>'
- };
- }
- };
-
-
-
- })(jQuery);
|