Resources/js/flot-0.8.1.js
author Mahlon E. Smith <mahlon@martini.nu>
Mon, 23 Sep 2013 09:10:55 -0700
changeset 1 b3419d05eabb
parent 0 80c32ef237c6
permissions -rw-r--r--
Checkpoint. * Got fonts working cross platform by only using SVG. This will change with the new webkit engine when Tide 1.4 is released. * Use the Tide API accessors for cross-window data. * Make the ThingFish server object an observable, so models and their URI bases can be changed easily. * Add options for saving window state and position. * Contextual menu updates/tests for later.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
/* Javascript plotting library for jQuery, version 0.8.1.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
Copyright (c) 2007-2013 IOLA and Ole Laursen.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
Licensed under the MIT license.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
*/
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
// first an inline dependency, jquery.colorhelpers.js, we inline it here
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
// for convenience
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
/* Plugin for jQuery for working with colors.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
 *
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
 * Version 1.1.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
 *
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
 * Inspiration from jQuery color animation plugin by John Resig.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
 *
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
 * Released under the MIT license by Ole Laursen, October 2009.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
 *
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
 * Examples:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
 *
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
 *   $.color.parse("#fff").scale('rgb', 0.25).add('a', -0.5).toString()
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
 *   var c = $.color.extract($("#mydiv"), 'background-color');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
 *   console.log(c.r, c.g, c.b, c.a);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
 *   $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
 *
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
 * Note that .scale() and .add() return the same modified object
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
 * instead of making a new one.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
 *
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
 * V. 1.1: Fix error handling so e.g. parsing an empty string does
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
 * produce a color rather than just crashing.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
 */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
(function(B){B.color={};B.color.make=function(F,E,C,D){var G={};G.r=F||0;G.g=E||0;G.b=C||0;G.a=D!=null?D:1;G.add=function(J,I){for(var H=0;H<J.length;++H){G[J.charAt(H)]+=I}return G.normalize()};G.scale=function(J,I){for(var H=0;H<J.length;++H){G[J.charAt(H)]*=I}return G.normalize()};G.toString=function(){if(G.a>=1){return"rgb("+[G.r,G.g,G.b].join(",")+")"}else{return"rgba("+[G.r,G.g,G.b,G.a].join(",")+")"}};G.normalize=function(){function H(J,K,I){return K<J?J:(K>I?I:K)}G.r=H(0,parseInt(G.r),255);G.g=H(0,parseInt(G.g),255);G.b=H(0,parseInt(G.b),255);G.a=H(0,G.a,1);return G};G.clone=function(){return B.color.make(G.r,G.b,G.g,G.a)};return G.normalize()};B.color.extract=function(D,C){var E;do{E=D.css(C).toLowerCase();if(E!=""&&E!="transparent"){break}D=D.parent()}while(!B.nodeName(D.get(0),"body"));if(E=="rgba(0, 0, 0, 0)"){E="transparent"}return B.color.parse(E)};B.color.parse=function(F){var E,C=B.color.make;if(E=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(F)){return C(parseInt(E[1],10),parseInt(E[2],10),parseInt(E[3],10))}if(E=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(F)){return C(parseInt(E[1],10),parseInt(E[2],10),parseInt(E[3],10),parseFloat(E[4]))}if(E=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(F)){return C(parseFloat(E[1])*2.55,parseFloat(E[2])*2.55,parseFloat(E[3])*2.55)}if(E=/rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(F)){return C(parseFloat(E[1])*2.55,parseFloat(E[2])*2.55,parseFloat(E[3])*2.55,parseFloat(E[4]))}if(E=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(F)){return C(parseInt(E[1],16),parseInt(E[2],16),parseInt(E[3],16))}if(E=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(F)){return C(parseInt(E[1]+E[1],16),parseInt(E[2]+E[2],16),parseInt(E[3]+E[3],16))}var D=B.trim(F).toLowerCase();if(D=="transparent"){return C(255,255,255,0)}else{E=A[D]||[0,0,0];return C(E[0],E[1],E[2])}};var A={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
// the actual Flot code
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
(function($) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
	// Cache the prototype hasOwnProperty for faster access
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
	var hasOwnProperty = Object.prototype.hasOwnProperty;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
	///////////////////////////////////////////////////////////////////////////
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
	// The Canvas object is a wrapper around an HTML5 <canvas> tag.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
	// @constructor
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
	// @param {string} cls List of classes to apply to the canvas.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
	// @param {element} container Element onto which to append the canvas.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
	// Requiring a container is a little iffy, but unfortunately canvas
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
	// operations don't work unless the canvas is attached to the DOM.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
	function Canvas(cls, container) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
		var element = container.children("." + cls)[0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
		if (element == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
			element = document.createElement("canvas");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
			element.className = cls;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
			$(element).css({ direction: "ltr", position: "absolute", left: 0, top: 0 })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
				.appendTo(container);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
			// If HTML5 Canvas isn't available, fall back to [Ex|Flash]canvas
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
			if (!element.getContext) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
				if (window.G_vmlCanvasManager) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
					element = window.G_vmlCanvasManager.initElement(element);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
				} else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
					throw new Error("Canvas is not available. If you're using IE with a fall-back such as Excanvas, then there's either a mistake in your conditional include, or the page has no DOCTYPE and is rendering in Quirks Mode.");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
				}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
			}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
		this.element = element;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
		var context = this.context = element.getContext("2d");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
		// Determine the screen's ratio of physical to device-independent
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    79
		// pixels.  This is the ratio between the canvas width that the browser
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
		// advertises and the number of pixels actually present in that space.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
		// The iPhone 4, for example, has a device-independent width of 320px,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
		// but its screen is actually 640px wide.  It therefore has a pixel
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
		// ratio of 2, while most normal devices have a ratio of 1.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
		var devicePixelRatio = window.devicePixelRatio || 1,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
			backingStoreRatio =
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
				context.webkitBackingStorePixelRatio ||
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
				context.mozBackingStorePixelRatio ||
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
				context.msBackingStorePixelRatio ||
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
				context.oBackingStorePixelRatio ||
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
				context.backingStorePixelRatio || 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
		this.pixelRatio = devicePixelRatio / backingStoreRatio;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
		// Size the canvas to match the internal dimensions of its container
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    98
		this.resize(container.width(), container.height());
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
		// Collection of HTML div layers for text overlaid onto the canvas
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   102
		this.textContainer = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
		this.text = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
		// Cache of text fragments and metrics, so we can avoid expensively
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
		// re-calculating them when the plot is re-rendered in a loop.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
		this._textCache = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
	}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
	// Resizes the canvas to the given dimensions.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
	// @param {number} width New width of the canvas, in pixels.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
	// @param {number} width New height of the canvas, in pixels.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   116
	Canvas.prototype.resize = function(width, height) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   117
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   118
		if (width <= 0 || height <= 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
			throw new Error("Invalid dimensions for plot, width = " + width + ", height = " + height);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
		var element = this.element,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123
			context = this.context,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
			pixelRatio = this.pixelRatio;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   126
		// Resize the canvas, increasing its density based on the display's
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   127
		// pixel ratio; basically giving it more pixels without increasing the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
		// size of its element, to take advantage of the fact that retina
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
		// displays have that many more pixels in the same advertised space.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   131
		// Resizing should reset the state (excanvas seems to be buggy though)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133
		if (this.width != width) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   134
			element.width = width * pixelRatio;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   135
			element.style.width = width + "px";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
			this.width = width;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   138
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   139
		if (this.height != height) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   140
			element.height = height * pixelRatio;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141
			element.style.height = height + "px";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
			this.height = height;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   143
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   145
		// Save the context, so we can reset in case we get replotted.  The
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   146
		// restore ensure that we're really back at the initial state, and
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   147
		// should be safe even if we haven't saved the initial state yet.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   148
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   149
		context.restore();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   150
		context.save();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   151
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   152
		// Scale the coordinate space to match the display density; so even though we
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   153
		// may have twice as many pixels, we still want lines and other drawing to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   154
		// appear at the same size; the extra pixels will just make them crisper.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
		context.scale(pixelRatio, pixelRatio);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   157
	};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   158
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   159
	// Clears the entire canvas area, not including any overlaid HTML text
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   160
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   161
	Canvas.prototype.clear = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   162
		this.context.clearRect(0, 0, this.width, this.height);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   163
	};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
	// Finishes rendering the canvas, including managing the text overlay.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   166
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   167
	Canvas.prototype.render = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   169
		var cache = this._textCache;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   170
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
		// For each text layer, add elements marked as active that haven't
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   172
		// already been rendered, and remove those that are no longer active.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   173
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   174
		for (var layerKey in cache) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   175
			if (hasOwnProperty.call(cache, layerKey)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
				var layer = this.getTextLayer(layerKey),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   178
					layerCache = cache[layerKey];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   179
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   180
				layer.hide();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   181
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   182
				for (var styleKey in layerCache) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   183
					if (hasOwnProperty.call(layerCache, styleKey)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   184
						var styleCache = layerCache[styleKey];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   185
						for (var key in styleCache) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   186
							if (hasOwnProperty.call(styleCache, key)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   187
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   188
								var positions = styleCache[key].positions;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   189
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   190
								for (var i = 0, position; position = positions[i]; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   191
									if (position.active) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   192
										if (!position.rendered) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   193
											layer.append(position.element);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   194
											position.rendered = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   195
										}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   196
									} else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   197
										positions.splice(i--, 1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   198
										if (position.rendered) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   199
											position.element.detach();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   200
										}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   201
									}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   202
								}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   203
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   204
								if (positions.length == 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   205
									delete styleCache[key];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   206
								}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   207
							}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   208
						}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   209
					}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   210
				}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   211
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   212
				layer.show();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   213
			}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   214
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   215
	};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   216
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   217
	// Creates (if necessary) and returns the text overlay container.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   218
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   219
	// @param {string} classes String of space-separated CSS classes used to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   220
	//     uniquely identify the text layer.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   221
	// @return {object} The jQuery-wrapped text-layer div.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   222
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   223
	Canvas.prototype.getTextLayer = function(classes) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   224
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   225
		var layer = this.text[classes];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   226
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   227
		// Create the text layer if it doesn't exist
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   228
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   229
		if (layer == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   230
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   231
			// Create the text layer container, if it doesn't exist
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   232
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   233
			if (this.textContainer == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   234
				this.textContainer = $("<div class='flot-text'></div>")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   235
					.css({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   236
						position: "absolute",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   237
						top: 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   238
						left: 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   239
						bottom: 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   240
						right: 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   241
						'font-size': "smaller",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   242
						color: "#545454"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   243
					})
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   244
					.insertAfter(this.element);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   245
			}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   246
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   247
			layer = this.text[classes] = $("<div></div>")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   248
				.addClass(classes)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   249
				.css({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   250
					position: "absolute",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   251
					top: 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   252
					left: 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   253
					bottom: 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   254
					right: 0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   255
				})
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   256
				.appendTo(this.textContainer);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   257
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   258
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   259
		return layer;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   260
	};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   261
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   262
	// Creates (if necessary) and returns a text info object.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   263
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   264
	// The object looks like this:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   265
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   266
	// {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   267
	//     width: Width of the text's wrapper div.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   268
	//     height: Height of the text's wrapper div.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   269
	//     element: The jQuery-wrapped HTML div containing the text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   270
	//     positions: Array of positions at which this text is drawn.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   271
	// }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   272
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   273
	// The positions array contains objects that look like this:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   274
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   275
	// {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   276
	//     active: Flag indicating whether the text should be visible.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   277
	//     rendered: Flag indicating whether the text is currently visible.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   278
	//     element: The jQuery-wrapped HTML div containing the text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   279
	//     x: X coordinate at which to draw the text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   280
	//     y: Y coordinate at which to draw the text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   281
	// }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   282
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   283
	// Each position after the first receives a clone of the original element.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   284
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   285
	// The idea is that that the width, height, and general 'identity' of the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   286
	// text is constant no matter where it is placed; the placements are a
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   287
	// secondary property.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   288
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   289
	// Canvas maintains a cache of recently-used text info objects; getTextInfo
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   290
	// either returns the cached element or creates a new entry.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   291
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   292
	// @param {string} layer A string of space-separated CSS classes uniquely
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   293
	//     identifying the layer containing this text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   294
	// @param {string} text Text string to retrieve info for.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   295
	// @param {(string|object)=} font Either a string of space-separated CSS
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   296
	//     classes or a font-spec object, defining the text's font and style.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   297
	// @param {number=} angle Angle at which to rotate the text, in degrees.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   298
	//     Angle is currently unused, it will be implemented in the future.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   299
	// @param {number=} width Maximum width of the text before it wraps.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   300
	// @return {object} a text info object.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   301
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   302
	Canvas.prototype.getTextInfo = function(layer, text, font, angle, width) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   303
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   304
		var textStyle, layerCache, styleCache, info;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   305
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   306
		// Cast the value to a string, in case we were given a number or such
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   307
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   308
		text = "" + text;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   309
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   310
		// If the font is a font-spec object, generate a CSS font definition
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   311
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   312
		if (typeof font === "object") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   313
			textStyle = font.style + " " + font.variant + " " + font.weight + " " + font.size + "px/" + font.lineHeight + "px " + font.family;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   314
		} else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   315
			textStyle = font;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   316
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   317
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   318
		// Retrieve (or create) the cache for the text's layer and styles
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   319
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   320
		layerCache = this._textCache[layer];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   321
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   322
		if (layerCache == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   323
			layerCache = this._textCache[layer] = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   324
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   325
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   326
		styleCache = layerCache[textStyle];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   327
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   328
		if (styleCache == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   329
			styleCache = layerCache[textStyle] = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   330
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   331
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   332
		info = styleCache[text];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   333
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   334
		// If we can't find a matching element in our cache, create a new one
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   335
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   336
		if (info == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   337
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   338
			var element = $("<div></div>").html(text)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   339
				.css({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   340
					position: "absolute",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   341
					'max-width': width,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   342
					top: -9999
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   343
				})
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   344
				.appendTo(this.getTextLayer(layer));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   345
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   346
			if (typeof font === "object") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   347
				element.css({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   348
					font: textStyle,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   349
					color: font.color
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   350
				});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   351
			} else if (typeof font === "string") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   352
				element.addClass(font);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   353
			}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   354
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   355
			info = styleCache[text] = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   356
				width: element.outerWidth(true),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   357
				height: element.outerHeight(true),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   358
				element: element,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   359
				positions: []
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   360
			};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   361
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   362
			element.detach();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   363
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   364
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   365
		return info;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   366
	};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   367
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   368
	// Adds a text string to the canvas text overlay.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   369
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   370
	// The text isn't drawn immediately; it is marked as rendering, which will
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   371
	// result in its addition to the canvas on the next render pass.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   372
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   373
	// @param {string} layer A string of space-separated CSS classes uniquely
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   374
	//     identifying the layer containing this text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   375
	// @param {number} x X coordinate at which to draw the text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   376
	// @param {number} y Y coordinate at which to draw the text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   377
	// @param {string} text Text string to draw.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   378
	// @param {(string|object)=} font Either a string of space-separated CSS
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   379
	//     classes or a font-spec object, defining the text's font and style.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   380
	// @param {number=} angle Angle at which to rotate the text, in degrees.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   381
	//     Angle is currently unused, it will be implemented in the future.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   382
	// @param {number=} width Maximum width of the text before it wraps.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   383
	// @param {string=} halign Horizontal alignment of the text; either "left",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   384
	//     "center" or "right".
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   385
	// @param {string=} valign Vertical alignment of the text; either "top",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   386
	//     "middle" or "bottom".
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   387
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   388
	Canvas.prototype.addText = function(layer, x, y, text, font, angle, width, halign, valign) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   389
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   390
		var info = this.getTextInfo(layer, text, font, angle, width),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   391
			positions = info.positions;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   392
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   393
		// Tweak the div's position to match the text's alignment
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   394
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   395
		if (halign == "center") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   396
			x -= info.width / 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   397
		} else if (halign == "right") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   398
			x -= info.width;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   399
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   400
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   401
		if (valign == "middle") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   402
			y -= info.height / 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   403
		} else if (valign == "bottom") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   404
			y -= info.height;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   405
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   406
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   407
		// Determine whether this text already exists at this position.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   408
		// If so, mark it for inclusion in the next render pass.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   409
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   410
		for (var i = 0, position; position = positions[i]; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   411
			if (position.x == x && position.y == y) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   412
				position.active = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   413
				return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   414
			}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   415
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   416
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   417
		// If the text doesn't exist at this position, create a new entry
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   418
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   419
		// For the very first position we'll re-use the original element,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   420
		// while for subsequent ones we'll clone it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   421
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   422
		position = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   423
			active: true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   424
			rendered: false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   425
			element: positions.length ? info.element.clone() : info.element,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   426
			x: x,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   427
			y: y
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   428
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   429
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   430
		positions.push(position);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   431
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   432
		// Move the element to its final position within the container
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   433
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   434
		position.element.css({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   435
			top: Math.round(y),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   436
			left: Math.round(x),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   437
			'text-align': halign	// In case the text wraps
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   438
		});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   439
	};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   440
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   441
	// Removes one or more text strings from the canvas text overlay.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   442
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   443
	// If no parameters are given, all text within the layer is removed.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   444
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   445
	// Note that the text is not immediately removed; it is simply marked as
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   446
	// inactive, which will result in its removal on the next render pass.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   447
	// This avoids the performance penalty for 'clear and redraw' behavior,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   448
	// where we potentially get rid of all text on a layer, but will likely
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   449
	// add back most or all of it later, as when redrawing axes, for example.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   450
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   451
	// @param {string} layer A string of space-separated CSS classes uniquely
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   452
	//     identifying the layer containing this text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   453
	// @param {number=} x X coordinate of the text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   454
	// @param {number=} y Y coordinate of the text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   455
	// @param {string=} text Text string to remove.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   456
	// @param {(string|object)=} font Either a string of space-separated CSS
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   457
	//     classes or a font-spec object, defining the text's font and style.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   458
	// @param {number=} angle Angle at which the text is rotated, in degrees.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   459
	//     Angle is currently unused, it will be implemented in the future.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   460
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   461
	Canvas.prototype.removeText = function(layer, x, y, text, font, angle) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   462
		if (text == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   463
			var layerCache = this._textCache[layer];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   464
			if (layerCache != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   465
				for (var styleKey in layerCache) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   466
					if (hasOwnProperty.call(layerCache, styleKey)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   467
						var styleCache = layerCache[styleKey];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   468
						for (var key in styleCache) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   469
							if (hasOwnProperty.call(styleCache, key)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   470
								var positions = styleCache[key].positions;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   471
								for (var i = 0, position; position = positions[i]; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   472
									position.active = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   473
								}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   474
							}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   475
						}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   476
					}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   477
				}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   478
			}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   479
		} else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   480
			var positions = this.getTextInfo(layer, text, font, angle).positions;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   481
			for (var i = 0, position; position = positions[i]; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   482
				if (position.x == x && position.y == y) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   483
					position.active = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   484
				}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   485
			}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   486
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   487
	};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   488
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   489
	///////////////////////////////////////////////////////////////////////////
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   490
	// The top-level container for the entire plot.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   491
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   492
    function Plot(placeholder, data_, options_, plugins) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   493
        // data is on the form:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   494
        //   [ series1, series2 ... ]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   495
        // where series is either just the data as [ [x1, y1], [x2, y2], ... ]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   496
        // or { data: [ [x1, y1], [x2, y2], ... ], label: "some label", ... }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   497
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   498
        var series = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   499
            options = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   500
                // the color theme used for graphs
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   501
                colors: ["#edc240", "#afd8f8", "#cb4b4b", "#4da74d", "#9440ed"],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   502
                legend: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   503
                    show: true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   504
                    noColumns: 1, // number of colums in legend table
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   505
                    labelFormatter: null, // fn: string -> string
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   506
                    labelBoxBorderColor: "#ccc", // border color for the little label boxes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   507
                    container: null, // container (as jQuery object) to put legend in, null means default on top of graph
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   508
                    position: "ne", // position of default legend container within plot
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   509
                    margin: 5, // distance from grid edge to default legend container within plot
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   510
                    backgroundColor: null, // null means auto-detect
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   511
                    backgroundOpacity: 0.85, // set to 0 to avoid background
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   512
                    sorted: null    // default to no legend sorting
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   513
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   514
                xaxis: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   515
                    show: null, // null = auto-detect, true = always, false = never
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   516
                    position: "bottom", // or "top"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   517
                    mode: null, // null or "time"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   518
                    font: null, // null (derived from CSS in placeholder) or object like { size: 11, lineHeight: 13, style: "italic", weight: "bold", family: "sans-serif", variant: "small-caps" }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   519
                    color: null, // base color, labels, ticks
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   520
                    tickColor: null, // possibly different color of ticks, e.g. "rgba(0,0,0,0.15)"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   521
                    transform: null, // null or f: number -> number to transform axis
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   522
                    inverseTransform: null, // if transform is set, this should be the inverse function
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   523
                    min: null, // min. value to show, null means set automatically
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   524
                    max: null, // max. value to show, null means set automatically
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   525
                    autoscaleMargin: null, // margin in % to add if auto-setting min/max
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   526
                    ticks: null, // either [1, 3] or [[1, "a"], 3] or (fn: axis info -> ticks) or app. number of ticks for auto-ticks
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   527
                    tickFormatter: null, // fn: number -> string
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   528
                    labelWidth: null, // size of tick labels in pixels
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   529
                    labelHeight: null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   530
                    reserveSpace: null, // whether to reserve space even if axis isn't shown
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   531
                    tickLength: null, // size in pixels of ticks, or "full" for whole line
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   532
                    alignTicksWithAxis: null, // axis number or null for no sync
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   533
                    tickDecimals: null, // no. of decimals, null means auto
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   534
                    tickSize: null, // number or [number, "unit"]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   535
                    minTickSize: null // number or [number, "unit"]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   536
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   537
                yaxis: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   538
                    autoscaleMargin: 0.02,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   539
                    position: "left" // or "right"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   540
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   541
                xaxes: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   542
                yaxes: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   543
                series: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   544
                    points: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   545
                        show: false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   546
                        radius: 3,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   547
                        lineWidth: 2, // in pixels
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   548
                        fill: true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   549
                        fillColor: "#ffffff",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   550
                        symbol: "circle" // or callback
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   551
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   552
                    lines: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   553
                        // we don't put in show: false so we can see
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   554
                        // whether lines were actively disabled
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   555
                        lineWidth: 2, // in pixels
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   556
                        fill: false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   557
                        fillColor: null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   558
                        steps: false
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   559
                        // Omit 'zero', so we can later default its value to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   560
                        // match that of the 'fill' option.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   561
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   562
                    bars: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   563
                        show: false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   564
                        lineWidth: 2, // in pixels
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   565
                        barWidth: 1, // in units of the x axis
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   566
                        fill: true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   567
                        fillColor: null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   568
                        align: "left", // "left", "right", or "center"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   569
                        horizontal: false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   570
                        zero: true
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   571
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   572
                    shadowSize: 3,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   573
                    highlightColor: null
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   574
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   575
                grid: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   576
                    show: true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   577
                    aboveData: false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   578
                    color: "#545454", // primary color used for outline and labels
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   579
                    backgroundColor: null, // null for transparent, else color
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   580
                    borderColor: null, // set if different from the grid color
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   581
                    tickColor: null, // color for the ticks, e.g. "rgba(0,0,0,0.15)"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   582
                    margin: 0, // distance from the canvas edge to the grid
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   583
                    labelMargin: 5, // in pixels
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   584
                    axisMargin: 8, // in pixels
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   585
                    borderWidth: 2, // in pixels
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   586
                    minBorderMargin: null, // in pixels, null means taken from points radius
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   587
                    markings: null, // array of ranges or fn: axes -> array of ranges
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   588
                    markingsColor: "#f4f4f4",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   589
                    markingsLineWidth: 2,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   590
                    // interactive stuff
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   591
                    clickable: false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   592
                    hoverable: false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   593
                    autoHighlight: true, // highlight in case mouse is near
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   594
                    mouseActiveRadius: 10 // how far the mouse can be away to activate an item
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   595
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   596
                interaction: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   597
                    redrawOverlayInterval: 1000/60 // time between updates, -1 means in same flow
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   598
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   599
                hooks: {}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   600
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   601
        surface = null,     // the canvas for the plot itself
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   602
        overlay = null,     // canvas for interactive stuff on top of plot
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   603
        eventHolder = null, // jQuery object that events should be bound to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   604
        ctx = null, octx = null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   605
        xaxes = [], yaxes = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   606
        plotOffset = { left: 0, right: 0, top: 0, bottom: 0},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   607
        plotWidth = 0, plotHeight = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   608
        hooks = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   609
            processOptions: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   610
            processRawData: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   611
            processDatapoints: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   612
            processOffset: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   613
            drawBackground: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   614
            drawSeries: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   615
            draw: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   616
            bindEvents: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   617
            drawOverlay: [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   618
            shutdown: []
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   619
        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   620
        plot = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   621
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   622
        // public functions
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   623
        plot.setData = setData;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   624
        plot.setupGrid = setupGrid;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   625
        plot.draw = draw;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   626
        plot.getPlaceholder = function() { return placeholder; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   627
        plot.getCanvas = function() { return surface.element; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   628
        plot.getPlotOffset = function() { return plotOffset; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   629
        plot.width = function () { return plotWidth; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   630
        plot.height = function () { return plotHeight; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   631
        plot.offset = function () {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   632
            var o = eventHolder.offset();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   633
            o.left += plotOffset.left;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   634
            o.top += plotOffset.top;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   635
            return o;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   636
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   637
        plot.getData = function () { return series; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   638
        plot.getAxes = function () {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   639
            var res = {}, i;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   640
            $.each(xaxes.concat(yaxes), function (_, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   641
                if (axis)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   642
                    res[axis.direction + (axis.n != 1 ? axis.n : "") + "axis"] = axis;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   643
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   644
            return res;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   645
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   646
        plot.getXAxes = function () { return xaxes; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   647
        plot.getYAxes = function () { return yaxes; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   648
        plot.c2p = canvasToAxisCoords;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   649
        plot.p2c = axisToCanvasCoords;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   650
        plot.getOptions = function () { return options; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   651
        plot.highlight = highlight;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   652
        plot.unhighlight = unhighlight;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   653
        plot.triggerRedrawOverlay = triggerRedrawOverlay;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   654
        plot.pointOffset = function(point) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   655
            return {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   656
                left: parseInt(xaxes[axisNumber(point, "x") - 1].p2c(+point.x) + plotOffset.left, 10),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   657
                top: parseInt(yaxes[axisNumber(point, "y") - 1].p2c(+point.y) + plotOffset.top, 10)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   658
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   659
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   660
        plot.shutdown = shutdown;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   661
        plot.resize = function () {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   662
        	var width = placeholder.width(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   663
        		height = placeholder.height();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   664
            surface.resize(width, height);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   665
            overlay.resize(width, height);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   666
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   667
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   668
        // public attributes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   669
        plot.hooks = hooks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   670
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   671
        // initialize
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   672
        initPlugins(plot);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   673
        parseOptions(options_);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   674
        setupCanvases();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   675
        setData(data_);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   676
        setupGrid();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   677
        draw();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   678
        bindEvents();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   679
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   680
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   681
        function executeHooks(hook, args) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   682
            args = [plot].concat(args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   683
            for (var i = 0; i < hook.length; ++i)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   684
                hook[i].apply(this, args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   685
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   686
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   687
        function initPlugins() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   688
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   689
            // References to key classes, allowing plugins to modify them
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   690
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   691
            var classes = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   692
                Canvas: Canvas
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   693
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   694
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   695
            for (var i = 0; i < plugins.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   696
                var p = plugins[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   697
                p.init(plot, classes);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   698
                if (p.options)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   699
                    $.extend(true, options, p.options);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   700
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   701
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   702
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   703
        function parseOptions(opts) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   704
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   705
            $.extend(true, options, opts);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   706
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   707
            // $.extend merges arrays, rather than replacing them.  When less
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   708
            // colors are provided than the size of the default palette, we
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   709
            // end up with those colors plus the remaining defaults, which is
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   710
            // not expected behavior; avoid it by replacing them here.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   711
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   712
            if (opts && opts.colors) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   713
            	options.colors = opts.colors;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   714
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   715
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   716
            if (options.xaxis.color == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   717
                options.xaxis.color = $.color.parse(options.grid.color).scale('a', 0.22).toString();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   718
            if (options.yaxis.color == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   719
                options.yaxis.color = $.color.parse(options.grid.color).scale('a', 0.22).toString();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   720
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   721
            if (options.xaxis.tickColor == null) // grid.tickColor for back-compatibility
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   722
                options.xaxis.tickColor = options.grid.tickColor || options.xaxis.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   723
            if (options.yaxis.tickColor == null) // grid.tickColor for back-compatibility
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   724
                options.yaxis.tickColor = options.grid.tickColor || options.yaxis.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   725
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   726
            if (options.grid.borderColor == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   727
                options.grid.borderColor = options.grid.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   728
            if (options.grid.tickColor == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   729
                options.grid.tickColor = $.color.parse(options.grid.color).scale('a', 0.22).toString();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   730
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   731
            // Fill in defaults for axis options, including any unspecified
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   732
            // font-spec fields, if a font-spec was provided.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   733
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   734
            // If no x/y axis options were provided, create one of each anyway,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   735
            // since the rest of the code assumes that they exist.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   736
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   737
            var i, axisOptions, axisCount,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   738
                fontDefaults = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   739
                    style: placeholder.css("font-style"),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   740
                    size: Math.round(0.8 * (+placeholder.css("font-size").replace("px", "") || 13)),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   741
                    variant: placeholder.css("font-variant"),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   742
                    weight: placeholder.css("font-weight"),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   743
                    family: placeholder.css("font-family")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   744
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   745
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   746
            fontDefaults.lineHeight = fontDefaults.size * 1.15;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   747
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   748
            axisCount = options.xaxes.length || 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   749
            for (i = 0; i < axisCount; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   750
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   751
                axisOptions = options.xaxes[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   752
                if (axisOptions && !axisOptions.tickColor) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   753
                    axisOptions.tickColor = axisOptions.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   754
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   755
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   756
                axisOptions = $.extend(true, {}, options.xaxis, axisOptions);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   757
                options.xaxes[i] = axisOptions;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   758
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   759
                if (axisOptions.font) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   760
                    axisOptions.font = $.extend({}, fontDefaults, axisOptions.font);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   761
                    if (!axisOptions.font.color) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   762
                        axisOptions.font.color = axisOptions.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   763
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   764
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   765
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   766
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   767
            axisCount = options.yaxes.length || 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   768
            for (i = 0; i < axisCount; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   769
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   770
                axisOptions = options.yaxes[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   771
                if (axisOptions && !axisOptions.tickColor) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   772
                    axisOptions.tickColor = axisOptions.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   773
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   774
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   775
                axisOptions = $.extend(true, {}, options.yaxis, axisOptions);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   776
                options.yaxes[i] = axisOptions;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   777
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   778
                if (axisOptions.font) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   779
                    axisOptions.font = $.extend({}, fontDefaults, axisOptions.font);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   780
                    if (!axisOptions.font.color) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   781
                        axisOptions.font.color = axisOptions.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   782
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   783
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   784
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   785
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   786
            // backwards compatibility, to be removed in future
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   787
            if (options.xaxis.noTicks && options.xaxis.ticks == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   788
                options.xaxis.ticks = options.xaxis.noTicks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   789
            if (options.yaxis.noTicks && options.yaxis.ticks == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   790
                options.yaxis.ticks = options.yaxis.noTicks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   791
            if (options.x2axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   792
                options.xaxes[1] = $.extend(true, {}, options.xaxis, options.x2axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   793
                options.xaxes[1].position = "top";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   794
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   795
            if (options.y2axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   796
                options.yaxes[1] = $.extend(true, {}, options.yaxis, options.y2axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   797
                options.yaxes[1].position = "right";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   798
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   799
            if (options.grid.coloredAreas)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   800
                options.grid.markings = options.grid.coloredAreas;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   801
            if (options.grid.coloredAreasColor)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   802
                options.grid.markingsColor = options.grid.coloredAreasColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   803
            if (options.lines)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   804
                $.extend(true, options.series.lines, options.lines);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   805
            if (options.points)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   806
                $.extend(true, options.series.points, options.points);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   807
            if (options.bars)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   808
                $.extend(true, options.series.bars, options.bars);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   809
            if (options.shadowSize != null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   810
                options.series.shadowSize = options.shadowSize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   811
            if (options.highlightColor != null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   812
                options.series.highlightColor = options.highlightColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   813
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   814
            // save options on axes for future reference
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   815
            for (i = 0; i < options.xaxes.length; ++i)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   816
                getOrCreateAxis(xaxes, i + 1).options = options.xaxes[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   817
            for (i = 0; i < options.yaxes.length; ++i)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   818
                getOrCreateAxis(yaxes, i + 1).options = options.yaxes[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   819
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   820
            // add hooks from options
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   821
            for (var n in hooks)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   822
                if (options.hooks[n] && options.hooks[n].length)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   823
                    hooks[n] = hooks[n].concat(options.hooks[n]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   824
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   825
            executeHooks(hooks.processOptions, [options]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   826
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   827
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   828
        function setData(d) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   829
            series = parseData(d);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   830
            fillInSeriesOptions();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   831
            processData();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   832
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   833
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   834
        function parseData(d) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   835
            var res = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   836
            for (var i = 0; i < d.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   837
                var s = $.extend(true, {}, options.series);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   838
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   839
                if (d[i].data != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   840
                    s.data = d[i].data; // move the data instead of deep-copy
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   841
                    delete d[i].data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   842
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   843
                    $.extend(true, s, d[i]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   844
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   845
                    d[i].data = s.data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   846
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   847
                else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   848
                    s.data = d[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   849
                res.push(s);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   850
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   851
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   852
            return res;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   853
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   854
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   855
        function axisNumber(obj, coord) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   856
            var a = obj[coord + "axis"];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   857
            if (typeof a == "object") // if we got a real axis, extract number
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   858
                a = a.n;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   859
            if (typeof a != "number")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   860
                a = 1; // default to first axis
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   861
            return a;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   862
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   863
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   864
        function allAxes() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   865
            // return flat array without annoying null entries
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   866
            return $.grep(xaxes.concat(yaxes), function (a) { return a; });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   867
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   868
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   869
        function canvasToAxisCoords(pos) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   870
            // return an object with x/y corresponding to all used axes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   871
            var res = {}, i, axis;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   872
            for (i = 0; i < xaxes.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   873
                axis = xaxes[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   874
                if (axis && axis.used)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   875
                    res["x" + axis.n] = axis.c2p(pos.left);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   876
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   877
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   878
            for (i = 0; i < yaxes.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   879
                axis = yaxes[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   880
                if (axis && axis.used)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   881
                    res["y" + axis.n] = axis.c2p(pos.top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   882
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   883
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   884
            if (res.x1 !== undefined)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   885
                res.x = res.x1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   886
            if (res.y1 !== undefined)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   887
                res.y = res.y1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   888
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   889
            return res;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   890
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   891
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   892
        function axisToCanvasCoords(pos) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   893
            // get canvas coords from the first pair of x/y found in pos
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   894
            var res = {}, i, axis, key;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   895
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   896
            for (i = 0; i < xaxes.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   897
                axis = xaxes[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   898
                if (axis && axis.used) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   899
                    key = "x" + axis.n;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   900
                    if (pos[key] == null && axis.n == 1)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   901
                        key = "x";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   902
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   903
                    if (pos[key] != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   904
                        res.left = axis.p2c(pos[key]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   905
                        break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   906
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   907
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   908
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   909
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   910
            for (i = 0; i < yaxes.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   911
                axis = yaxes[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   912
                if (axis && axis.used) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   913
                    key = "y" + axis.n;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   914
                    if (pos[key] == null && axis.n == 1)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   915
                        key = "y";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   916
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   917
                    if (pos[key] != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   918
                        res.top = axis.p2c(pos[key]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   919
                        break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   920
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   921
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   922
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   923
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   924
            return res;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   925
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   926
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   927
        function getOrCreateAxis(axes, number) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   928
            if (!axes[number - 1])
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   929
                axes[number - 1] = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   930
                    n: number, // save the number for future reference
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   931
                    direction: axes == xaxes ? "x" : "y",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   932
                    options: $.extend(true, {}, axes == xaxes ? options.xaxis : options.yaxis)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   933
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   934
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   935
            return axes[number - 1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   936
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   937
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   938
        function fillInSeriesOptions() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   939
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   940
            var neededColors = series.length, maxIndex = -1, i;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   941
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   942
            // Subtract the number of series that already have fixed colors or
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   943
            // color indexes from the number that we still need to generate.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   944
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   945
            for (i = 0; i < series.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   946
                var sc = series[i].color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   947
                if (sc != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   948
                    neededColors--;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   949
                    if (typeof sc == "number" && sc > maxIndex) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   950
                        maxIndex = sc;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   951
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   952
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   953
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   954
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   955
            // If any of the series have fixed color indexes, then we need to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   956
            // generate at least as many colors as the highest index.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   957
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   958
            if (neededColors <= maxIndex) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   959
                neededColors = maxIndex + 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   960
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   961
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   962
            // Generate all the colors, using first the option colors and then
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   963
            // variations on those colors once they're exhausted.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   964
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   965
            var c, colors = [], colorPool = options.colors,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   966
                colorPoolSize = colorPool.length, variation = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   967
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   968
            for (i = 0; i < neededColors; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   969
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   970
                c = $.color.parse(colorPool[i % colorPoolSize] || "#666");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   971
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   972
                // Each time we exhaust the colors in the pool we adjust
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   973
                // a scaling factor used to produce more variations on
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   974
                // those colors. The factor alternates negative/positive
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   975
                // to produce lighter/darker colors.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   976
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   977
                // Reset the variation after every few cycles, or else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   978
                // it will end up producing only white or black colors.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   979
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   980
                if (i % colorPoolSize == 0 && i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   981
                    if (variation >= 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   982
                        if (variation < 0.5) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   983
                            variation = -variation - 0.2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   984
                        } else variation = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   985
                    } else variation = -variation;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   986
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   987
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   988
                colors[i] = c.scale('rgb', 1 + variation);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   989
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   990
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   991
            // Finalize the series options, filling in their colors
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   992
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   993
            var colori = 0, s;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   994
            for (i = 0; i < series.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   995
                s = series[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   996
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   997
                // assign colors
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   998
                if (s.color == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   999
                    s.color = colors[colori].toString();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1000
                    ++colori;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1001
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1002
                else if (typeof s.color == "number")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1003
                    s.color = colors[s.color].toString();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1004
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1005
                // turn on lines automatically in case nothing is set
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1006
                if (s.lines.show == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1007
                    var v, show = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1008
                    for (v in s)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1009
                        if (s[v] && s[v].show) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1010
                            show = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1011
                            break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1012
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1013
                    if (show)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1014
                        s.lines.show = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1015
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1016
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1017
                // If nothing was provided for lines.zero, default it to match
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1018
                // lines.fill, since areas by default should extend to zero.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1019
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1020
                if (s.lines.zero == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1021
                    s.lines.zero = !!s.lines.fill;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1022
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1023
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1024
                // setup axes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1025
                s.xaxis = getOrCreateAxis(xaxes, axisNumber(s, "x"));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1026
                s.yaxis = getOrCreateAxis(yaxes, axisNumber(s, "y"));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1027
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1028
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1029
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1030
        function processData() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1031
            var topSentry = Number.POSITIVE_INFINITY,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1032
                bottomSentry = Number.NEGATIVE_INFINITY,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1033
                fakeInfinity = Number.MAX_VALUE,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1034
                i, j, k, m, length,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1035
                s, points, ps, x, y, axis, val, f, p,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1036
                data, format;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1037
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1038
            function updateAxis(axis, min, max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1039
                if (min < axis.datamin && min != -fakeInfinity)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1040
                    axis.datamin = min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1041
                if (max > axis.datamax && max != fakeInfinity)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1042
                    axis.datamax = max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1043
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1044
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1045
            $.each(allAxes(), function (_, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1046
                // init axis
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1047
                axis.datamin = topSentry;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1048
                axis.datamax = bottomSentry;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1049
                axis.used = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1050
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1051
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1052
            for (i = 0; i < series.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1053
                s = series[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1054
                s.datapoints = { points: [] };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1055
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1056
                executeHooks(hooks.processRawData, [ s, s.data, s.datapoints ]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1057
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1058
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1059
            // first pass: clean and copy data
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1060
            for (i = 0; i < series.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1061
                s = series[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1062
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1063
                data = s.data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1064
                format = s.datapoints.format;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1065
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1066
                if (!format) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1067
                    format = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1068
                    // find out how to copy
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1069
                    format.push({ x: true, number: true, required: true });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1070
                    format.push({ y: true, number: true, required: true });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1071
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1072
                    if (s.bars.show || (s.lines.show && s.lines.fill)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1073
                        var autoscale = !!((s.bars.show && s.bars.zero) || (s.lines.show && s.lines.zero));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1074
                        format.push({ y: true, number: true, required: false, defaultValue: 0, autoscale: autoscale });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1075
                        if (s.bars.horizontal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1076
                            delete format[format.length - 1].y;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1077
                            format[format.length - 1].x = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1078
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1079
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1080
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1081
                    s.datapoints.format = format;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1082
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1083
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1084
                if (s.datapoints.pointsize != null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1085
                    continue; // already filled in
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1086
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1087
                s.datapoints.pointsize = format.length;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1088
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1089
                ps = s.datapoints.pointsize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1090
                points = s.datapoints.points;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1091
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1092
                var insertSteps = s.lines.show && s.lines.steps;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1093
                s.xaxis.used = s.yaxis.used = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1094
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1095
                for (j = k = 0; j < data.length; ++j, k += ps) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1096
                    p = data[j];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1097
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1098
                    var nullify = p == null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1099
                    if (!nullify) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1100
                        for (m = 0; m < ps; ++m) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1101
                            val = p[m];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1102
                            f = format[m];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1103
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1104
                            if (f) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1105
                                if (f.number && val != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1106
                                    val = +val; // convert to number
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1107
                                    if (isNaN(val))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1108
                                        val = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1109
                                    else if (val == Infinity)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1110
                                        val = fakeInfinity;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1111
                                    else if (val == -Infinity)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1112
                                        val = -fakeInfinity;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1113
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1114
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1115
                                if (val == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1116
                                    if (f.required)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1117
                                        nullify = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1118
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1119
                                    if (f.defaultValue != null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1120
                                        val = f.defaultValue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1121
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1122
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1123
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1124
                            points[k + m] = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1125
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1126
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1127
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1128
                    if (nullify) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1129
                        for (m = 0; m < ps; ++m) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1130
                            val = points[k + m];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1131
                            if (val != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1132
                                f = format[m];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1133
                                // extract min/max info
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1134
                                if (f.autoscale) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1135
                                    if (f.x) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1136
                                        updateAxis(s.xaxis, val, val);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1137
                                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1138
                                    if (f.y) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1139
                                        updateAxis(s.yaxis, val, val);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1140
                                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1141
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1142
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1143
                            points[k + m] = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1144
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1145
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1146
                    else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1147
                        // a little bit of line specific stuff that
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1148
                        // perhaps shouldn't be here, but lacking
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1149
                        // better means...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1150
                        if (insertSteps && k > 0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1151
                            && points[k - ps] != null
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1152
                            && points[k - ps] != points[k]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1153
                            && points[k - ps + 1] != points[k + 1]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1154
                            // copy the point to make room for a middle point
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1155
                            for (m = 0; m < ps; ++m)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1156
                                points[k + ps + m] = points[k + m];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1157
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1158
                            // middle point has same y
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1159
                            points[k + 1] = points[k - ps + 1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1160
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1161
                            // we've added a point, better reflect that
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1162
                            k += ps;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1163
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1164
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1165
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1166
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1167
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1168
            // give the hooks a chance to run
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1169
            for (i = 0; i < series.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1170
                s = series[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1171
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1172
                executeHooks(hooks.processDatapoints, [ s, s.datapoints]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1173
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1174
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1175
            // second pass: find datamax/datamin for auto-scaling
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1176
            for (i = 0; i < series.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1177
                s = series[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1178
                points = s.datapoints.points;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1179
                ps = s.datapoints.pointsize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1180
                format = s.datapoints.format;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1181
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1182
                var xmin = topSentry, ymin = topSentry,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1183
                    xmax = bottomSentry, ymax = bottomSentry;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1184
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1185
                for (j = 0; j < points.length; j += ps) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1186
                    if (points[j] == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1187
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1188
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1189
                    for (m = 0; m < ps; ++m) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1190
                        val = points[j + m];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1191
                        f = format[m];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1192
                        if (!f || f.autoscale === false || val == fakeInfinity || val == -fakeInfinity)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1193
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1194
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1195
                        if (f.x) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1196
                            if (val < xmin)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1197
                                xmin = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1198
                            if (val > xmax)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1199
                                xmax = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1200
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1201
                        if (f.y) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1202
                            if (val < ymin)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1203
                                ymin = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1204
                            if (val > ymax)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1205
                                ymax = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1206
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1207
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1208
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1209
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1210
                if (s.bars.show) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1211
                    // make sure we got room for the bar on the dancing floor
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1212
                    var delta;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1213
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1214
                    switch (s.bars.align) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1215
                        case "left":
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1216
                            delta = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1217
                            break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1218
                        case "right":
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1219
                            delta = -s.bars.barWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1220
                            break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1221
                        case "center":
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1222
                            delta = -s.bars.barWidth / 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1223
                            break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1224
                        default:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1225
                            throw new Error("Invalid bar alignment: " + s.bars.align);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1226
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1227
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1228
                    if (s.bars.horizontal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1229
                        ymin += delta;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1230
                        ymax += delta + s.bars.barWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1231
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1232
                    else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1233
                        xmin += delta;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1234
                        xmax += delta + s.bars.barWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1235
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1236
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1237
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1238
                updateAxis(s.xaxis, xmin, xmax);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1239
                updateAxis(s.yaxis, ymin, ymax);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1240
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1241
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1242
            $.each(allAxes(), function (_, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1243
                if (axis.datamin == topSentry)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1244
                    axis.datamin = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1245
                if (axis.datamax == bottomSentry)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1246
                    axis.datamax = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1247
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1248
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1249
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1250
        function setupCanvases() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1251
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1252
            // Make sure the placeholder is clear of everything except canvases
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1253
            // from a previous plot in this container that we'll try to re-use.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1254
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1255
            placeholder.css("padding", 0) // padding messes up the positioning
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1256
                .children(":not(.flot-base,.flot-overlay)").remove();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1257
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1258
            if (placeholder.css("position") == 'static')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1259
                placeholder.css("position", "relative"); // for positioning labels and overlay
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1260
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1261
            surface = new Canvas("flot-base", placeholder);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1262
            overlay = new Canvas("flot-overlay", placeholder); // overlay canvas for interactive features
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1263
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1264
            ctx = surface.context;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1265
            octx = overlay.context;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1266
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1267
            // define which element we're listening for events on
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1268
            eventHolder = $(overlay.element).unbind();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1269
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1270
            // If we're re-using a plot object, shut down the old one
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1271
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1272
            var existing = placeholder.data("plot");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1273
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1274
            if (existing) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1275
                existing.shutdown();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1276
                overlay.clear();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1277
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1278
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1279
            // save in case we get replotted
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1280
            placeholder.data("plot", plot);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1281
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1282
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1283
        function bindEvents() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1284
            // bind events
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1285
            if (options.grid.hoverable) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1286
                eventHolder.mousemove(onMouseMove);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1287
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1288
                // Use bind, rather than .mouseleave, because we officially
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1289
                // still support jQuery 1.2.6, which doesn't define a shortcut
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1290
                // for mouseenter or mouseleave.  This was a bug/oversight that
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1291
                // was fixed somewhere around 1.3.x.  We can return to using
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1292
                // .mouseleave when we drop support for 1.2.6.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1293
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1294
                eventHolder.bind("mouseleave", onMouseLeave);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1295
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1296
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1297
            if (options.grid.clickable)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1298
                eventHolder.click(onClick);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1299
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1300
            executeHooks(hooks.bindEvents, [eventHolder]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1301
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1302
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1303
        function shutdown() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1304
            if (redrawTimeout)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1305
                clearTimeout(redrawTimeout);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1306
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1307
            eventHolder.unbind("mousemove", onMouseMove);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1308
            eventHolder.unbind("mouseleave", onMouseLeave);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1309
            eventHolder.unbind("click", onClick);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1310
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1311
            executeHooks(hooks.shutdown, [eventHolder]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1312
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1313
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1314
        function setTransformationHelpers(axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1315
            // set helper functions on the axis, assumes plot area
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1316
            // has been computed already
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1317
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1318
            function identity(x) { return x; }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1319
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1320
            var s, m, t = axis.options.transform || identity,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1321
                it = axis.options.inverseTransform;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1322
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1323
            // precompute how much the axis is scaling a point
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1324
            // in canvas space
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1325
            if (axis.direction == "x") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1326
                s = axis.scale = plotWidth / Math.abs(t(axis.max) - t(axis.min));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1327
                m = Math.min(t(axis.max), t(axis.min));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1328
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1329
            else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1330
                s = axis.scale = plotHeight / Math.abs(t(axis.max) - t(axis.min));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1331
                s = -s;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1332
                m = Math.max(t(axis.max), t(axis.min));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1333
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1334
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1335
            // data point to canvas coordinate
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1336
            if (t == identity) // slight optimization
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1337
                axis.p2c = function (p) { return (p - m) * s; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1338
            else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1339
                axis.p2c = function (p) { return (t(p) - m) * s; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1340
            // canvas coordinate to data point
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1341
            if (!it)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1342
                axis.c2p = function (c) { return m + c / s; };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1343
            else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1344
                axis.c2p = function (c) { return it(m + c / s); };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1345
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1346
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1347
        function measureTickLabels(axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1348
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1349
            var opts = axis.options,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1350
                ticks = axis.ticks || [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1351
                labelWidth = opts.labelWidth || 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1352
                labelHeight = opts.labelHeight || 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1353
                maxWidth = labelWidth || axis.direction == "x" ? Math.floor(surface.width / (ticks.length || 1)) : null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1354
                legacyStyles = axis.direction + "Axis " + axis.direction + axis.n + "Axis",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1355
                layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + legacyStyles,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1356
                font = opts.font || "flot-tick-label tickLabel";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1357
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1358
            for (var i = 0; i < ticks.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1359
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1360
                var t = ticks[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1361
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1362
                if (!t.label)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1363
                    continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1364
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1365
                var info = surface.getTextInfo(layer, t.label, font, null, maxWidth);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1366
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1367
                labelWidth = Math.max(labelWidth, info.width);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1368
                labelHeight = Math.max(labelHeight, info.height);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1369
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1370
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1371
            axis.labelWidth = opts.labelWidth || labelWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1372
            axis.labelHeight = opts.labelHeight || labelHeight;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1373
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1374
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1375
        function allocateAxisBoxFirstPhase(axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1376
            // find the bounding box of the axis by looking at label
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1377
            // widths/heights and ticks, make room by diminishing the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1378
            // plotOffset; this first phase only looks at one
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1379
            // dimension per axis, the other dimension depends on the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1380
            // other axes so will have to wait
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1381
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1382
            var lw = axis.labelWidth,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1383
                lh = axis.labelHeight,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1384
                pos = axis.options.position,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1385
                tickLength = axis.options.tickLength,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1386
                axisMargin = options.grid.axisMargin,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1387
                padding = options.grid.labelMargin,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1388
                all = axis.direction == "x" ? xaxes : yaxes,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1389
                index, innermost;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1390
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1391
            // determine axis margin
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1392
            var samePosition = $.grep(all, function (a) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1393
                return a && a.options.position == pos && a.reserveSpace;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1394
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1395
            if ($.inArray(axis, samePosition) == samePosition.length - 1)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1396
                axisMargin = 0; // outermost
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1397
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1398
            // determine tick length - if we're innermost, we can use "full"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1399
            if (tickLength == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1400
                var sameDirection = $.grep(all, function (a) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1401
                    return a && a.reserveSpace;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1402
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1403
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1404
                innermost = $.inArray(axis, sameDirection) == 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1405
                if (innermost)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1406
                    tickLength = "full";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1407
                else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1408
                    tickLength = 5;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1409
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1410
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1411
            if (!isNaN(+tickLength))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1412
                padding += +tickLength;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1413
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1414
            // compute box
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1415
            if (axis.direction == "x") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1416
                lh += padding;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1417
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1418
                if (pos == "bottom") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1419
                    plotOffset.bottom += lh + axisMargin;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1420
                    axis.box = { top: surface.height - plotOffset.bottom, height: lh };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1421
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1422
                else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1423
                    axis.box = { top: plotOffset.top + axisMargin, height: lh };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1424
                    plotOffset.top += lh + axisMargin;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1425
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1426
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1427
            else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1428
                lw += padding;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1429
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1430
                if (pos == "left") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1431
                    axis.box = { left: plotOffset.left + axisMargin, width: lw };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1432
                    plotOffset.left += lw + axisMargin;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1433
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1434
                else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1435
                    plotOffset.right += lw + axisMargin;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1436
                    axis.box = { left: surface.width - plotOffset.right, width: lw };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1437
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1438
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1439
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1440
             // save for future reference
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1441
            axis.position = pos;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1442
            axis.tickLength = tickLength;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1443
            axis.box.padding = padding;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1444
            axis.innermost = innermost;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1445
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1446
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1447
        function allocateAxisBoxSecondPhase(axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1448
            // now that all axis boxes have been placed in one
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1449
            // dimension, we can set the remaining dimension coordinates
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1450
            if (axis.direction == "x") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1451
                axis.box.left = plotOffset.left - axis.labelWidth / 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1452
                axis.box.width = surface.width - plotOffset.left - plotOffset.right + axis.labelWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1453
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1454
            else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1455
                axis.box.top = plotOffset.top - axis.labelHeight / 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1456
                axis.box.height = surface.height - plotOffset.bottom - plotOffset.top + axis.labelHeight;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1457
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1458
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1459
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1460
        function adjustLayoutForThingsStickingOut() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1461
            // possibly adjust plot offset to ensure everything stays
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1462
            // inside the canvas and isn't clipped off
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1463
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1464
            var minMargin = options.grid.minBorderMargin,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1465
                margins = { x: 0, y: 0 }, i, axis;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1466
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1467
            // check stuff from the plot (FIXME: this should just read
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1468
            // a value from the series, otherwise it's impossible to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1469
            // customize)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1470
            if (minMargin == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1471
                minMargin = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1472
                for (i = 0; i < series.length; ++i)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1473
                    minMargin = Math.max(minMargin, 2 * (series[i].points.radius + series[i].points.lineWidth/2));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1474
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1475
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1476
            margins.x = margins.y = Math.ceil(minMargin);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1477
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1478
            // check axis labels, note we don't check the actual
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1479
            // labels but instead use the overall width/height to not
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1480
            // jump as much around with replots
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1481
            $.each(allAxes(), function (_, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1482
                var dir = axis.direction;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1483
                if (axis.reserveSpace)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1484
                    margins[dir] = Math.ceil(Math.max(margins[dir], (dir == "x" ? axis.labelWidth : axis.labelHeight) / 2));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1485
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1486
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1487
            plotOffset.left = Math.max(margins.x, plotOffset.left);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1488
            plotOffset.right = Math.max(margins.x, plotOffset.right);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1489
            plotOffset.top = Math.max(margins.y, plotOffset.top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1490
            plotOffset.bottom = Math.max(margins.y, plotOffset.bottom);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1491
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1492
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1493
        function setupGrid() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1494
            var i, axes = allAxes(), showGrid = options.grid.show;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1495
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1496
            // Initialize the plot's offset from the edge of the canvas
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1497
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1498
            for (var a in plotOffset) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1499
                var margin = options.grid.margin || 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1500
                plotOffset[a] = typeof margin == "number" ? margin : margin[a] || 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1501
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1502
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1503
            executeHooks(hooks.processOffset, [plotOffset]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1504
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1505
            // If the grid is visible, add its border width to the offset
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1506
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1507
            for (var a in plotOffset) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1508
                if(typeof(options.grid.borderWidth) == "object") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1509
                    plotOffset[a] += showGrid ? options.grid.borderWidth[a] : 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1510
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1511
                else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1512
                    plotOffset[a] += showGrid ? options.grid.borderWidth : 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1513
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1514
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1515
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1516
            // init axes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1517
            $.each(axes, function (_, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1518
                axis.show = axis.options.show;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1519
                if (axis.show == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1520
                    axis.show = axis.used; // by default an axis is visible if it's got data
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1521
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1522
                axis.reserveSpace = axis.show || axis.options.reserveSpace;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1523
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1524
                setRange(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1525
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1526
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1527
            if (showGrid) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1528
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1529
                var allocatedAxes = $.grep(axes, function (axis) { return axis.reserveSpace; });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1530
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1531
                $.each(allocatedAxes, function (_, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1532
                    // make the ticks
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1533
                    setupTickGeneration(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1534
                    setTicks(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1535
                    snapRangeToTicks(axis, axis.ticks);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1536
                    // find labelWidth/Height for axis
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1537
                    measureTickLabels(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1538
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1539
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1540
                // with all dimensions calculated, we can compute the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1541
                // axis bounding boxes, start from the outside
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1542
                // (reverse order)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1543
                for (i = allocatedAxes.length - 1; i >= 0; --i)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1544
                    allocateAxisBoxFirstPhase(allocatedAxes[i]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1545
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1546
                // make sure we've got enough space for things that
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1547
                // might stick out
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1548
                adjustLayoutForThingsStickingOut();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1549
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1550
                $.each(allocatedAxes, function (_, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1551
                    allocateAxisBoxSecondPhase(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1552
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1553
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1554
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1555
            plotWidth = surface.width - plotOffset.left - plotOffset.right;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1556
            plotHeight = surface.height - plotOffset.bottom - plotOffset.top;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1557
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1558
            // now we got the proper plot dimensions, we can compute the scaling
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1559
            $.each(axes, function (_, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1560
                setTransformationHelpers(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1561
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1562
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1563
            if (showGrid) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1564
                drawAxisLabels();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1565
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1566
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1567
            insertLegend();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1568
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1569
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1570
        function setRange(axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1571
            var opts = axis.options,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1572
                min = +(opts.min != null ? opts.min : axis.datamin),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1573
                max = +(opts.max != null ? opts.max : axis.datamax),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1574
                delta = max - min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1575
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1576
            if (delta == 0.0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1577
                // degenerate case
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1578
                var widen = max == 0 ? 1 : 0.01;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1579
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1580
                if (opts.min == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1581
                    min -= widen;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1582
                // always widen max if we couldn't widen min to ensure we
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1583
                // don't fall into min == max which doesn't work
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1584
                if (opts.max == null || opts.min != null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1585
                    max += widen;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1586
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1587
            else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1588
                // consider autoscaling
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1589
                var margin = opts.autoscaleMargin;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1590
                if (margin != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1591
                    if (opts.min == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1592
                        min -= delta * margin;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1593
                        // make sure we don't go below zero if all values
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1594
                        // are positive
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1595
                        if (min < 0 && axis.datamin != null && axis.datamin >= 0)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1596
                            min = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1597
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1598
                    if (opts.max == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1599
                        max += delta * margin;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1600
                        if (max > 0 && axis.datamax != null && axis.datamax <= 0)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1601
                            max = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1602
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1603
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1604
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1605
            axis.min = min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1606
            axis.max = max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1607
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1608
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1609
        function setupTickGeneration(axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1610
            var opts = axis.options;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1611
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1612
            // estimate number of ticks
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1613
            var noTicks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1614
            if (typeof opts.ticks == "number" && opts.ticks > 0)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1615
                noTicks = opts.ticks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1616
            else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1617
                // heuristic based on the model a*sqrt(x) fitted to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1618
                // some data points that seemed reasonable
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1619
                noTicks = 0.3 * Math.sqrt(axis.direction == "x" ? surface.width : surface.height);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1620
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1621
            var delta = (axis.max - axis.min) / noTicks,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1622
                dec = -Math.floor(Math.log(delta) / Math.LN10),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1623
                maxDec = opts.tickDecimals;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1624
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1625
            if (maxDec != null && dec > maxDec) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1626
                dec = maxDec;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1627
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1628
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1629
            var magn = Math.pow(10, -dec),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1630
                norm = delta / magn, // norm is between 1.0 and 10.0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1631
                size;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1632
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1633
            if (norm < 1.5) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1634
                size = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1635
            } else if (norm < 3) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1636
                size = 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1637
                // special case for 2.5, requires an extra decimal
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1638
                if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1639
                    size = 2.5;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1640
                    ++dec;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1641
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1642
            } else if (norm < 7.5) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1643
                size = 5;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1644
            } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1645
                size = 10;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1646
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1647
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1648
            size *= magn;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1649
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1650
            if (opts.minTickSize != null && size < opts.minTickSize) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1651
                size = opts.minTickSize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1652
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1653
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1654
            axis.delta = delta;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1655
            axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1656
            axis.tickSize = opts.tickSize || size;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1657
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1658
            // Time mode was moved to a plug-in in 0.8, but since so many people use this
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1659
            // we'll add an especially friendly make sure they remembered to include it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1660
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1661
            if (opts.mode == "time" && !axis.tickGenerator) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1662
                throw new Error("Time mode requires the flot.time plugin.");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1663
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1664
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1665
            // Flot supports base-10 axes; any other mode else is handled by a plug-in,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1666
            // like flot.time.js.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1667
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1668
            if (!axis.tickGenerator) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1669
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1670
                axis.tickGenerator = function (axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1671
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1672
                    var ticks = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1673
                        start = floorInBase(axis.min, axis.tickSize),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1674
                        i = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1675
                        v = Number.NaN,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1676
                        prev;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1677
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1678
                    do {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1679
                        prev = v;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1680
                        v = start + i * axis.tickSize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1681
                        ticks.push(v);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1682
                        ++i;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1683
                    } while (v < axis.max && v != prev);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1684
                    return ticks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1685
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1686
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1687
				axis.tickFormatter = function (value, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1688
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1689
					var factor = axis.tickDecimals ? Math.pow(10, axis.tickDecimals) : 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1690
					var formatted = "" + Math.round(value * factor) / factor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1691
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1692
					// If tickDecimals was specified, ensure that we have exactly that
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1693
					// much precision; otherwise default to the value's own precision.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1694
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1695
					if (axis.tickDecimals != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1696
						var decimal = formatted.indexOf(".");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1697
						var precision = decimal == -1 ? 0 : formatted.length - decimal - 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1698
						if (precision < axis.tickDecimals) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1699
							return (precision ? formatted : formatted + ".") + ("" + factor).substr(1, axis.tickDecimals - precision);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1700
						}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1701
					}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1702
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1703
                    return formatted;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1704
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1705
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1706
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1707
            if ($.isFunction(opts.tickFormatter))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1708
                axis.tickFormatter = function (v, axis) { return "" + opts.tickFormatter(v, axis); };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1709
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1710
            if (opts.alignTicksWithAxis != null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1711
                var otherAxis = (axis.direction == "x" ? xaxes : yaxes)[opts.alignTicksWithAxis - 1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1712
                if (otherAxis && otherAxis.used && otherAxis != axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1713
                    // consider snapping min/max to outermost nice ticks
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1714
                    var niceTicks = axis.tickGenerator(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1715
                    if (niceTicks.length > 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1716
                        if (opts.min == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1717
                            axis.min = Math.min(axis.min, niceTicks[0]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1718
                        if (opts.max == null && niceTicks.length > 1)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1719
                            axis.max = Math.max(axis.max, niceTicks[niceTicks.length - 1]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1720
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1721
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1722
                    axis.tickGenerator = function (axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1723
                        // copy ticks, scaled to this axis
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1724
                        var ticks = [], v, i;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1725
                        for (i = 0; i < otherAxis.ticks.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1726
                            v = (otherAxis.ticks[i].v - otherAxis.min) / (otherAxis.max - otherAxis.min);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1727
                            v = axis.min + v * (axis.max - axis.min);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1728
                            ticks.push(v);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1729
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1730
                        return ticks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1731
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1732
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1733
                    // we might need an extra decimal since forced
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1734
                    // ticks don't necessarily fit naturally
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1735
                    if (!axis.mode && opts.tickDecimals == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1736
                        var extraDec = Math.max(0, -Math.floor(Math.log(axis.delta) / Math.LN10) + 1),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1737
                            ts = axis.tickGenerator(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1738
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1739
                        // only proceed if the tick interval rounded
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1740
                        // with an extra decimal doesn't give us a
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1741
                        // zero at end
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1742
                        if (!(ts.length > 1 && /\..*0$/.test((ts[1] - ts[0]).toFixed(extraDec))))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1743
                            axis.tickDecimals = extraDec;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1744
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1745
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1746
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1747
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1748
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1749
        function setTicks(axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1750
            var oticks = axis.options.ticks, ticks = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1751
            if (oticks == null || (typeof oticks == "number" && oticks > 0))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1752
                ticks = axis.tickGenerator(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1753
            else if (oticks) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1754
                if ($.isFunction(oticks))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1755
                    // generate the ticks
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1756
                    ticks = oticks(axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1757
                else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1758
                    ticks = oticks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1759
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1760
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1761
            // clean up/labelify the supplied ticks, copy them over
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1762
            var i, v;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1763
            axis.ticks = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1764
            for (i = 0; i < ticks.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1765
                var label = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1766
                var t = ticks[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1767
                if (typeof t == "object") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1768
                    v = +t[0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1769
                    if (t.length > 1)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1770
                        label = t[1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1771
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1772
                else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1773
                    v = +t;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1774
                if (label == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1775
                    label = axis.tickFormatter(v, axis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1776
                if (!isNaN(v))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1777
                    axis.ticks.push({ v: v, label: label });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1778
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1779
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1780
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1781
        function snapRangeToTicks(axis, ticks) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1782
            if (axis.options.autoscaleMargin && ticks.length > 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1783
                // snap to ticks
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1784
                if (axis.options.min == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1785
                    axis.min = Math.min(axis.min, ticks[0].v);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1786
                if (axis.options.max == null && ticks.length > 1)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1787
                    axis.max = Math.max(axis.max, ticks[ticks.length - 1].v);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1788
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1789
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1790
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1791
        function draw() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1792
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1793
            surface.clear();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1794
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1795
            executeHooks(hooks.drawBackground, [ctx]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1796
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1797
            var grid = options.grid;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1798
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1799
            // draw background, if any
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1800
            if (grid.show && grid.backgroundColor)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1801
                drawBackground();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1802
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1803
            if (grid.show && !grid.aboveData) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1804
                drawGrid();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1805
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1806
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1807
            for (var i = 0; i < series.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1808
                executeHooks(hooks.drawSeries, [ctx, series[i]]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1809
                drawSeries(series[i]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1810
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1811
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1812
            executeHooks(hooks.draw, [ctx]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1813
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1814
            if (grid.show && grid.aboveData) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1815
                drawGrid();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1816
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1817
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1818
            surface.render();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1819
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1820
            // A draw implies that either the axes or data have changed, so we
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1821
            // should probably update the overlay highlights as well.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1822
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1823
            triggerRedrawOverlay();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1824
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1825
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1826
        function extractRange(ranges, coord) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1827
            var axis, from, to, key, axes = allAxes();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1828
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1829
            for (var i = 0; i < axes.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1830
                axis = axes[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1831
                if (axis.direction == coord) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1832
                    key = coord + axis.n + "axis";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1833
                    if (!ranges[key] && axis.n == 1)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1834
                        key = coord + "axis"; // support x1axis as xaxis
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1835
                    if (ranges[key]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1836
                        from = ranges[key].from;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1837
                        to = ranges[key].to;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1838
                        break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1839
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1840
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1841
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1842
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1843
            // backwards-compat stuff - to be removed in future
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1844
            if (!ranges[key]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1845
                axis = coord == "x" ? xaxes[0] : yaxes[0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1846
                from = ranges[coord + "1"];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1847
                to = ranges[coord + "2"];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1848
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1849
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1850
            // auto-reverse as an added bonus
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1851
            if (from != null && to != null && from > to) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1852
                var tmp = from;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1853
                from = to;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1854
                to = tmp;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1855
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1856
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1857
            return { from: from, to: to, axis: axis };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1858
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1859
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1860
        function drawBackground() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1861
            ctx.save();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1862
            ctx.translate(plotOffset.left, plotOffset.top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1863
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1864
            ctx.fillStyle = getColorOrGradient(options.grid.backgroundColor, plotHeight, 0, "rgba(255, 255, 255, 0)");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1865
            ctx.fillRect(0, 0, plotWidth, plotHeight);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1866
            ctx.restore();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1867
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1868
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1869
        function drawGrid() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1870
            var i, axes, bw, bc;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1871
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1872
            ctx.save();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1873
            ctx.translate(plotOffset.left, plotOffset.top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1874
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1875
            // draw markings
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1876
            var markings = options.grid.markings;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1877
            if (markings) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1878
                if ($.isFunction(markings)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1879
                    axes = plot.getAxes();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1880
                    // xmin etc. is backwards compatibility, to be
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1881
                    // removed in the future
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1882
                    axes.xmin = axes.xaxis.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1883
                    axes.xmax = axes.xaxis.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1884
                    axes.ymin = axes.yaxis.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1885
                    axes.ymax = axes.yaxis.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1886
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1887
                    markings = markings(axes);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1888
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1889
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1890
                for (i = 0; i < markings.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1891
                    var m = markings[i],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1892
                        xrange = extractRange(m, "x"),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1893
                        yrange = extractRange(m, "y");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1894
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1895
                    // fill in missing
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1896
                    if (xrange.from == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1897
                        xrange.from = xrange.axis.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1898
                    if (xrange.to == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1899
                        xrange.to = xrange.axis.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1900
                    if (yrange.from == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1901
                        yrange.from = yrange.axis.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1902
                    if (yrange.to == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1903
                        yrange.to = yrange.axis.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1904
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1905
                    // clip
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1906
                    if (xrange.to < xrange.axis.min || xrange.from > xrange.axis.max ||
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1907
                        yrange.to < yrange.axis.min || yrange.from > yrange.axis.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1908
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1909
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1910
                    xrange.from = Math.max(xrange.from, xrange.axis.min);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1911
                    xrange.to = Math.min(xrange.to, xrange.axis.max);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1912
                    yrange.from = Math.max(yrange.from, yrange.axis.min);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1913
                    yrange.to = Math.min(yrange.to, yrange.axis.max);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1914
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1915
                    if (xrange.from == xrange.to && yrange.from == yrange.to)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1916
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1917
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1918
                    // then draw
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1919
                    xrange.from = xrange.axis.p2c(xrange.from);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1920
                    xrange.to = xrange.axis.p2c(xrange.to);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1921
                    yrange.from = yrange.axis.p2c(yrange.from);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1922
                    yrange.to = yrange.axis.p2c(yrange.to);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1923
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1924
                    if (xrange.from == xrange.to || yrange.from == yrange.to) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1925
                        // draw line
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1926
                        ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1927
                        ctx.strokeStyle = m.color || options.grid.markingsColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1928
                        ctx.lineWidth = m.lineWidth || options.grid.markingsLineWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1929
                        ctx.moveTo(xrange.from, yrange.from);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1930
                        ctx.lineTo(xrange.to, yrange.to);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1931
                        ctx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1932
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1933
                    else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1934
                        // fill area
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1935
                        ctx.fillStyle = m.color || options.grid.markingsColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1936
                        ctx.fillRect(xrange.from, yrange.to,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1937
                                     xrange.to - xrange.from,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1938
                                     yrange.from - yrange.to);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1939
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1940
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1941
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1942
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1943
            // draw the ticks
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1944
            axes = allAxes();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1945
            bw = options.grid.borderWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1946
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1947
            for (var j = 0; j < axes.length; ++j) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1948
                var axis = axes[j], box = axis.box,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1949
                    t = axis.tickLength, x, y, xoff, yoff;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1950
                if (!axis.show || axis.ticks.length == 0)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1951
                    continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1952
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1953
                ctx.lineWidth = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1954
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1955
                // find the edges
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1956
                if (axis.direction == "x") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1957
                    x = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1958
                    if (t == "full")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1959
                        y = (axis.position == "top" ? 0 : plotHeight);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1960
                    else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1961
                        y = box.top - plotOffset.top + (axis.position == "top" ? box.height : 0);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1962
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1963
                else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1964
                    y = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1965
                    if (t == "full")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1966
                        x = (axis.position == "left" ? 0 : plotWidth);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1967
                    else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1968
                        x = box.left - plotOffset.left + (axis.position == "left" ? box.width : 0);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1969
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1970
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1971
                // draw tick bar
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1972
                if (!axis.innermost) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1973
                    ctx.strokeStyle = axis.options.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1974
                    ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1975
                    xoff = yoff = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1976
                    if (axis.direction == "x")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1977
                        xoff = plotWidth + 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1978
                    else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1979
                        yoff = plotHeight + 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1980
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1981
                    if (ctx.lineWidth == 1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1982
                        if (axis.direction == "x") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1983
                            y = Math.floor(y) + 0.5;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1984
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1985
                            x = Math.floor(x) + 0.5;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1986
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1987
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1988
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1989
                    ctx.moveTo(x, y);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1990
                    ctx.lineTo(x + xoff, y + yoff);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1991
                    ctx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1992
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1993
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1994
                // draw ticks
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1995
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1996
                ctx.strokeStyle = axis.options.tickColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1997
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1998
                ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1999
                for (i = 0; i < axis.ticks.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2000
                    var v = axis.ticks[i].v;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2001
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2002
                    xoff = yoff = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2003
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2004
                    if (isNaN(v) || v < axis.min || v > axis.max
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2005
                        // skip those lying on the axes if we got a border
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2006
                        || (t == "full"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2007
                            && ((typeof bw == "object" && bw[axis.position] > 0) || bw > 0)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2008
                            && (v == axis.min || v == axis.max)))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2009
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2010
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2011
                    if (axis.direction == "x") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2012
                        x = axis.p2c(v);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2013
                        yoff = t == "full" ? -plotHeight : t;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2014
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2015
                        if (axis.position == "top")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2016
                            yoff = -yoff;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2017
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2018
                    else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2019
                        y = axis.p2c(v);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2020
                        xoff = t == "full" ? -plotWidth : t;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2021
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2022
                        if (axis.position == "left")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2023
                            xoff = -xoff;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2024
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2025
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2026
                    if (ctx.lineWidth == 1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2027
                        if (axis.direction == "x")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2028
                            x = Math.floor(x) + 0.5;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2029
                        else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2030
                            y = Math.floor(y) + 0.5;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2031
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2032
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2033
                    ctx.moveTo(x, y);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2034
                    ctx.lineTo(x + xoff, y + yoff);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2035
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2036
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2037
                ctx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2038
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2039
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2040
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2041
            // draw border
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2042
            if (bw) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2043
                // If either borderWidth or borderColor is an object, then draw the border
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2044
                // line by line instead of as one rectangle
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2045
                bc = options.grid.borderColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2046
                if(typeof bw == "object" || typeof bc == "object") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2047
                    if (typeof bw !== "object") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2048
                        bw = {top: bw, right: bw, bottom: bw, left: bw};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2049
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2050
                    if (typeof bc !== "object") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2051
                        bc = {top: bc, right: bc, bottom: bc, left: bc};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2052
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2053
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2054
                    if (bw.top > 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2055
                        ctx.strokeStyle = bc.top;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2056
                        ctx.lineWidth = bw.top;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2057
                        ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2058
                        ctx.moveTo(0 - bw.left, 0 - bw.top/2);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2059
                        ctx.lineTo(plotWidth, 0 - bw.top/2);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2060
                        ctx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2061
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2062
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2063
                    if (bw.right > 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2064
                        ctx.strokeStyle = bc.right;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2065
                        ctx.lineWidth = bw.right;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2066
                        ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2067
                        ctx.moveTo(plotWidth + bw.right / 2, 0 - bw.top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2068
                        ctx.lineTo(plotWidth + bw.right / 2, plotHeight);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2069
                        ctx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2070
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2071
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2072
                    if (bw.bottom > 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2073
                        ctx.strokeStyle = bc.bottom;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2074
                        ctx.lineWidth = bw.bottom;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2075
                        ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2076
                        ctx.moveTo(plotWidth + bw.right, plotHeight + bw.bottom / 2);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2077
                        ctx.lineTo(0, plotHeight + bw.bottom / 2);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2078
                        ctx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2079
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2080
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2081
                    if (bw.left > 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2082
                        ctx.strokeStyle = bc.left;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2083
                        ctx.lineWidth = bw.left;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2084
                        ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2085
                        ctx.moveTo(0 - bw.left/2, plotHeight + bw.bottom);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2086
                        ctx.lineTo(0- bw.left/2, 0);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2087
                        ctx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2088
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2089
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2090
                else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2091
                    ctx.lineWidth = bw;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2092
                    ctx.strokeStyle = options.grid.borderColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2093
                    ctx.strokeRect(-bw/2, -bw/2, plotWidth + bw, plotHeight + bw);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2094
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2095
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2096
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2097
            ctx.restore();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2098
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2099
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2100
        function drawAxisLabels() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2101
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2102
            $.each(allAxes(), function (_, axis) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2103
                if (!axis.show || axis.ticks.length == 0)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2104
                    return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2105
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2106
                var box = axis.box,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2107
                    legacyStyles = axis.direction + "Axis " + axis.direction + axis.n + "Axis",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2108
                    layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + legacyStyles,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2109
                    font = axis.options.font || "flot-tick-label tickLabel",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2110
                    tick, x, y, halign, valign;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2111
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2112
                surface.removeText(layer);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2113
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2114
                for (var i = 0; i < axis.ticks.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2115
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2116
                    tick = axis.ticks[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2117
                    if (!tick.label || tick.v < axis.min || tick.v > axis.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2118
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2119
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2120
                    if (axis.direction == "x") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2121
                        halign = "center";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2122
                        x = plotOffset.left + axis.p2c(tick.v);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2123
                        if (axis.position == "bottom") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2124
                            y = box.top + box.padding;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2125
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2126
                            y = box.top + box.height - box.padding;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2127
                            valign = "bottom";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2128
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2129
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2130
                        valign = "middle";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2131
                        y = plotOffset.top + axis.p2c(tick.v);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2132
                        if (axis.position == "left") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2133
                            x = box.left + box.width - box.padding;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2134
                            halign = "right";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2135
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2136
                            x = box.left + box.padding;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2137
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2138
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2139
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2140
                    surface.addText(layer, x, y, tick.label, font, null, null, halign, valign);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2141
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2142
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2143
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2144
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2145
        function drawSeries(series) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2146
            if (series.lines.show)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2147
                drawSeriesLines(series);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2148
            if (series.bars.show)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2149
                drawSeriesBars(series);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2150
            if (series.points.show)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2151
                drawSeriesPoints(series);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2152
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2153
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2154
        function drawSeriesLines(series) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2155
            function plotLine(datapoints, xoffset, yoffset, axisx, axisy) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2156
                var points = datapoints.points,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2157
                    ps = datapoints.pointsize,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2158
                    prevx = null, prevy = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2159
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2160
                ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2161
                for (var i = ps; i < points.length; i += ps) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2162
                    var x1 = points[i - ps], y1 = points[i - ps + 1],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2163
                        x2 = points[i], y2 = points[i + 1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2164
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2165
                    if (x1 == null || x2 == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2166
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2167
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2168
                    // clip with ymin
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2169
                    if (y1 <= y2 && y1 < axisy.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2170
                        if (y2 < axisy.min)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2171
                            continue;   // line segment is outside
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2172
                        // compute new intersection point
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2173
                        x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2174
                        y1 = axisy.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2175
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2176
                    else if (y2 <= y1 && y2 < axisy.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2177
                        if (y1 < axisy.min)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2178
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2179
                        x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2180
                        y2 = axisy.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2181
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2182
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2183
                    // clip with ymax
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2184
                    if (y1 >= y2 && y1 > axisy.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2185
                        if (y2 > axisy.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2186
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2187
                        x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2188
                        y1 = axisy.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2189
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2190
                    else if (y2 >= y1 && y2 > axisy.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2191
                        if (y1 > axisy.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2192
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2193
                        x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2194
                        y2 = axisy.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2195
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2196
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2197
                    // clip with xmin
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2198
                    if (x1 <= x2 && x1 < axisx.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2199
                        if (x2 < axisx.min)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2200
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2201
                        y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2202
                        x1 = axisx.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2203
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2204
                    else if (x2 <= x1 && x2 < axisx.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2205
                        if (x1 < axisx.min)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2206
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2207
                        y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2208
                        x2 = axisx.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2209
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2210
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2211
                    // clip with xmax
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2212
                    if (x1 >= x2 && x1 > axisx.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2213
                        if (x2 > axisx.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2214
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2215
                        y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2216
                        x1 = axisx.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2217
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2218
                    else if (x2 >= x1 && x2 > axisx.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2219
                        if (x1 > axisx.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2220
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2221
                        y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2222
                        x2 = axisx.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2223
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2224
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2225
                    if (x1 != prevx || y1 != prevy)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2226
                        ctx.moveTo(axisx.p2c(x1) + xoffset, axisy.p2c(y1) + yoffset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2227
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2228
                    prevx = x2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2229
                    prevy = y2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2230
                    ctx.lineTo(axisx.p2c(x2) + xoffset, axisy.p2c(y2) + yoffset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2231
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2232
                ctx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2233
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2234
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2235
            function plotLineArea(datapoints, axisx, axisy) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2236
                var points = datapoints.points,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2237
                    ps = datapoints.pointsize,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2238
                    bottom = Math.min(Math.max(0, axisy.min), axisy.max),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2239
                    i = 0, top, areaOpen = false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2240
                    ypos = 1, segmentStart = 0, segmentEnd = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2241
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2242
                // we process each segment in two turns, first forward
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2243
                // direction to sketch out top, then once we hit the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2244
                // end we go backwards to sketch the bottom
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2245
                while (true) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2246
                    if (ps > 0 && i > points.length + ps)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2247
                        break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2248
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2249
                    i += ps; // ps is negative if going backwards
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2250
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2251
                    var x1 = points[i - ps],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2252
                        y1 = points[i - ps + ypos],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2253
                        x2 = points[i], y2 = points[i + ypos];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2254
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2255
                    if (areaOpen) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2256
                        if (ps > 0 && x1 != null && x2 == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2257
                            // at turning point
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2258
                            segmentEnd = i;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2259
                            ps = -ps;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2260
                            ypos = 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2261
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2262
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2263
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2264
                        if (ps < 0 && i == segmentStart + ps) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2265
                            // done with the reverse sweep
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2266
                            ctx.fill();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2267
                            areaOpen = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2268
                            ps = -ps;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2269
                            ypos = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2270
                            i = segmentStart = segmentEnd + ps;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2271
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2272
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2273
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2274
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2275
                    if (x1 == null || x2 == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2276
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2277
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2278
                    // clip x values
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2279
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2280
                    // clip with xmin
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2281
                    if (x1 <= x2 && x1 < axisx.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2282
                        if (x2 < axisx.min)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2283
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2284
                        y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2285
                        x1 = axisx.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2286
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2287
                    else if (x2 <= x1 && x2 < axisx.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2288
                        if (x1 < axisx.min)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2289
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2290
                        y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2291
                        x2 = axisx.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2292
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2293
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2294
                    // clip with xmax
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2295
                    if (x1 >= x2 && x1 > axisx.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2296
                        if (x2 > axisx.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2297
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2298
                        y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2299
                        x1 = axisx.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2300
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2301
                    else if (x2 >= x1 && x2 > axisx.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2302
                        if (x1 > axisx.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2303
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2304
                        y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2305
                        x2 = axisx.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2306
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2307
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2308
                    if (!areaOpen) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2309
                        // open area
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2310
                        ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2311
                        ctx.moveTo(axisx.p2c(x1), axisy.p2c(bottom));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2312
                        areaOpen = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2313
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2314
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2315
                    // now first check the case where both is outside
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2316
                    if (y1 >= axisy.max && y2 >= axisy.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2317
                        ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.max));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2318
                        ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.max));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2319
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2320
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2321
                    else if (y1 <= axisy.min && y2 <= axisy.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2322
                        ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.min));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2323
                        ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.min));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2324
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2325
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2326
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2327
                    // else it's a bit more complicated, there might
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2328
                    // be a flat maxed out rectangle first, then a
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2329
                    // triangular cutout or reverse; to find these
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2330
                    // keep track of the current x values
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2331
                    var x1old = x1, x2old = x2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2332
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2333
                    // clip the y values, without shortcutting, we
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2334
                    // go through all cases in turn
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2335
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2336
                    // clip with ymin
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2337
                    if (y1 <= y2 && y1 < axisy.min && y2 >= axisy.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2338
                        x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2339
                        y1 = axisy.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2340
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2341
                    else if (y2 <= y1 && y2 < axisy.min && y1 >= axisy.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2342
                        x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2343
                        y2 = axisy.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2344
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2345
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2346
                    // clip with ymax
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2347
                    if (y1 >= y2 && y1 > axisy.max && y2 <= axisy.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2348
                        x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2349
                        y1 = axisy.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2350
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2351
                    else if (y2 >= y1 && y2 > axisy.max && y1 <= axisy.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2352
                        x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2353
                        y2 = axisy.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2354
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2355
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2356
                    // if the x value was changed we got a rectangle
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2357
                    // to fill
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2358
                    if (x1 != x1old) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2359
                        ctx.lineTo(axisx.p2c(x1old), axisy.p2c(y1));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2360
                        // it goes to (x1, y1), but we fill that below
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2361
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2362
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2363
                    // fill triangular section, this sometimes result
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2364
                    // in redundant points if (x1, y1) hasn't changed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2365
                    // from previous line to, but we just ignore that
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2366
                    ctx.lineTo(axisx.p2c(x1), axisy.p2c(y1));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2367
                    ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2368
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2369
                    // fill the other rectangle if it's there
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2370
                    if (x2 != x2old) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2371
                        ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2372
                        ctx.lineTo(axisx.p2c(x2old), axisy.p2c(y2));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2373
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2374
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2375
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2376
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2377
            ctx.save();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2378
            ctx.translate(plotOffset.left, plotOffset.top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2379
            ctx.lineJoin = "round";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2380
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2381
            var lw = series.lines.lineWidth,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2382
                sw = series.shadowSize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2383
            // FIXME: consider another form of shadow when filling is turned on
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2384
            if (lw > 0 && sw > 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2385
                // draw shadow as a thick and thin line with transparency
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2386
                ctx.lineWidth = sw;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2387
                ctx.strokeStyle = "rgba(0,0,0,0.1)";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2388
                // position shadow at angle from the mid of line
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2389
                var angle = Math.PI/18;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2390
                plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/2), Math.cos(angle) * (lw/2 + sw/2), series.xaxis, series.yaxis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2391
                ctx.lineWidth = sw/2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2392
                plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/4), Math.cos(angle) * (lw/2 + sw/4), series.xaxis, series.yaxis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2393
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2394
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2395
            ctx.lineWidth = lw;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2396
            ctx.strokeStyle = series.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2397
            var fillStyle = getFillStyle(series.lines, series.color, 0, plotHeight);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2398
            if (fillStyle) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2399
                ctx.fillStyle = fillStyle;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2400
                plotLineArea(series.datapoints, series.xaxis, series.yaxis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2401
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2402
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2403
            if (lw > 0)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2404
                plotLine(series.datapoints, 0, 0, series.xaxis, series.yaxis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2405
            ctx.restore();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2406
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2407
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2408
        function drawSeriesPoints(series) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2409
            function plotPoints(datapoints, radius, fillStyle, offset, shadow, axisx, axisy, symbol) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2410
                var points = datapoints.points, ps = datapoints.pointsize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2411
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2412
                for (var i = 0; i < points.length; i += ps) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2413
                    var x = points[i], y = points[i + 1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2414
                    if (x == null || x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2415
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2416
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2417
                    ctx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2418
                    x = axisx.p2c(x);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2419
                    y = axisy.p2c(y) + offset;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2420
                    if (symbol == "circle")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2421
                        ctx.arc(x, y, radius, 0, shadow ? Math.PI : Math.PI * 2, false);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2422
                    else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2423
                        symbol(ctx, x, y, radius, shadow);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2424
                    ctx.closePath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2425
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2426
                    if (fillStyle) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2427
                        ctx.fillStyle = fillStyle;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2428
                        ctx.fill();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2429
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2430
                    ctx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2431
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2432
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2433
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2434
            ctx.save();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2435
            ctx.translate(plotOffset.left, plotOffset.top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2436
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2437
            var lw = series.points.lineWidth,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2438
                sw = series.shadowSize,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2439
                radius = series.points.radius,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2440
                symbol = series.points.symbol;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2441
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2442
            // If the user sets the line width to 0, we change it to a very 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2443
            // small value. A line width of 0 seems to force the default of 1.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2444
            // Doing the conditional here allows the shadow setting to still be 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2445
            // optional even with a lineWidth of 0.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2446
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2447
            if( lw == 0 )
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2448
                lw = 0.0001;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2449
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2450
            if (lw > 0 && sw > 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2451
                // draw shadow in two steps
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2452
                var w = sw / 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2453
                ctx.lineWidth = w;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2454
                ctx.strokeStyle = "rgba(0,0,0,0.1)";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2455
                plotPoints(series.datapoints, radius, null, w + w/2, true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2456
                           series.xaxis, series.yaxis, symbol);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2457
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2458
                ctx.strokeStyle = "rgba(0,0,0,0.2)";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2459
                plotPoints(series.datapoints, radius, null, w/2, true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2460
                           series.xaxis, series.yaxis, symbol);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2461
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2462
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2463
            ctx.lineWidth = lw;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2464
            ctx.strokeStyle = series.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2465
            plotPoints(series.datapoints, radius,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2466
                       getFillStyle(series.points, series.color), 0, false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2467
                       series.xaxis, series.yaxis, symbol);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2468
            ctx.restore();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2469
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2470
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2471
        function drawBar(x, y, b, barLeft, barRight, offset, fillStyleCallback, axisx, axisy, c, horizontal, lineWidth) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2472
            var left, right, bottom, top,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2473
                drawLeft, drawRight, drawTop, drawBottom,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2474
                tmp;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2475
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2476
            // in horizontal mode, we start the bar from the left
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2477
            // instead of from the bottom so it appears to be
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2478
            // horizontal rather than vertical
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2479
            if (horizontal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2480
                drawBottom = drawRight = drawTop = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2481
                drawLeft = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2482
                left = b;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2483
                right = x;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2484
                top = y + barLeft;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2485
                bottom = y + barRight;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2486
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2487
                // account for negative bars
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2488
                if (right < left) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2489
                    tmp = right;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2490
                    right = left;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2491
                    left = tmp;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2492
                    drawLeft = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2493
                    drawRight = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2494
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2495
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2496
            else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2497
                drawLeft = drawRight = drawTop = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2498
                drawBottom = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2499
                left = x + barLeft;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2500
                right = x + barRight;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2501
                bottom = b;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2502
                top = y;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2503
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2504
                // account for negative bars
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2505
                if (top < bottom) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2506
                    tmp = top;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2507
                    top = bottom;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2508
                    bottom = tmp;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2509
                    drawBottom = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2510
                    drawTop = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2511
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2512
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2513
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2514
            // clip
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2515
            if (right < axisx.min || left > axisx.max ||
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2516
                top < axisy.min || bottom > axisy.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2517
                return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2518
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2519
            if (left < axisx.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2520
                left = axisx.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2521
                drawLeft = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2522
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2523
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2524
            if (right > axisx.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2525
                right = axisx.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2526
                drawRight = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2527
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2528
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2529
            if (bottom < axisy.min) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2530
                bottom = axisy.min;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2531
                drawBottom = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2532
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2533
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2534
            if (top > axisy.max) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2535
                top = axisy.max;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2536
                drawTop = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2537
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2538
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2539
            left = axisx.p2c(left);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2540
            bottom = axisy.p2c(bottom);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2541
            right = axisx.p2c(right);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2542
            top = axisy.p2c(top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2543
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2544
            // fill the bar
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2545
            if (fillStyleCallback) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2546
                c.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2547
                c.moveTo(left, bottom);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2548
                c.lineTo(left, top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2549
                c.lineTo(right, top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2550
                c.lineTo(right, bottom);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2551
                c.fillStyle = fillStyleCallback(bottom, top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2552
                c.fill();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2553
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2554
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2555
            // draw outline
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2556
            if (lineWidth > 0 && (drawLeft || drawRight || drawTop || drawBottom)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2557
                c.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2558
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2559
                // FIXME: inline moveTo is buggy with excanvas
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2560
                c.moveTo(left, bottom + offset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2561
                if (drawLeft)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2562
                    c.lineTo(left, top + offset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2563
                else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2564
                    c.moveTo(left, top + offset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2565
                if (drawTop)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2566
                    c.lineTo(right, top + offset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2567
                else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2568
                    c.moveTo(right, top + offset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2569
                if (drawRight)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2570
                    c.lineTo(right, bottom + offset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2571
                else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2572
                    c.moveTo(right, bottom + offset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2573
                if (drawBottom)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2574
                    c.lineTo(left, bottom + offset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2575
                else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2576
                    c.moveTo(left, bottom + offset);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2577
                c.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2578
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2579
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2580
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2581
        function drawSeriesBars(series) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2582
            function plotBars(datapoints, barLeft, barRight, offset, fillStyleCallback, axisx, axisy) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2583
                var points = datapoints.points, ps = datapoints.pointsize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2584
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2585
                for (var i = 0; i < points.length; i += ps) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2586
                    if (points[i] == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2587
                        continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2588
                    drawBar(points[i], points[i + 1], points[i + 2], barLeft, barRight, offset, fillStyleCallback, axisx, axisy, ctx, series.bars.horizontal, series.bars.lineWidth);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2589
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2590
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2591
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2592
            ctx.save();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2593
            ctx.translate(plotOffset.left, plotOffset.top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2594
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2595
            // FIXME: figure out a way to add shadows (for instance along the right edge)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2596
            ctx.lineWidth = series.bars.lineWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2597
            ctx.strokeStyle = series.color;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2598
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2599
            var barLeft;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2600
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2601
            switch (series.bars.align) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2602
                case "left":
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2603
                    barLeft = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2604
                    break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2605
                case "right":
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2606
                    barLeft = -series.bars.barWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2607
                    break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2608
                case "center":
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2609
                    barLeft = -series.bars.barWidth / 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2610
                    break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2611
                default:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2612
                    throw new Error("Invalid bar alignment: " + series.bars.align);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2613
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2614
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2615
            var fillStyleCallback = series.bars.fill ? function (bottom, top) { return getFillStyle(series.bars, series.color, bottom, top); } : null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2616
            plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, 0, fillStyleCallback, series.xaxis, series.yaxis);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2617
            ctx.restore();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2618
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2619
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2620
        function getFillStyle(filloptions, seriesColor, bottom, top) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2621
            var fill = filloptions.fill;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2622
            if (!fill)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2623
                return null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2624
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2625
            if (filloptions.fillColor)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2626
                return getColorOrGradient(filloptions.fillColor, bottom, top, seriesColor);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2627
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2628
            var c = $.color.parse(seriesColor);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2629
            c.a = typeof fill == "number" ? fill : 0.4;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2630
            c.normalize();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2631
            return c.toString();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2632
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2633
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2634
        function insertLegend() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2635
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2636
            placeholder.find(".legend").remove();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2637
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2638
            if (!options.legend.show)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2639
                return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2640
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2641
            var fragments = [], entries = [], rowStarted = false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2642
                lf = options.legend.labelFormatter, s, label;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2643
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2644
            // Build a list of legend entries, with each having a label and a color
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2645
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2646
            for (var i = 0; i < series.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2647
                s = series[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2648
                if (s.label) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2649
                    label = lf ? lf(s.label, s) : s.label;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2650
                    if (label) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2651
                        entries.push({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2652
                            label: label,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2653
                            color: s.color
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2654
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2655
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2656
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2657
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2658
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2659
            // Sort the legend using either the default or a custom comparator
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2660
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2661
            if (options.legend.sorted) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2662
                if ($.isFunction(options.legend.sorted)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2663
                    entries.sort(options.legend.sorted);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2664
                } else if (options.legend.sorted == "reverse") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2665
                	entries.reverse();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2666
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2667
                    var ascending = options.legend.sorted != "descending";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2668
                    entries.sort(function(a, b) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2669
                        return a.label == b.label ? 0 : (
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2670
                            (a.label < b.label) != ascending ? 1 : -1   // Logical XOR
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2671
                        );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2672
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2673
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2674
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2675
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2676
            // Generate markup for the list of entries, in their final order
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2677
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2678
            for (var i = 0; i < entries.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2679
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2680
                var entry = entries[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2681
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2682
                if (i % options.legend.noColumns == 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2683
                    if (rowStarted)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2684
                        fragments.push('</tr>');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2685
                    fragments.push('<tr>');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2686
                    rowStarted = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2687
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2688
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2689
                fragments.push(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2690
                    '<td class="legendColorBox"><div style="border:1px solid ' + options.legend.labelBoxBorderColor + ';padding:1px"><div style="width:4px;height:0;border:5px solid ' + entry.color + ';overflow:hidden"></div></div></td>' +
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2691
                    '<td class="legendLabel">' + entry.label + '</td>'
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2692
                );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2693
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2694
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2695
            if (rowStarted)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2696
                fragments.push('</tr>');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2697
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2698
            if (fragments.length == 0)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2699
                return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2700
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2701
            var table = '<table style="font-size:smaller;color:' + options.grid.color + '">' + fragments.join("") + '</table>';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2702
            if (options.legend.container != null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2703
                $(options.legend.container).html(table);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2704
            else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2705
                var pos = "",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2706
                    p = options.legend.position,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2707
                    m = options.legend.margin;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2708
                if (m[0] == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2709
                    m = [m, m];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2710
                if (p.charAt(0) == "n")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2711
                    pos += 'top:' + (m[1] + plotOffset.top) + 'px;';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2712
                else if (p.charAt(0) == "s")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2713
                    pos += 'bottom:' + (m[1] + plotOffset.bottom) + 'px;';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2714
                if (p.charAt(1) == "e")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2715
                    pos += 'right:' + (m[0] + plotOffset.right) + 'px;';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2716
                else if (p.charAt(1) == "w")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2717
                    pos += 'left:' + (m[0] + plotOffset.left) + 'px;';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2718
                var legend = $('<div class="legend">' + table.replace('style="', 'style="position:absolute;' + pos +';') + '</div>').appendTo(placeholder);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2719
                if (options.legend.backgroundOpacity != 0.0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2720
                    // put in the transparent background
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2721
                    // separately to avoid blended labels and
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2722
                    // label boxes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2723
                    var c = options.legend.backgroundColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2724
                    if (c == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2725
                        c = options.grid.backgroundColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2726
                        if (c && typeof c == "string")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2727
                            c = $.color.parse(c);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2728
                        else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2729
                            c = $.color.extract(legend, 'background-color');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2730
                        c.a = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2731
                        c = c.toString();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2732
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2733
                    var div = legend.children();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2734
                    $('<div style="position:absolute;width:' + div.width() + 'px;height:' + div.height() + 'px;' + pos +'background-color:' + c + ';"> </div>').prependTo(legend).css('opacity', options.legend.backgroundOpacity);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2735
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2736
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2737
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2738
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2739
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2740
        // interactive features
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2741
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2742
        var highlights = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2743
            redrawTimeout = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2744
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2745
        // returns the data item the mouse is over, or null if none is found
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2746
        function findNearbyItem(mouseX, mouseY, seriesFilter) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2747
            var maxDistance = options.grid.mouseActiveRadius,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2748
                smallestDistance = maxDistance * maxDistance + 1,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2749
                item = null, foundPoint = false, i, j, ps;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2750
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2751
            for (i = series.length - 1; i >= 0; --i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2752
                if (!seriesFilter(series[i]))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2753
                    continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2754
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2755
                var s = series[i],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2756
                    axisx = s.xaxis,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2757
                    axisy = s.yaxis,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2758
                    points = s.datapoints.points,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2759
                    mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2760
                    my = axisy.c2p(mouseY),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2761
                    maxx = maxDistance / axisx.scale,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2762
                    maxy = maxDistance / axisy.scale;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2763
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2764
                ps = s.datapoints.pointsize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2765
                // with inverse transforms, we can't use the maxx/maxy
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2766
                // optimization, sadly
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2767
                if (axisx.options.inverseTransform)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2768
                    maxx = Number.MAX_VALUE;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2769
                if (axisy.options.inverseTransform)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2770
                    maxy = Number.MAX_VALUE;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2771
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2772
                if (s.lines.show || s.points.show) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2773
                    for (j = 0; j < points.length; j += ps) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2774
                        var x = points[j], y = points[j + 1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2775
                        if (x == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2776
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2777
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2778
                        // For points and lines, the cursor must be within a
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2779
                        // certain distance to the data point
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2780
                        if (x - mx > maxx || x - mx < -maxx ||
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2781
                            y - my > maxy || y - my < -maxy)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2782
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2783
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2784
                        // We have to calculate distances in pixels, not in
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2785
                        // data units, because the scales of the axes may be different
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2786
                        var dx = Math.abs(axisx.p2c(x) - mouseX),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2787
                            dy = Math.abs(axisy.p2c(y) - mouseY),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2788
                            dist = dx * dx + dy * dy; // we save the sqrt
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2789
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2790
                        // use <= to ensure last point takes precedence
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2791
                        // (last generally means on top of)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2792
                        if (dist < smallestDistance) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2793
                            smallestDistance = dist;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2794
                            item = [i, j / ps];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2795
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2796
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2797
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2798
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2799
                if (s.bars.show && !item) { // no other point can be nearby
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2800
                    var barLeft = s.bars.align == "left" ? 0 : -s.bars.barWidth/2,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2801
                        barRight = barLeft + s.bars.barWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2802
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2803
                    for (j = 0; j < points.length; j += ps) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2804
                        var x = points[j], y = points[j + 1], b = points[j + 2];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2805
                        if (x == null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2806
                            continue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2807
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2808
                        // for a bar graph, the cursor must be inside the bar
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2809
                        if (series[i].bars.horizontal ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2810
                            (mx <= Math.max(b, x) && mx >= Math.min(b, x) &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2811
                             my >= y + barLeft && my <= y + barRight) :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2812
                            (mx >= x + barLeft && mx <= x + barRight &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2813
                             my >= Math.min(b, y) && my <= Math.max(b, y)))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2814
                                item = [i, j / ps];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2815
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2816
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2817
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2818
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2819
            if (item) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2820
                i = item[0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2821
                j = item[1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2822
                ps = series[i].datapoints.pointsize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2823
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2824
                return { datapoint: series[i].datapoints.points.slice(j * ps, (j + 1) * ps),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2825
                         dataIndex: j,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2826
                         series: series[i],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2827
                         seriesIndex: i };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2828
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2829
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2830
            return null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2831
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2832
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2833
        function onMouseMove(e) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2834
            if (options.grid.hoverable)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2835
                triggerClickHoverEvent("plothover", e,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2836
                                       function (s) { return s["hoverable"] != false; });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2837
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2838
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2839
        function onMouseLeave(e) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2840
            if (options.grid.hoverable)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2841
                triggerClickHoverEvent("plothover", e,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2842
                                       function (s) { return false; });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2843
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2844
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2845
        function onClick(e) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2846
            triggerClickHoverEvent("plotclick", e,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2847
                                   function (s) { return s["clickable"] != false; });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2848
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2849
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2850
        // trigger click or hover event (they send the same parameters
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2851
        // so we share their code)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2852
        function triggerClickHoverEvent(eventname, event, seriesFilter) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2853
            var offset = eventHolder.offset(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2854
                canvasX = event.pageX - offset.left - plotOffset.left,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2855
                canvasY = event.pageY - offset.top - plotOffset.top,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2856
            pos = canvasToAxisCoords({ left: canvasX, top: canvasY });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2857
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2858
            pos.pageX = event.pageX;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2859
            pos.pageY = event.pageY;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2860
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2861
            var item = findNearbyItem(canvasX, canvasY, seriesFilter);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2862
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2863
            if (item) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2864
                // fill in mouse pos for any listeners out there
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2865
                item.pageX = parseInt(item.series.xaxis.p2c(item.datapoint[0]) + offset.left + plotOffset.left, 10);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2866
                item.pageY = parseInt(item.series.yaxis.p2c(item.datapoint[1]) + offset.top + plotOffset.top, 10);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2867
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2868
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2869
            if (options.grid.autoHighlight) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2870
                // clear auto-highlights
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2871
                for (var i = 0; i < highlights.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2872
                    var h = highlights[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2873
                    if (h.auto == eventname &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2874
                        !(item && h.series == item.series &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2875
                          h.point[0] == item.datapoint[0] &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2876
                          h.point[1] == item.datapoint[1]))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2877
                        unhighlight(h.series, h.point);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2878
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2879
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2880
                if (item)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2881
                    highlight(item.series, item.datapoint, eventname);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2882
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2883
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2884
            placeholder.trigger(eventname, [ pos, item ]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2885
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2886
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2887
        function triggerRedrawOverlay() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2888
            var t = options.interaction.redrawOverlayInterval;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2889
            if (t == -1) {      // skip event queue
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2890
                drawOverlay();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2891
                return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2892
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2893
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2894
            if (!redrawTimeout)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2895
                redrawTimeout = setTimeout(drawOverlay, t);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2896
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2897
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2898
        function drawOverlay() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2899
            redrawTimeout = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2900
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2901
            // draw highlights
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2902
            octx.save();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2903
            overlay.clear();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2904
            octx.translate(plotOffset.left, plotOffset.top);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2905
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2906
            var i, hi;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2907
            for (i = 0; i < highlights.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2908
                hi = highlights[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2909
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2910
                if (hi.series.bars.show)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2911
                    drawBarHighlight(hi.series, hi.point);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2912
                else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2913
                    drawPointHighlight(hi.series, hi.point);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2914
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2915
            octx.restore();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2916
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2917
            executeHooks(hooks.drawOverlay, [octx]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2918
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2919
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2920
        function highlight(s, point, auto) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2921
            if (typeof s == "number")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2922
                s = series[s];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2923
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2924
            if (typeof point == "number") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2925
                var ps = s.datapoints.pointsize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2926
                point = s.datapoints.points.slice(ps * point, ps * (point + 1));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2927
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2928
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2929
            var i = indexOfHighlight(s, point);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2930
            if (i == -1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2931
                highlights.push({ series: s, point: point, auto: auto });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2932
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2933
                triggerRedrawOverlay();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2934
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2935
            else if (!auto)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2936
                highlights[i].auto = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2937
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2938
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2939
        function unhighlight(s, point) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2940
            if (s == null && point == null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2941
                highlights = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2942
                triggerRedrawOverlay();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2943
                return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2944
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2945
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2946
            if (typeof s == "number")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2947
                s = series[s];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2948
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2949
            if (typeof point == "number") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2950
                var ps = s.datapoints.pointsize;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2951
                point = s.datapoints.points.slice(ps * point, ps * (point + 1));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2952
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2953
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2954
            var i = indexOfHighlight(s, point);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2955
            if (i != -1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2956
                highlights.splice(i, 1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2957
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2958
                triggerRedrawOverlay();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2959
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2960
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2961
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2962
        function indexOfHighlight(s, p) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2963
            for (var i = 0; i < highlights.length; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2964
                var h = highlights[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2965
                if (h.series == s && h.point[0] == p[0]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2966
                    && h.point[1] == p[1])
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2967
                    return i;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2968
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2969
            return -1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2970
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2971
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2972
        function drawPointHighlight(series, point) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2973
            var x = point[0], y = point[1],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2974
                axisx = series.xaxis, axisy = series.yaxis,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2975
                highlightColor = (typeof series.highlightColor === "string") ? series.highlightColor : $.color.parse(series.color).scale('a', 0.5).toString();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2976
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2977
            if (x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2978
                return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2979
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2980
            var pointRadius = series.points.radius + series.points.lineWidth / 2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2981
            octx.lineWidth = pointRadius;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2982
            octx.strokeStyle = highlightColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2983
            var radius = 1.5 * pointRadius;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2984
            x = axisx.p2c(x);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2985
            y = axisy.p2c(y);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2986
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2987
            octx.beginPath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2988
            if (series.points.symbol == "circle")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2989
                octx.arc(x, y, radius, 0, 2 * Math.PI, false);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2990
            else
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2991
                series.points.symbol(octx, x, y, radius, false);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2992
            octx.closePath();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2993
            octx.stroke();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2994
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2995
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2996
        function drawBarHighlight(series, point) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2997
            var highlightColor = (typeof series.highlightColor === "string") ? series.highlightColor : $.color.parse(series.color).scale('a', 0.5).toString(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2998
                fillStyle = highlightColor,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2999
                barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3000
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3001
            octx.lineWidth = series.bars.lineWidth;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3002
            octx.strokeStyle = highlightColor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3003
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3004
            drawBar(point[0], point[1], point[2] || 0, barLeft, barLeft + series.bars.barWidth,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3005
                    0, function () { return fillStyle; }, series.xaxis, series.yaxis, octx, series.bars.horizontal, series.bars.lineWidth);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3006
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3007
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3008
        function getColorOrGradient(spec, bottom, top, defaultColor) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3009
            if (typeof spec == "string")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3010
                return spec;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3011
            else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3012
                // assume this is a gradient spec; IE currently only
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3013
                // supports a simple vertical gradient properly, so that's
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3014
                // what we support too
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3015
                var gradient = ctx.createLinearGradient(0, top, 0, bottom);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3016
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3017
                for (var i = 0, l = spec.colors.length; i < l; ++i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3018
                    var c = spec.colors[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3019
                    if (typeof c != "string") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3020
                        var co = $.color.parse(defaultColor);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3021
                        if (c.brightness != null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3022
                            co = co.scale('rgb', c.brightness);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3023
                        if (c.opacity != null)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3024
                            co.a *= c.opacity;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3025
                        c = co.toString();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3026
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3027
                    gradient.addColorStop(i / (l - 1), c);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3028
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3029
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3030
                return gradient;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3031
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3032
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3033
    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3034
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3035
    // Add the plot function to the top level of the jQuery object
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3036
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3037
    $.plot = function(placeholder, data, options) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3038
        //var t0 = new Date();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3039
        var plot = new Plot($(placeholder), data, options, $.plot.plugins);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3040
        //(window.console ? console.log : alert)("time used (msecs): " + ((new Date()).getTime() - t0.getTime()));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3041
        return plot;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3042
    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3043
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3044
    $.plot.version = "0.8.1";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3045
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3046
    $.plot.plugins = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3047
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3048
    // Also add the plot function as a chainable property
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3049
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3050
    $.fn.plot = function(data, options) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3051
        return this.each(function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3052
            $.plot(this, data, options);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3053
        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3054
    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3055
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3056
    // round to nearby lower multiple of base
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3057
    function floorInBase(n, base) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3058
        return base * Math.floor(n / base);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3059
    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3060
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3061
})(jQuery);