1 /* Javascript plotting library for jQuery, version 0.8.1.
3 Copyright (c) 2007-2013 IOLA and Ole Laursen.
4 Licensed under the MIT license.
8 // first an inline dependency, jquery.colorhelpers.js, we inline it here
11 /* Plugin for jQuery for working with colors.
15 * Inspiration from jQuery color animation plugin by John Resig.
17 * Released under the MIT license by Ole Laursen, October 2009.
21 * $.color.parse("#fff").scale('rgb', 0.25).add('a', -0.5).toString()
22 * var c = $.color.extract($("#mydiv"), 'background-color');
23 * console.log(c.r, c.g, c.b, c.a);
24 * $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)"
26 * Note that .scale() and .add() return the same modified object
27 * instead of making a new one.
29 * V. 1.1: Fix error handling so e.g. parsing an empty string does
30 * produce a color rather than just crashing.
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);
34 // the actual Flot code
37 // Cache the prototype hasOwnProperty for faster access
39 var hasOwnProperty = Object.prototype.hasOwnProperty;
41 ///////////////////////////////////////////////////////////////////////////
42 // The Canvas object is a wrapper around an HTML5 <canvas> tag.
45 // @param {string} cls List of classes to apply to the canvas.
46 // @param {element} container Element onto which to append the canvas.
48 // Requiring a container is a little iffy, but unfortunately canvas
49 // operations don't work unless the canvas is attached to the DOM.
51 function Canvas(cls, container) {
53 var element = container.children("." + cls)[0];
55 if (element == null) {
57 element = document.createElement("canvas");
58 element.className = cls;
60 $(element).css({ direction: "ltr", position: "absolute", left: 0, top: 0 })
63 // If HTML5 Canvas isn't available, fall back to [Ex|Flash]canvas
65 if (!element.getContext) {
66 if (window.G_vmlCanvasManager) {
67 element = window.G_vmlCanvasManager.initElement(element);
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.");
74 this.element = element;
76 var context = this.context = element.getContext("2d");
78 // Determine the screen's ratio of physical to device-independent
79 // pixels. This is the ratio between the canvas width that the browser
80 // advertises and the number of pixels actually present in that space.
82 // The iPhone 4, for example, has a device-independent width of 320px,
83 // but its screen is actually 640px wide. It therefore has a pixel
84 // ratio of 2, while most normal devices have a ratio of 1.
86 var devicePixelRatio = window.devicePixelRatio || 1,
88 context.webkitBackingStorePixelRatio ||
89 context.mozBackingStorePixelRatio ||
90 context.msBackingStorePixelRatio ||
91 context.oBackingStorePixelRatio ||
92 context.backingStorePixelRatio || 1;
94 this.pixelRatio = devicePixelRatio / backingStoreRatio;
96 // Size the canvas to match the internal dimensions of its container
98 this.resize(container.width(), container.height());
100 // Collection of HTML div layers for text overlaid onto the canvas
102 this.textContainer = null;
105 // Cache of text fragments and metrics, so we can avoid expensively
106 // re-calculating them when the plot is re-rendered in a loop.
108 this._textCache = {};
111 // Resizes the canvas to the given dimensions.
113 // @param {number} width New width of the canvas, in pixels.
114 // @param {number} width New height of the canvas, in pixels.
116 Canvas.prototype.resize = function(width, height) {
118 if (width <= 0 || height <= 0) {
119 throw new Error("Invalid dimensions for plot, width = " + width + ", height = " + height);
122 var element = this.element,
123 context = this.context,
124 pixelRatio = this.pixelRatio;
126 // Resize the canvas, increasing its density based on the display's
127 // pixel ratio; basically giving it more pixels without increasing the
128 // size of its element, to take advantage of the fact that retina
129 // displays have that many more pixels in the same advertised space.
131 // Resizing should reset the state (excanvas seems to be buggy though)
133 if (this.width != width) {
134 element.width = width * pixelRatio;
135 element.style.width = width + "px";
139 if (this.height != height) {
140 element.height = height * pixelRatio;
141 element.style.height = height + "px";
142 this.height = height;
145 // Save the context, so we can reset in case we get replotted. The
146 // restore ensure that we're really back at the initial state, and
147 // should be safe even if we haven't saved the initial state yet.
152 // Scale the coordinate space to match the display density; so even though we
153 // may have twice as many pixels, we still want lines and other drawing to
154 // appear at the same size; the extra pixels will just make them crisper.
156 context.scale(pixelRatio, pixelRatio);
159 // Clears the entire canvas area, not including any overlaid HTML text
161 Canvas.prototype.clear = function() {
162 this.context.clearRect(0, 0, this.width, this.height);
165 // Finishes rendering the canvas, including managing the text overlay.
167 Canvas.prototype.render = function() {
169 var cache = this._textCache;
171 // For each text layer, add elements marked as active that haven't
172 // already been rendered, and remove those that are no longer active.
174 for (var layerKey in cache) {
175 if (hasOwnProperty.call(cache, layerKey)) {
177 var layer = this.getTextLayer(layerKey),
178 layerCache = cache[layerKey];
182 for (var styleKey in layerCache) {
183 if (hasOwnProperty.call(layerCache, styleKey)) {
184 var styleCache = layerCache[styleKey];
185 for (var key in styleCache) {
186 if (hasOwnProperty.call(styleCache, key)) {
188 var positions = styleCache[key].positions;
190 for (var i = 0, position; position = positions[i]; i++) {
191 if (position.active) {
192 if (!position.rendered) {
193 layer.append(position.element);
194 position.rendered = true;
197 positions.splice(i--, 1);
198 if (position.rendered) {
199 position.element.detach();
204 if (positions.length == 0) {
205 delete styleCache[key];
217 // Creates (if necessary) and returns the text overlay container.
219 // @param {string} classes String of space-separated CSS classes used to
220 // uniquely identify the text layer.
221 // @return {object} The jQuery-wrapped text-layer div.
223 Canvas.prototype.getTextLayer = function(classes) {
225 var layer = this.text[classes];
227 // Create the text layer if it doesn't exist
231 // Create the text layer container, if it doesn't exist
233 if (this.textContainer == null) {
234 this.textContainer = $("<div class='flot-text'></div>")
236 position: "absolute",
241 'font-size': "smaller",
244 .insertAfter(this.element);
247 layer = this.text[classes] = $("<div></div>")
250 position: "absolute",
256 .appendTo(this.textContainer);
262 // Creates (if necessary) and returns a text info object.
264 // The object looks like this:
267 // width: Width of the text's wrapper div.
268 // height: Height of the text's wrapper div.
269 // element: The jQuery-wrapped HTML div containing the text.
270 // positions: Array of positions at which this text is drawn.
273 // The positions array contains objects that look like this:
276 // active: Flag indicating whether the text should be visible.
277 // rendered: Flag indicating whether the text is currently visible.
278 // element: The jQuery-wrapped HTML div containing the text.
279 // x: X coordinate at which to draw the text.
280 // y: Y coordinate at which to draw the text.
283 // Each position after the first receives a clone of the original element.
285 // The idea is that that the width, height, and general 'identity' of the
286 // text is constant no matter where it is placed; the placements are a
287 // secondary property.
289 // Canvas maintains a cache of recently-used text info objects; getTextInfo
290 // either returns the cached element or creates a new entry.
292 // @param {string} layer A string of space-separated CSS classes uniquely
293 // identifying the layer containing this text.
294 // @param {string} text Text string to retrieve info for.
295 // @param {(string|object)=} font Either a string of space-separated CSS
296 // classes or a font-spec object, defining the text's font and style.
297 // @param {number=} angle Angle at which to rotate the text, in degrees.
298 // Angle is currently unused, it will be implemented in the future.
299 // @param {number=} width Maximum width of the text before it wraps.
300 // @return {object} a text info object.
302 Canvas.prototype.getTextInfo = function(layer, text, font, angle, width) {
304 var textStyle, layerCache, styleCache, info;
306 // Cast the value to a string, in case we were given a number or such
310 // If the font is a font-spec object, generate a CSS font definition
312 if (typeof font === "object") {
313 textStyle = font.style + " " + font.variant + " " + font.weight + " " + font.size + "px/" + font.lineHeight + "px " + font.family;
318 // Retrieve (or create) the cache for the text's layer and styles
320 layerCache = this._textCache[layer];
322 if (layerCache == null) {
323 layerCache = this._textCache[layer] = {};
326 styleCache = layerCache[textStyle];
328 if (styleCache == null) {
329 styleCache = layerCache[textStyle] = {};
332 info = styleCache[text];
334 // If we can't find a matching element in our cache, create a new one
338 var element = $("<div></div>").html(text)
340 position: "absolute",
344 .appendTo(this.getTextLayer(layer));
346 if (typeof font === "object") {
351 } else if (typeof font === "string") {
352 element.addClass(font);
355 info = styleCache[text] = {
356 width: element.outerWidth(true),
357 height: element.outerHeight(true),
368 // Adds a text string to the canvas text overlay.
370 // The text isn't drawn immediately; it is marked as rendering, which will
371 // result in its addition to the canvas on the next render pass.
373 // @param {string} layer A string of space-separated CSS classes uniquely
374 // identifying the layer containing this text.
375 // @param {number} x X coordinate at which to draw the text.
376 // @param {number} y Y coordinate at which to draw the text.
377 // @param {string} text Text string to draw.
378 // @param {(string|object)=} font Either a string of space-separated CSS
379 // classes or a font-spec object, defining the text's font and style.
380 // @param {number=} angle Angle at which to rotate the text, in degrees.
381 // Angle is currently unused, it will be implemented in the future.
382 // @param {number=} width Maximum width of the text before it wraps.
383 // @param {string=} halign Horizontal alignment of the text; either "left",
384 // "center" or "right".
385 // @param {string=} valign Vertical alignment of the text; either "top",
386 // "middle" or "bottom".
388 Canvas.prototype.addText = function(layer, x, y, text, font, angle, width, halign, valign) {
390 var info = this.getTextInfo(layer, text, font, angle, width),
391 positions = info.positions;
393 // Tweak the div's position to match the text's alignment
395 if (halign == "center") {
397 } else if (halign == "right") {
401 if (valign == "middle") {
402 y -= info.height / 2;
403 } else if (valign == "bottom") {
407 // Determine whether this text already exists at this position.
408 // If so, mark it for inclusion in the next render pass.
410 for (var i = 0, position; position = positions[i]; i++) {
411 if (position.x == x && position.y == y) {
412 position.active = true;
417 // If the text doesn't exist at this position, create a new entry
419 // For the very first position we'll re-use the original element,
420 // while for subsequent ones we'll clone it.
425 element: positions.length ? info.element.clone() : info.element,
430 positions.push(position);
432 // Move the element to its final position within the container
434 position.element.css({
437 'text-align': halign // In case the text wraps
441 // Removes one or more text strings from the canvas text overlay.
443 // If no parameters are given, all text within the layer is removed.
445 // Note that the text is not immediately removed; it is simply marked as
446 // inactive, which will result in its removal on the next render pass.
447 // This avoids the performance penalty for 'clear and redraw' behavior,
448 // where we potentially get rid of all text on a layer, but will likely
449 // add back most or all of it later, as when redrawing axes, for example.
451 // @param {string} layer A string of space-separated CSS classes uniquely
452 // identifying the layer containing this text.
453 // @param {number=} x X coordinate of the text.
454 // @param {number=} y Y coordinate of the text.
455 // @param {string=} text Text string to remove.
456 // @param {(string|object)=} font Either a string of space-separated CSS
457 // classes or a font-spec object, defining the text's font and style.
458 // @param {number=} angle Angle at which the text is rotated, in degrees.
459 // Angle is currently unused, it will be implemented in the future.
461 Canvas.prototype.removeText = function(layer, x, y, text, font, angle) {
463 var layerCache = this._textCache[layer];
464 if (layerCache != null) {
465 for (var styleKey in layerCache) {
466 if (hasOwnProperty.call(layerCache, styleKey)) {
467 var styleCache = layerCache[styleKey];
468 for (var key in styleCache) {
469 if (hasOwnProperty.call(styleCache, key)) {
470 var positions = styleCache[key].positions;
471 for (var i = 0, position; position = positions[i]; i++) {
472 position.active = false;
480 var positions = this.getTextInfo(layer, text, font, angle).positions;
481 for (var i = 0, position; position = positions[i]; i++) {
482 if (position.x == x && position.y == y) {
483 position.active = false;
489 ///////////////////////////////////////////////////////////////////////////
490 // The top-level container for the entire plot.
492 function Plot(placeholder, data_, options_, plugins) {
493 // data is on the form:
494 // [ series1, series2 ... ]
495 // where series is either just the data as [ [x1, y1], [x2, y2], ... ]
496 // or { data: [ [x1, y1], [x2, y2], ... ], label: "some label", ... }
500 // the color theme used for graphs
501 colors: ["#edc240", "#afd8f8", "#cb4b4b", "#4da74d", "#9440ed"],
504 noColumns: 1, // number of colums in legend table
505 labelFormatter: null, // fn: string -> string
506 labelBoxBorderColor: "#ccc", // border color for the little label boxes
507 container: null, // container (as jQuery object) to put legend in, null means default on top of graph
508 position: "ne", // position of default legend container within plot
509 margin: 5, // distance from grid edge to default legend container within plot
510 backgroundColor: null, // null means auto-detect
511 backgroundOpacity: 0.85, // set to 0 to avoid background
512 sorted: null // default to no legend sorting
515 show: null, // null = auto-detect, true = always, false = never
516 position: "bottom", // or "top"
517 mode: null, // null or "time"
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" }
519 color: null, // base color, labels, ticks
520 tickColor: null, // possibly different color of ticks, e.g. "rgba(0,0,0,0.15)"
521 transform: null, // null or f: number -> number to transform axis
522 inverseTransform: null, // if transform is set, this should be the inverse function
523 min: null, // min. value to show, null means set automatically
524 max: null, // max. value to show, null means set automatically
525 autoscaleMargin: null, // margin in % to add if auto-setting min/max
526 ticks: null, // either [1, 3] or [[1, "a"], 3] or (fn: axis info -> ticks) or app. number of ticks for auto-ticks
527 tickFormatter: null, // fn: number -> string
528 labelWidth: null, // size of tick labels in pixels
530 reserveSpace: null, // whether to reserve space even if axis isn't shown
531 tickLength: null, // size in pixels of ticks, or "full" for whole line
532 alignTicksWithAxis: null, // axis number or null for no sync
533 tickDecimals: null, // no. of decimals, null means auto
534 tickSize: null, // number or [number, "unit"]
535 minTickSize: null // number or [number, "unit"]
538 autoscaleMargin: 0.02,
539 position: "left" // or "right"
547 lineWidth: 2, // in pixels
549 fillColor: "#ffffff",
550 symbol: "circle" // or callback
553 // we don't put in show: false so we can see
554 // whether lines were actively disabled
555 lineWidth: 2, // in pixels
559 // Omit 'zero', so we can later default its value to
560 // match that of the 'fill' option.
564 lineWidth: 2, // in pixels
565 barWidth: 1, // in units of the x axis
568 align: "left", // "left", "right", or "center"
578 color: "#545454", // primary color used for outline and labels
579 backgroundColor: null, // null for transparent, else color
580 borderColor: null, // set if different from the grid color
581 tickColor: null, // color for the ticks, e.g. "rgba(0,0,0,0.15)"
582 margin: 0, // distance from the canvas edge to the grid
583 labelMargin: 5, // in pixels
584 axisMargin: 8, // in pixels
585 borderWidth: 2, // in pixels
586 minBorderMargin: null, // in pixels, null means taken from points radius
587 markings: null, // array of ranges or fn: axes -> array of ranges
588 markingsColor: "#f4f4f4",
589 markingsLineWidth: 2,
593 autoHighlight: true, // highlight in case mouse is near
594 mouseActiveRadius: 10 // how far the mouse can be away to activate an item
597 redrawOverlayInterval: 1000/60 // time between updates, -1 means in same flow
601 surface = null, // the canvas for the plot itself
602 overlay = null, // canvas for interactive stuff on top of plot
603 eventHolder = null, // jQuery object that events should be bound to
604 ctx = null, octx = null,
605 xaxes = [], yaxes = [],
606 plotOffset = { left: 0, right: 0, top: 0, bottom: 0},
607 plotWidth = 0, plotHeight = 0,
611 processDatapoints: [],
623 plot.setData = setData;
624 plot.setupGrid = setupGrid;
626 plot.getPlaceholder = function() { return placeholder; };
627 plot.getCanvas = function() { return surface.element; };
628 plot.getPlotOffset = function() { return plotOffset; };
629 plot.width = function () { return plotWidth; };
630 plot.height = function () { return plotHeight; };
631 plot.offset = function () {
632 var o = eventHolder.offset();
633 o.left += plotOffset.left;
634 o.top += plotOffset.top;
637 plot.getData = function () { return series; };
638 plot.getAxes = function () {
640 $.each(xaxes.concat(yaxes), function (_, axis) {
642 res[axis.direction + (axis.n != 1 ? axis.n : "") + "axis"] = axis;
646 plot.getXAxes = function () { return xaxes; };
647 plot.getYAxes = function () { return yaxes; };
648 plot.c2p = canvasToAxisCoords;
649 plot.p2c = axisToCanvasCoords;
650 plot.getOptions = function () { return options; };
651 plot.highlight = highlight;
652 plot.unhighlight = unhighlight;
653 plot.triggerRedrawOverlay = triggerRedrawOverlay;
654 plot.pointOffset = function(point) {
656 left: parseInt(xaxes[axisNumber(point, "x") - 1].p2c(+point.x) + plotOffset.left, 10),
657 top: parseInt(yaxes[axisNumber(point, "y") - 1].p2c(+point.y) + plotOffset.top, 10)
660 plot.shutdown = shutdown;
661 plot.resize = function () {
662 var width = placeholder.width(),
663 height = placeholder.height();
664 surface.resize(width, height);
665 overlay.resize(width, height);
673 parseOptions(options_);
681 function executeHooks(hook, args) {
682 args = [plot].concat(args);
683 for (var i = 0; i < hook.length; ++i)
684 hook[i].apply(this, args);
687 function initPlugins() {
689 // References to key classes, allowing plugins to modify them
695 for (var i = 0; i < plugins.length; ++i) {
697 p.init(plot, classes);
699 $.extend(true, options, p.options);
703 function parseOptions(opts) {
705 $.extend(true, options, opts);
707 // $.extend merges arrays, rather than replacing them. When less
708 // colors are provided than the size of the default palette, we
709 // end up with those colors plus the remaining defaults, which is
710 // not expected behavior; avoid it by replacing them here.
712 if (opts && opts.colors) {
713 options.colors = opts.colors;
716 if (options.xaxis.color == null)
717 options.xaxis.color = $.color.parse(options.grid.color).scale('a', 0.22).toString();
718 if (options.yaxis.color == null)
719 options.yaxis.color = $.color.parse(options.grid.color).scale('a', 0.22).toString();
721 if (options.xaxis.tickColor == null) // grid.tickColor for back-compatibility
722 options.xaxis.tickColor = options.grid.tickColor || options.xaxis.color;
723 if (options.yaxis.tickColor == null) // grid.tickColor for back-compatibility
724 options.yaxis.tickColor = options.grid.tickColor || options.yaxis.color;
726 if (options.grid.borderColor == null)
727 options.grid.borderColor = options.grid.color;
728 if (options.grid.tickColor == null)
729 options.grid.tickColor = $.color.parse(options.grid.color).scale('a', 0.22).toString();
731 // Fill in defaults for axis options, including any unspecified
732 // font-spec fields, if a font-spec was provided.
734 // If no x/y axis options were provided, create one of each anyway,
735 // since the rest of the code assumes that they exist.
737 var i, axisOptions, axisCount,
739 style: placeholder.css("font-style"),
740 size: Math.round(0.8 * (+placeholder.css("font-size").replace("px", "") || 13)),
741 variant: placeholder.css("font-variant"),
742 weight: placeholder.css("font-weight"),
743 family: placeholder.css("font-family")
746 fontDefaults.lineHeight = fontDefaults.size * 1.15;
748 axisCount = options.xaxes.length || 1;
749 for (i = 0; i < axisCount; ++i) {
751 axisOptions = options.xaxes[i];
752 if (axisOptions && !axisOptions.tickColor) {
753 axisOptions.tickColor = axisOptions.color;
756 axisOptions = $.extend(true, {}, options.xaxis, axisOptions);
757 options.xaxes[i] = axisOptions;
759 if (axisOptions.font) {
760 axisOptions.font = $.extend({}, fontDefaults, axisOptions.font);
761 if (!axisOptions.font.color) {
762 axisOptions.font.color = axisOptions.color;
767 axisCount = options.yaxes.length || 1;
768 for (i = 0; i < axisCount; ++i) {
770 axisOptions = options.yaxes[i];
771 if (axisOptions && !axisOptions.tickColor) {
772 axisOptions.tickColor = axisOptions.color;
775 axisOptions = $.extend(true, {}, options.yaxis, axisOptions);
776 options.yaxes[i] = axisOptions;
778 if (axisOptions.font) {
779 axisOptions.font = $.extend({}, fontDefaults, axisOptions.font);
780 if (!axisOptions.font.color) {
781 axisOptions.font.color = axisOptions.color;
786 // backwards compatibility, to be removed in future
787 if (options.xaxis.noTicks && options.xaxis.ticks == null)
788 options.xaxis.ticks = options.xaxis.noTicks;
789 if (options.yaxis.noTicks && options.yaxis.ticks == null)
790 options.yaxis.ticks = options.yaxis.noTicks;
791 if (options.x2axis) {
792 options.xaxes[1] = $.extend(true, {}, options.xaxis, options.x2axis);
793 options.xaxes[1].position = "top";
795 if (options.y2axis) {
796 options.yaxes[1] = $.extend(true, {}, options.yaxis, options.y2axis);
797 options.yaxes[1].position = "right";
799 if (options.grid.coloredAreas)
800 options.grid.markings = options.grid.coloredAreas;
801 if (options.grid.coloredAreasColor)
802 options.grid.markingsColor = options.grid.coloredAreasColor;
804 $.extend(true, options.series.lines, options.lines);
806 $.extend(true, options.series.points, options.points);
808 $.extend(true, options.series.bars, options.bars);
809 if (options.shadowSize != null)
810 options.series.shadowSize = options.shadowSize;
811 if (options.highlightColor != null)
812 options.series.highlightColor = options.highlightColor;
814 // save options on axes for future reference
815 for (i = 0; i < options.xaxes.length; ++i)
816 getOrCreateAxis(xaxes, i + 1).options = options.xaxes[i];
817 for (i = 0; i < options.yaxes.length; ++i)
818 getOrCreateAxis(yaxes, i + 1).options = options.yaxes[i];
820 // add hooks from options
822 if (options.hooks[n] && options.hooks[n].length)
823 hooks[n] = hooks[n].concat(options.hooks[n]);
825 executeHooks(hooks.processOptions, [options]);
828 function setData(d) {
829 series = parseData(d);
830 fillInSeriesOptions();
834 function parseData(d) {
836 for (var i = 0; i < d.length; ++i) {
837 var s = $.extend(true, {}, options.series);
839 if (d[i].data != null) {
840 s.data = d[i].data; // move the data instead of deep-copy
843 $.extend(true, s, d[i]);
855 function axisNumber(obj, coord) {
856 var a = obj[coord + "axis"];
857 if (typeof a == "object") // if we got a real axis, extract number
859 if (typeof a != "number")
860 a = 1; // default to first axis
865 // return flat array without annoying null entries
866 return $.grep(xaxes.concat(yaxes), function (a) { return a; });
869 function canvasToAxisCoords(pos) {
870 // return an object with x/y corresponding to all used axes
871 var res = {}, i, axis;
872 for (i = 0; i < xaxes.length; ++i) {
874 if (axis && axis.used)
875 res["x" + axis.n] = axis.c2p(pos.left);
878 for (i = 0; i < yaxes.length; ++i) {
880 if (axis && axis.used)
881 res["y" + axis.n] = axis.c2p(pos.top);
884 if (res.x1 !== undefined)
886 if (res.y1 !== undefined)
892 function axisToCanvasCoords(pos) {
893 // get canvas coords from the first pair of x/y found in pos
894 var res = {}, i, axis, key;
896 for (i = 0; i < xaxes.length; ++i) {
898 if (axis && axis.used) {
900 if (pos[key] == null && axis.n == 1)
903 if (pos[key] != null) {
904 res.left = axis.p2c(pos[key]);
910 for (i = 0; i < yaxes.length; ++i) {
912 if (axis && axis.used) {
914 if (pos[key] == null && axis.n == 1)
917 if (pos[key] != null) {
918 res.top = axis.p2c(pos[key]);
927 function getOrCreateAxis(axes, number) {
928 if (!axes[number - 1])
930 n: number, // save the number for future reference
931 direction: axes == xaxes ? "x" : "y",
932 options: $.extend(true, {}, axes == xaxes ? options.xaxis : options.yaxis)
935 return axes[number - 1];
938 function fillInSeriesOptions() {
940 var neededColors = series.length, maxIndex = -1, i;
942 // Subtract the number of series that already have fixed colors or
943 // color indexes from the number that we still need to generate.
945 for (i = 0; i < series.length; ++i) {
946 var sc = series[i].color;
949 if (typeof sc == "number" && sc > maxIndex) {
955 // If any of the series have fixed color indexes, then we need to
956 // generate at least as many colors as the highest index.
958 if (neededColors <= maxIndex) {
959 neededColors = maxIndex + 1;
962 // Generate all the colors, using first the option colors and then
963 // variations on those colors once they're exhausted.
965 var c, colors = [], colorPool = options.colors,
966 colorPoolSize = colorPool.length, variation = 0;
968 for (i = 0; i < neededColors; i++) {
970 c = $.color.parse(colorPool[i % colorPoolSize] || "#666");
972 // Each time we exhaust the colors in the pool we adjust
973 // a scaling factor used to produce more variations on
974 // those colors. The factor alternates negative/positive
975 // to produce lighter/darker colors.
977 // Reset the variation after every few cycles, or else
978 // it will end up producing only white or black colors.
980 if (i % colorPoolSize == 0 && i) {
981 if (variation >= 0) {
982 if (variation < 0.5) {
983 variation = -variation - 0.2;
984 } else variation = 0;
985 } else variation = -variation;
988 colors[i] = c.scale('rgb', 1 + variation);
991 // Finalize the series options, filling in their colors
994 for (i = 0; i < series.length; ++i) {
998 if (s.color == null) {
999 s.color = colors[colori].toString();
1002 else if (typeof s.color == "number")
1003 s.color = colors[s.color].toString();
1005 // turn on lines automatically in case nothing is set
1006 if (s.lines.show == null) {
1009 if (s[v] && s[v].show) {
1014 s.lines.show = true;
1017 // If nothing was provided for lines.zero, default it to match
1018 // lines.fill, since areas by default should extend to zero.
1020 if (s.lines.zero == null) {
1021 s.lines.zero = !!s.lines.fill;
1025 s.xaxis = getOrCreateAxis(xaxes, axisNumber(s, "x"));
1026 s.yaxis = getOrCreateAxis(yaxes, axisNumber(s, "y"));
1030 function processData() {
1031 var topSentry = Number.POSITIVE_INFINITY,
1032 bottomSentry = Number.NEGATIVE_INFINITY,
1033 fakeInfinity = Number.MAX_VALUE,
1035 s, points, ps, x, y, axis, val, f, p,
1038 function updateAxis(axis, min, max) {
1039 if (min < axis.datamin && min != -fakeInfinity)
1041 if (max > axis.datamax && max != fakeInfinity)
1045 $.each(allAxes(), function (_, axis) {
1047 axis.datamin = topSentry;
1048 axis.datamax = bottomSentry;
1052 for (i = 0; i < series.length; ++i) {
1054 s.datapoints = { points: [] };
1056 executeHooks(hooks.processRawData, [ s, s.data, s.datapoints ]);
1059 // first pass: clean and copy data
1060 for (i = 0; i < series.length; ++i) {
1064 format = s.datapoints.format;
1068 // find out how to copy
1069 format.push({ x: true, number: true, required: true });
1070 format.push({ y: true, number: true, required: true });
1072 if (s.bars.show || (s.lines.show && s.lines.fill)) {
1073 var autoscale = !!((s.bars.show && s.bars.zero) || (s.lines.show && s.lines.zero));
1074 format.push({ y: true, number: true, required: false, defaultValue: 0, autoscale: autoscale });
1075 if (s.bars.horizontal) {
1076 delete format[format.length - 1].y;
1077 format[format.length - 1].x = true;
1081 s.datapoints.format = format;
1084 if (s.datapoints.pointsize != null)
1085 continue; // already filled in
1087 s.datapoints.pointsize = format.length;
1089 ps = s.datapoints.pointsize;
1090 points = s.datapoints.points;
1092 var insertSteps = s.lines.show && s.lines.steps;
1093 s.xaxis.used = s.yaxis.used = true;
1095 for (j = k = 0; j < data.length; ++j, k += ps) {
1098 var nullify = p == null;
1100 for (m = 0; m < ps; ++m) {
1105 if (f.number && val != null) {
1106 val = +val; // convert to number
1109 else if (val == Infinity)
1111 else if (val == -Infinity)
1112 val = -fakeInfinity;
1119 if (f.defaultValue != null)
1120 val = f.defaultValue;
1124 points[k + m] = val;
1129 for (m = 0; m < ps; ++m) {
1130 val = points[k + m];
1133 // extract min/max info
1136 updateAxis(s.xaxis, val, val);
1139 updateAxis(s.yaxis, val, val);
1143 points[k + m] = null;
1147 // a little bit of line specific stuff that
1148 // perhaps shouldn't be here, but lacking
1150 if (insertSteps && k > 0
1151 && points[k - ps] != null
1152 && points[k - ps] != points[k]
1153 && points[k - ps + 1] != points[k + 1]) {
1154 // copy the point to make room for a middle point
1155 for (m = 0; m < ps; ++m)
1156 points[k + ps + m] = points[k + m];
1158 // middle point has same y
1159 points[k + 1] = points[k - ps + 1];
1161 // we've added a point, better reflect that
1168 // give the hooks a chance to run
1169 for (i = 0; i < series.length; ++i) {
1172 executeHooks(hooks.processDatapoints, [ s, s.datapoints]);
1175 // second pass: find datamax/datamin for auto-scaling
1176 for (i = 0; i < series.length; ++i) {
1178 points = s.datapoints.points;
1179 ps = s.datapoints.pointsize;
1180 format = s.datapoints.format;
1182 var xmin = topSentry, ymin = topSentry,
1183 xmax = bottomSentry, ymax = bottomSentry;
1185 for (j = 0; j < points.length; j += ps) {
1186 if (points[j] == null)
1189 for (m = 0; m < ps; ++m) {
1190 val = points[j + m];
1192 if (!f || f.autoscale === false || val == fakeInfinity || val == -fakeInfinity)
1211 // make sure we got room for the bar on the dancing floor
1214 switch (s.bars.align) {
1219 delta = -s.bars.barWidth;
1222 delta = -s.bars.barWidth / 2;
1225 throw new Error("Invalid bar alignment: " + s.bars.align);
1228 if (s.bars.horizontal) {
1230 ymax += delta + s.bars.barWidth;
1234 xmax += delta + s.bars.barWidth;
1238 updateAxis(s.xaxis, xmin, xmax);
1239 updateAxis(s.yaxis, ymin, ymax);
1242 $.each(allAxes(), function (_, axis) {
1243 if (axis.datamin == topSentry)
1244 axis.datamin = null;
1245 if (axis.datamax == bottomSentry)
1246 axis.datamax = null;
1250 function setupCanvases() {
1252 // Make sure the placeholder is clear of everything except canvases
1253 // from a previous plot in this container that we'll try to re-use.
1255 placeholder.css("padding", 0) // padding messes up the positioning
1256 .children(":not(.flot-base,.flot-overlay)").remove();
1258 if (placeholder.css("position") == 'static')
1259 placeholder.css("position", "relative"); // for positioning labels and overlay
1261 surface = new Canvas("flot-base", placeholder);
1262 overlay = new Canvas("flot-overlay", placeholder); // overlay canvas for interactive features
1264 ctx = surface.context;
1265 octx = overlay.context;
1267 // define which element we're listening for events on
1268 eventHolder = $(overlay.element).unbind();
1270 // If we're re-using a plot object, shut down the old one
1272 var existing = placeholder.data("plot");
1275 existing.shutdown();
1279 // save in case we get replotted
1280 placeholder.data("plot", plot);
1283 function bindEvents() {
1285 if (options.grid.hoverable) {
1286 eventHolder.mousemove(onMouseMove);
1288 // Use bind, rather than .mouseleave, because we officially
1289 // still support jQuery 1.2.6, which doesn't define a shortcut
1290 // for mouseenter or mouseleave. This was a bug/oversight that
1291 // was fixed somewhere around 1.3.x. We can return to using
1292 // .mouseleave when we drop support for 1.2.6.
1294 eventHolder.bind("mouseleave", onMouseLeave);
1297 if (options.grid.clickable)
1298 eventHolder.click(onClick);
1300 executeHooks(hooks.bindEvents, [eventHolder]);
1303 function shutdown() {
1305 clearTimeout(redrawTimeout);
1307 eventHolder.unbind("mousemove", onMouseMove);
1308 eventHolder.unbind("mouseleave", onMouseLeave);
1309 eventHolder.unbind("click", onClick);
1311 executeHooks(hooks.shutdown, [eventHolder]);
1314 function setTransformationHelpers(axis) {
1315 // set helper functions on the axis, assumes plot area
1316 // has been computed already
1318 function identity(x) { return x; }
1320 var s, m, t = axis.options.transform || identity,
1321 it = axis.options.inverseTransform;
1323 // precompute how much the axis is scaling a point
1325 if (axis.direction == "x") {
1326 s = axis.scale = plotWidth / Math.abs(t(axis.max) - t(axis.min));
1327 m = Math.min(t(axis.max), t(axis.min));
1330 s = axis.scale = plotHeight / Math.abs(t(axis.max) - t(axis.min));
1332 m = Math.max(t(axis.max), t(axis.min));
1335 // data point to canvas coordinate
1336 if (t == identity) // slight optimization
1337 axis.p2c = function (p) { return (p - m) * s; };
1339 axis.p2c = function (p) { return (t(p) - m) * s; };
1340 // canvas coordinate to data point
1342 axis.c2p = function (c) { return m + c / s; };
1344 axis.c2p = function (c) { return it(m + c / s); };
1347 function measureTickLabels(axis) {
1349 var opts = axis.options,
1350 ticks = axis.ticks || [],
1351 labelWidth = opts.labelWidth || 0,
1352 labelHeight = opts.labelHeight || 0,
1353 maxWidth = labelWidth || axis.direction == "x" ? Math.floor(surface.width / (ticks.length || 1)) : null;
1354 legacyStyles = axis.direction + "Axis " + axis.direction + axis.n + "Axis",
1355 layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + legacyStyles,
1356 font = opts.font || "flot-tick-label tickLabel";
1358 for (var i = 0; i < ticks.length; ++i) {
1365 var info = surface.getTextInfo(layer, t.label, font, null, maxWidth);
1367 labelWidth = Math.max(labelWidth, info.width);
1368 labelHeight = Math.max(labelHeight, info.height);
1371 axis.labelWidth = opts.labelWidth || labelWidth;
1372 axis.labelHeight = opts.labelHeight || labelHeight;
1375 function allocateAxisBoxFirstPhase(axis) {
1376 // find the bounding box of the axis by looking at label
1377 // widths/heights and ticks, make room by diminishing the
1378 // plotOffset; this first phase only looks at one
1379 // dimension per axis, the other dimension depends on the
1380 // other axes so will have to wait
1382 var lw = axis.labelWidth,
1383 lh = axis.labelHeight,
1384 pos = axis.options.position,
1385 tickLength = axis.options.tickLength,
1386 axisMargin = options.grid.axisMargin,
1387 padding = options.grid.labelMargin,
1388 all = axis.direction == "x" ? xaxes : yaxes,
1391 // determine axis margin
1392 var samePosition = $.grep(all, function (a) {
1393 return a && a.options.position == pos && a.reserveSpace;
1395 if ($.inArray(axis, samePosition) == samePosition.length - 1)
1396 axisMargin = 0; // outermost
1398 // determine tick length - if we're innermost, we can use "full"
1399 if (tickLength == null) {
1400 var sameDirection = $.grep(all, function (a) {
1401 return a && a.reserveSpace;
1404 innermost = $.inArray(axis, sameDirection) == 0;
1406 tickLength = "full";
1411 if (!isNaN(+tickLength))
1412 padding += +tickLength;
1415 if (axis.direction == "x") {
1418 if (pos == "bottom") {
1419 plotOffset.bottom += lh + axisMargin;
1420 axis.box = { top: surface.height - plotOffset.bottom, height: lh };
1423 axis.box = { top: plotOffset.top + axisMargin, height: lh };
1424 plotOffset.top += lh + axisMargin;
1430 if (pos == "left") {
1431 axis.box = { left: plotOffset.left + axisMargin, width: lw };
1432 plotOffset.left += lw + axisMargin;
1435 plotOffset.right += lw + axisMargin;
1436 axis.box = { left: surface.width - plotOffset.right, width: lw };
1440 // save for future reference
1441 axis.position = pos;
1442 axis.tickLength = tickLength;
1443 axis.box.padding = padding;
1444 axis.innermost = innermost;
1447 function allocateAxisBoxSecondPhase(axis) {
1448 // now that all axis boxes have been placed in one
1449 // dimension, we can set the remaining dimension coordinates
1450 if (axis.direction == "x") {
1451 axis.box.left = plotOffset.left - axis.labelWidth / 2;
1452 axis.box.width = surface.width - plotOffset.left - plotOffset.right + axis.labelWidth;
1455 axis.box.top = plotOffset.top - axis.labelHeight / 2;
1456 axis.box.height = surface.height - plotOffset.bottom - plotOffset.top + axis.labelHeight;
1460 function adjustLayoutForThingsStickingOut() {
1461 // possibly adjust plot offset to ensure everything stays
1462 // inside the canvas and isn't clipped off
1464 var minMargin = options.grid.minBorderMargin,
1465 margins = { x: 0, y: 0 }, i, axis;
1467 // check stuff from the plot (FIXME: this should just read
1468 // a value from the series, otherwise it's impossible to
1470 if (minMargin == null) {
1472 for (i = 0; i < series.length; ++i)
1473 minMargin = Math.max(minMargin, 2 * (series[i].points.radius + series[i].points.lineWidth/2));
1476 margins.x = margins.y = Math.ceil(minMargin);
1478 // check axis labels, note we don't check the actual
1479 // labels but instead use the overall width/height to not
1480 // jump as much around with replots
1481 $.each(allAxes(), function (_, axis) {
1482 var dir = axis.direction;
1483 if (axis.reserveSpace)
1484 margins[dir] = Math.ceil(Math.max(margins[dir], (dir == "x" ? axis.labelWidth : axis.labelHeight) / 2));
1487 plotOffset.left = Math.max(margins.x, plotOffset.left);
1488 plotOffset.right = Math.max(margins.x, plotOffset.right);
1489 plotOffset.top = Math.max(margins.y, plotOffset.top);
1490 plotOffset.bottom = Math.max(margins.y, plotOffset.bottom);
1493 function setupGrid() {
1494 var i, axes = allAxes(), showGrid = options.grid.show;
1496 // Initialize the plot's offset from the edge of the canvas
1498 for (var a in plotOffset) {
1499 var margin = options.grid.margin || 0;
1500 plotOffset[a] = typeof margin == "number" ? margin : margin[a] || 0;
1503 executeHooks(hooks.processOffset, [plotOffset]);
1505 // If the grid is visible, add its border width to the offset
1507 for (var a in plotOffset) {
1508 if(typeof(options.grid.borderWidth) == "object") {
1509 plotOffset[a] += showGrid ? options.grid.borderWidth[a] : 0;
1512 plotOffset[a] += showGrid ? options.grid.borderWidth : 0;
1517 $.each(axes, function (_, axis) {
1518 axis.show = axis.options.show;
1519 if (axis.show == null)
1520 axis.show = axis.used; // by default an axis is visible if it's got data
1522 axis.reserveSpace = axis.show || axis.options.reserveSpace;
1529 var allocatedAxes = $.grep(axes, function (axis) { return axis.reserveSpace; });
1531 $.each(allocatedAxes, function (_, axis) {
1533 setupTickGeneration(axis);
1535 snapRangeToTicks(axis, axis.ticks);
1536 // find labelWidth/Height for axis
1537 measureTickLabels(axis);
1540 // with all dimensions calculated, we can compute the
1541 // axis bounding boxes, start from the outside
1543 for (i = allocatedAxes.length - 1; i >= 0; --i)
1544 allocateAxisBoxFirstPhase(allocatedAxes[i]);
1546 // make sure we've got enough space for things that
1548 adjustLayoutForThingsStickingOut();
1550 $.each(allocatedAxes, function (_, axis) {
1551 allocateAxisBoxSecondPhase(axis);
1555 plotWidth = surface.width - plotOffset.left - plotOffset.right;
1556 plotHeight = surface.height - plotOffset.bottom - plotOffset.top;
1558 // now we got the proper plot dimensions, we can compute the scaling
1559 $.each(axes, function (_, axis) {
1560 setTransformationHelpers(axis);
1570 function setRange(axis) {
1571 var opts = axis.options,
1572 min = +(opts.min != null ? opts.min : axis.datamin),
1573 max = +(opts.max != null ? opts.max : axis.datamax),
1578 var widen = max == 0 ? 1 : 0.01;
1580 if (opts.min == null)
1582 // always widen max if we couldn't widen min to ensure we
1583 // don't fall into min == max which doesn't work
1584 if (opts.max == null || opts.min != null)
1588 // consider autoscaling
1589 var margin = opts.autoscaleMargin;
1590 if (margin != null) {
1591 if (opts.min == null) {
1592 min -= delta * margin;
1593 // make sure we don't go below zero if all values
1595 if (min < 0 && axis.datamin != null && axis.datamin >= 0)
1598 if (opts.max == null) {
1599 max += delta * margin;
1600 if (max > 0 && axis.datamax != null && axis.datamax <= 0)
1609 function setupTickGeneration(axis) {
1610 var opts = axis.options;
1612 // estimate number of ticks
1614 if (typeof opts.ticks == "number" && opts.ticks > 0)
1615 noTicks = opts.ticks;
1617 // heuristic based on the model a*sqrt(x) fitted to
1618 // some data points that seemed reasonable
1619 noTicks = 0.3 * Math.sqrt(axis.direction == "x" ? surface.width : surface.height);
1621 var delta = (axis.max - axis.min) / noTicks,
1622 dec = -Math.floor(Math.log(delta) / Math.LN10),
1623 maxDec = opts.tickDecimals;
1625 if (maxDec != null && dec > maxDec) {
1629 var magn = Math.pow(10, -dec),
1630 norm = delta / magn, // norm is between 1.0 and 10.0
1635 } else if (norm < 3) {
1637 // special case for 2.5, requires an extra decimal
1638 if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
1642 } else if (norm < 7.5) {
1650 if (opts.minTickSize != null && size < opts.minTickSize) {
1651 size = opts.minTickSize;
1655 axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec);
1656 axis.tickSize = opts.tickSize || size;
1658 // Time mode was moved to a plug-in in 0.8, but since so many people use this
1659 // we'll add an especially friendly make sure they remembered to include it.
1661 if (opts.mode == "time" && !axis.tickGenerator) {
1662 throw new Error("Time mode requires the flot.time plugin.");
1665 // Flot supports base-10 axes; any other mode else is handled by a plug-in,
1666 // like flot.time.js.
1668 if (!axis.tickGenerator) {
1670 axis.tickGenerator = function (axis) {
1673 start = floorInBase(axis.min, axis.tickSize),
1680 v = start + i * axis.tickSize;
1683 } while (v < axis.max && v != prev);
1687 axis.tickFormatter = function (value, axis) {
1689 var factor = axis.tickDecimals ? Math.pow(10, axis.tickDecimals) : 1;
1690 var formatted = "" + Math.round(value * factor) / factor;
1692 // If tickDecimals was specified, ensure that we have exactly that
1693 // much precision; otherwise default to the value's own precision.
1695 if (axis.tickDecimals != null) {
1696 var decimal = formatted.indexOf(".");
1697 var precision = decimal == -1 ? 0 : formatted.length - decimal - 1;
1698 if (precision < axis.tickDecimals) {
1699 return (precision ? formatted : formatted + ".") + ("" + factor).substr(1, axis.tickDecimals - precision);
1707 if ($.isFunction(opts.tickFormatter))
1708 axis.tickFormatter = function (v, axis) { return "" + opts.tickFormatter(v, axis); };
1710 if (opts.alignTicksWithAxis != null) {
1711 var otherAxis = (axis.direction == "x" ? xaxes : yaxes)[opts.alignTicksWithAxis - 1];
1712 if (otherAxis && otherAxis.used && otherAxis != axis) {
1713 // consider snapping min/max to outermost nice ticks
1714 var niceTicks = axis.tickGenerator(axis);
1715 if (niceTicks.length > 0) {
1716 if (opts.min == null)
1717 axis.min = Math.min(axis.min, niceTicks[0]);
1718 if (opts.max == null && niceTicks.length > 1)
1719 axis.max = Math.max(axis.max, niceTicks[niceTicks.length - 1]);
1722 axis.tickGenerator = function (axis) {
1723 // copy ticks, scaled to this axis
1724 var ticks = [], v, i;
1725 for (i = 0; i < otherAxis.ticks.length; ++i) {
1726 v = (otherAxis.ticks[i].v - otherAxis.min) / (otherAxis.max - otherAxis.min);
1727 v = axis.min + v * (axis.max - axis.min);
1733 // we might need an extra decimal since forced
1734 // ticks don't necessarily fit naturally
1735 if (!axis.mode && opts.tickDecimals == null) {
1736 var extraDec = Math.max(0, -Math.floor(Math.log(axis.delta) / Math.LN10) + 1),
1737 ts = axis.tickGenerator(axis);
1739 // only proceed if the tick interval rounded
1740 // with an extra decimal doesn't give us a
1742 if (!(ts.length > 1 && /\..*0$/.test((ts[1] - ts[0]).toFixed(extraDec))))
1743 axis.tickDecimals = extraDec;
1749 function setTicks(axis) {
1750 var oticks = axis.options.ticks, ticks = [];
1751 if (oticks == null || (typeof oticks == "number" && oticks > 0))
1752 ticks = axis.tickGenerator(axis);
1754 if ($.isFunction(oticks))
1755 // generate the ticks
1756 ticks = oticks(axis);
1761 // clean up/labelify the supplied ticks, copy them over
1764 for (i = 0; i < ticks.length; ++i) {
1767 if (typeof t == "object") {
1775 label = axis.tickFormatter(v, axis);
1777 axis.ticks.push({ v: v, label: label });
1781 function snapRangeToTicks(axis, ticks) {
1782 if (axis.options.autoscaleMargin && ticks.length > 0) {
1784 if (axis.options.min == null)
1785 axis.min = Math.min(axis.min, ticks[0].v);
1786 if (axis.options.max == null && ticks.length > 1)
1787 axis.max = Math.max(axis.max, ticks[ticks.length - 1].v);
1795 executeHooks(hooks.drawBackground, [ctx]);
1797 var grid = options.grid;
1799 // draw background, if any
1800 if (grid.show && grid.backgroundColor)
1803 if (grid.show && !grid.aboveData) {
1807 for (var i = 0; i < series.length; ++i) {
1808 executeHooks(hooks.drawSeries, [ctx, series[i]]);
1809 drawSeries(series[i]);
1812 executeHooks(hooks.draw, [ctx]);
1814 if (grid.show && grid.aboveData) {
1820 // A draw implies that either the axes or data have changed, so we
1821 // should probably update the overlay highlights as well.
1823 triggerRedrawOverlay();
1826 function extractRange(ranges, coord) {
1827 var axis, from, to, key, axes = allAxes();
1829 for (var i = 0; i < axes.length; ++i) {
1831 if (axis.direction == coord) {
1832 key = coord + axis.n + "axis";
1833 if (!ranges[key] && axis.n == 1)
1834 key = coord + "axis"; // support x1axis as xaxis
1836 from = ranges[key].from;
1837 to = ranges[key].to;
1843 // backwards-compat stuff - to be removed in future
1845 axis = coord == "x" ? xaxes[0] : yaxes[0];
1846 from = ranges[coord + "1"];
1847 to = ranges[coord + "2"];
1850 // auto-reverse as an added bonus
1851 if (from != null && to != null && from > to) {
1857 return { from: from, to: to, axis: axis };
1860 function drawBackground() {
1862 ctx.translate(plotOffset.left, plotOffset.top);
1864 ctx.fillStyle = getColorOrGradient(options.grid.backgroundColor, plotHeight, 0, "rgba(255, 255, 255, 0)");
1865 ctx.fillRect(0, 0, plotWidth, plotHeight);
1869 function drawGrid() {
1870 var i, axes, bw, bc;
1873 ctx.translate(plotOffset.left, plotOffset.top);
1876 var markings = options.grid.markings;
1878 if ($.isFunction(markings)) {
1879 axes = plot.getAxes();
1880 // xmin etc. is backwards compatibility, to be
1881 // removed in the future
1882 axes.xmin = axes.xaxis.min;
1883 axes.xmax = axes.xaxis.max;
1884 axes.ymin = axes.yaxis.min;
1885 axes.ymax = axes.yaxis.max;
1887 markings = markings(axes);
1890 for (i = 0; i < markings.length; ++i) {
1891 var m = markings[i],
1892 xrange = extractRange(m, "x"),
1893 yrange = extractRange(m, "y");
1896 if (xrange.from == null)
1897 xrange.from = xrange.axis.min;
1898 if (xrange.to == null)
1899 xrange.to = xrange.axis.max;
1900 if (yrange.from == null)
1901 yrange.from = yrange.axis.min;
1902 if (yrange.to == null)
1903 yrange.to = yrange.axis.max;
1906 if (xrange.to < xrange.axis.min || xrange.from > xrange.axis.max ||
1907 yrange.to < yrange.axis.min || yrange.from > yrange.axis.max)
1910 xrange.from = Math.max(xrange.from, xrange.axis.min);
1911 xrange.to = Math.min(xrange.to, xrange.axis.max);
1912 yrange.from = Math.max(yrange.from, yrange.axis.min);
1913 yrange.to = Math.min(yrange.to, yrange.axis.max);
1915 if (xrange.from == xrange.to && yrange.from == yrange.to)
1919 xrange.from = xrange.axis.p2c(xrange.from);
1920 xrange.to = xrange.axis.p2c(xrange.to);
1921 yrange.from = yrange.axis.p2c(yrange.from);
1922 yrange.to = yrange.axis.p2c(yrange.to);
1924 if (xrange.from == xrange.to || yrange.from == yrange.to) {
1927 ctx.strokeStyle = m.color || options.grid.markingsColor;
1928 ctx.lineWidth = m.lineWidth || options.grid.markingsLineWidth;
1929 ctx.moveTo(xrange.from, yrange.from);
1930 ctx.lineTo(xrange.to, yrange.to);
1935 ctx.fillStyle = m.color || options.grid.markingsColor;
1936 ctx.fillRect(xrange.from, yrange.to,
1937 xrange.to - xrange.from,
1938 yrange.from - yrange.to);
1945 bw = options.grid.borderWidth;
1947 for (var j = 0; j < axes.length; ++j) {
1948 var axis = axes[j], box = axis.box,
1949 t = axis.tickLength, x, y, xoff, yoff;
1950 if (!axis.show || axis.ticks.length == 0)
1956 if (axis.direction == "x") {
1959 y = (axis.position == "top" ? 0 : plotHeight);
1961 y = box.top - plotOffset.top + (axis.position == "top" ? box.height : 0);
1966 x = (axis.position == "left" ? 0 : plotWidth);
1968 x = box.left - plotOffset.left + (axis.position == "left" ? box.width : 0);
1972 if (!axis.innermost) {
1973 ctx.strokeStyle = axis.options.color;
1976 if (axis.direction == "x")
1977 xoff = plotWidth + 1;
1979 yoff = plotHeight + 1;
1981 if (ctx.lineWidth == 1) {
1982 if (axis.direction == "x") {
1983 y = Math.floor(y) + 0.5;
1985 x = Math.floor(x) + 0.5;
1990 ctx.lineTo(x + xoff, y + yoff);
1996 ctx.strokeStyle = axis.options.tickColor;
1999 for (i = 0; i < axis.ticks.length; ++i) {
2000 var v = axis.ticks[i].v;
2004 if (isNaN(v) || v < axis.min || v > axis.max
2005 // skip those lying on the axes if we got a border
2007 && ((typeof bw == "object" && bw[axis.position] > 0) || bw > 0)
2008 && (v == axis.min || v == axis.max)))
2011 if (axis.direction == "x") {
2013 yoff = t == "full" ? -plotHeight : t;
2015 if (axis.position == "top")
2020 xoff = t == "full" ? -plotWidth : t;
2022 if (axis.position == "left")
2026 if (ctx.lineWidth == 1) {
2027 if (axis.direction == "x")
2028 x = Math.floor(x) + 0.5;
2030 y = Math.floor(y) + 0.5;
2034 ctx.lineTo(x + xoff, y + yoff);
2043 // If either borderWidth or borderColor is an object, then draw the border
2044 // line by line instead of as one rectangle
2045 bc = options.grid.borderColor;
2046 if(typeof bw == "object" || typeof bc == "object") {
2047 if (typeof bw !== "object") {
2048 bw = {top: bw, right: bw, bottom: bw, left: bw};
2050 if (typeof bc !== "object") {
2051 bc = {top: bc, right: bc, bottom: bc, left: bc};
2055 ctx.strokeStyle = bc.top;
2056 ctx.lineWidth = bw.top;
2058 ctx.moveTo(0 - bw.left, 0 - bw.top/2);
2059 ctx.lineTo(plotWidth, 0 - bw.top/2);
2064 ctx.strokeStyle = bc.right;
2065 ctx.lineWidth = bw.right;
2067 ctx.moveTo(plotWidth + bw.right / 2, 0 - bw.top);
2068 ctx.lineTo(plotWidth + bw.right / 2, plotHeight);
2072 if (bw.bottom > 0) {
2073 ctx.strokeStyle = bc.bottom;
2074 ctx.lineWidth = bw.bottom;
2076 ctx.moveTo(plotWidth + bw.right, plotHeight + bw.bottom / 2);
2077 ctx.lineTo(0, plotHeight + bw.bottom / 2);
2082 ctx.strokeStyle = bc.left;
2083 ctx.lineWidth = bw.left;
2085 ctx.moveTo(0 - bw.left/2, plotHeight + bw.bottom);
2086 ctx.lineTo(0- bw.left/2, 0);
2092 ctx.strokeStyle = options.grid.borderColor;
2093 ctx.strokeRect(-bw/2, -bw/2, plotWidth + bw, plotHeight + bw);
2100 function drawAxisLabels() {
2102 $.each(allAxes(), function (_, axis) {
2103 if (!axis.show || axis.ticks.length == 0)
2107 legacyStyles = axis.direction + "Axis " + axis.direction + axis.n + "Axis",
2108 layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + legacyStyles,
2109 font = axis.options.font || "flot-tick-label tickLabel",
2110 tick, x, y, halign, valign;
2112 surface.removeText(layer);
2114 for (var i = 0; i < axis.ticks.length; ++i) {
2116 tick = axis.ticks[i];
2117 if (!tick.label || tick.v < axis.min || tick.v > axis.max)
2120 if (axis.direction == "x") {
2122 x = plotOffset.left + axis.p2c(tick.v);
2123 if (axis.position == "bottom") {
2124 y = box.top + box.padding;
2126 y = box.top + box.height - box.padding;
2131 y = plotOffset.top + axis.p2c(tick.v);
2132 if (axis.position == "left") {
2133 x = box.left + box.width - box.padding;
2136 x = box.left + box.padding;
2140 surface.addText(layer, x, y, tick.label, font, null, null, halign, valign);
2145 function drawSeries(series) {
2146 if (series.lines.show)
2147 drawSeriesLines(series);
2148 if (series.bars.show)
2149 drawSeriesBars(series);
2150 if (series.points.show)
2151 drawSeriesPoints(series);
2154 function drawSeriesLines(series) {
2155 function plotLine(datapoints, xoffset, yoffset, axisx, axisy) {
2156 var points = datapoints.points,
2157 ps = datapoints.pointsize,
2158 prevx = null, prevy = null;
2161 for (var i = ps; i < points.length; i += ps) {
2162 var x1 = points[i - ps], y1 = points[i - ps + 1],
2163 x2 = points[i], y2 = points[i + 1];
2165 if (x1 == null || x2 == null)
2169 if (y1 <= y2 && y1 < axisy.min) {
2171 continue; // line segment is outside
2172 // compute new intersection point
2173 x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
2176 else if (y2 <= y1 && y2 < axisy.min) {
2179 x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
2184 if (y1 >= y2 && y1 > axisy.max) {
2187 x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
2190 else if (y2 >= y1 && y2 > axisy.max) {
2193 x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
2198 if (x1 <= x2 && x1 < axisx.min) {
2201 y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
2204 else if (x2 <= x1 && x2 < axisx.min) {
2207 y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
2212 if (x1 >= x2 && x1 > axisx.max) {
2215 y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
2218 else if (x2 >= x1 && x2 > axisx.max) {
2221 y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
2225 if (x1 != prevx || y1 != prevy)
2226 ctx.moveTo(axisx.p2c(x1) + xoffset, axisy.p2c(y1) + yoffset);
2230 ctx.lineTo(axisx.p2c(x2) + xoffset, axisy.p2c(y2) + yoffset);
2235 function plotLineArea(datapoints, axisx, axisy) {
2236 var points = datapoints.points,
2237 ps = datapoints.pointsize,
2238 bottom = Math.min(Math.max(0, axisy.min), axisy.max),
2239 i = 0, top, areaOpen = false,
2240 ypos = 1, segmentStart = 0, segmentEnd = 0;
2242 // we process each segment in two turns, first forward
2243 // direction to sketch out top, then once we hit the
2244 // end we go backwards to sketch the bottom
2246 if (ps > 0 && i > points.length + ps)
2249 i += ps; // ps is negative if going backwards
2251 var x1 = points[i - ps],
2252 y1 = points[i - ps + ypos],
2253 x2 = points[i], y2 = points[i + ypos];
2256 if (ps > 0 && x1 != null && x2 == null) {
2264 if (ps < 0 && i == segmentStart + ps) {
2265 // done with the reverse sweep
2270 i = segmentStart = segmentEnd + ps;
2275 if (x1 == null || x2 == null)
2281 if (x1 <= x2 && x1 < axisx.min) {
2284 y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
2287 else if (x2 <= x1 && x2 < axisx.min) {
2290 y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
2295 if (x1 >= x2 && x1 > axisx.max) {
2298 y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
2301 else if (x2 >= x1 && x2 > axisx.max) {
2304 y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
2311 ctx.moveTo(axisx.p2c(x1), axisy.p2c(bottom));
2315 // now first check the case where both is outside
2316 if (y1 >= axisy.max && y2 >= axisy.max) {
2317 ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.max));
2318 ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.max));
2321 else if (y1 <= axisy.min && y2 <= axisy.min) {
2322 ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.min));
2323 ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.min));
2327 // else it's a bit more complicated, there might
2328 // be a flat maxed out rectangle first, then a
2329 // triangular cutout or reverse; to find these
2330 // keep track of the current x values
2331 var x1old = x1, x2old = x2;
2333 // clip the y values, without shortcutting, we
2334 // go through all cases in turn
2337 if (y1 <= y2 && y1 < axisy.min && y2 >= axisy.min) {
2338 x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
2341 else if (y2 <= y1 && y2 < axisy.min && y1 >= axisy.min) {
2342 x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
2347 if (y1 >= y2 && y1 > axisy.max && y2 <= axisy.max) {
2348 x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
2351 else if (y2 >= y1 && y2 > axisy.max && y1 <= axisy.max) {
2352 x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
2356 // if the x value was changed we got a rectangle
2359 ctx.lineTo(axisx.p2c(x1old), axisy.p2c(y1));
2360 // it goes to (x1, y1), but we fill that below
2363 // fill triangular section, this sometimes result
2364 // in redundant points if (x1, y1) hasn't changed
2365 // from previous line to, but we just ignore that
2366 ctx.lineTo(axisx.p2c(x1), axisy.p2c(y1));
2367 ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
2369 // fill the other rectangle if it's there
2371 ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
2372 ctx.lineTo(axisx.p2c(x2old), axisy.p2c(y2));
2378 ctx.translate(plotOffset.left, plotOffset.top);
2379 ctx.lineJoin = "round";
2381 var lw = series.lines.lineWidth,
2382 sw = series.shadowSize;
2383 // FIXME: consider another form of shadow when filling is turned on
2384 if (lw > 0 && sw > 0) {
2385 // draw shadow as a thick and thin line with transparency
2387 ctx.strokeStyle = "rgba(0,0,0,0.1)";
2388 // position shadow at angle from the mid of line
2389 var angle = Math.PI/18;
2390 plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/2), Math.cos(angle) * (lw/2 + sw/2), series.xaxis, series.yaxis);
2391 ctx.lineWidth = sw/2;
2392 plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/4), Math.cos(angle) * (lw/2 + sw/4), series.xaxis, series.yaxis);
2396 ctx.strokeStyle = series.color;
2397 var fillStyle = getFillStyle(series.lines, series.color, 0, plotHeight);
2399 ctx.fillStyle = fillStyle;
2400 plotLineArea(series.datapoints, series.xaxis, series.yaxis);
2404 plotLine(series.datapoints, 0, 0, series.xaxis, series.yaxis);
2408 function drawSeriesPoints(series) {
2409 function plotPoints(datapoints, radius, fillStyle, offset, shadow, axisx, axisy, symbol) {
2410 var points = datapoints.points, ps = datapoints.pointsize;
2412 for (var i = 0; i < points.length; i += ps) {
2413 var x = points[i], y = points[i + 1];
2414 if (x == null || x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
2419 y = axisy.p2c(y) + offset;
2420 if (symbol == "circle")
2421 ctx.arc(x, y, radius, 0, shadow ? Math.PI : Math.PI * 2, false);
2423 symbol(ctx, x, y, radius, shadow);
2427 ctx.fillStyle = fillStyle;
2435 ctx.translate(plotOffset.left, plotOffset.top);
2437 var lw = series.points.lineWidth,
2438 sw = series.shadowSize,
2439 radius = series.points.radius,
2440 symbol = series.points.symbol;
2442 // If the user sets the line width to 0, we change it to a very
2443 // small value. A line width of 0 seems to force the default of 1.
2444 // Doing the conditional here allows the shadow setting to still be
2445 // optional even with a lineWidth of 0.
2450 if (lw > 0 && sw > 0) {
2451 // draw shadow in two steps
2454 ctx.strokeStyle = "rgba(0,0,0,0.1)";
2455 plotPoints(series.datapoints, radius, null, w + w/2, true,
2456 series.xaxis, series.yaxis, symbol);
2458 ctx.strokeStyle = "rgba(0,0,0,0.2)";
2459 plotPoints(series.datapoints, radius, null, w/2, true,
2460 series.xaxis, series.yaxis, symbol);
2464 ctx.strokeStyle = series.color;
2465 plotPoints(series.datapoints, radius,
2466 getFillStyle(series.points, series.color), 0, false,
2467 series.xaxis, series.yaxis, symbol);
2471 function drawBar(x, y, b, barLeft, barRight, offset, fillStyleCallback, axisx, axisy, c, horizontal, lineWidth) {
2472 var left, right, bottom, top,
2473 drawLeft, drawRight, drawTop, drawBottom,
2476 // in horizontal mode, we start the bar from the left
2477 // instead of from the bottom so it appears to be
2478 // horizontal rather than vertical
2480 drawBottom = drawRight = drawTop = true;
2485 bottom = y + barRight;
2487 // account for negative bars
2497 drawLeft = drawRight = drawTop = true;
2500 right = x + barRight;
2504 // account for negative bars
2515 if (right < axisx.min || left > axisx.max ||
2516 top < axisy.min || bottom > axisy.max)
2519 if (left < axisx.min) {
2524 if (right > axisx.max) {
2529 if (bottom < axisy.min) {
2534 if (top > axisy.max) {
2539 left = axisx.p2c(left);
2540 bottom = axisy.p2c(bottom);
2541 right = axisx.p2c(right);
2542 top = axisy.p2c(top);
2545 if (fillStyleCallback) {
2547 c.moveTo(left, bottom);
2548 c.lineTo(left, top);
2549 c.lineTo(right, top);
2550 c.lineTo(right, bottom);
2551 c.fillStyle = fillStyleCallback(bottom, top);
2556 if (lineWidth > 0 && (drawLeft || drawRight || drawTop || drawBottom)) {
2559 // FIXME: inline moveTo is buggy with excanvas
2560 c.moveTo(left, bottom + offset);
2562 c.lineTo(left, top + offset);
2564 c.moveTo(left, top + offset);
2566 c.lineTo(right, top + offset);
2568 c.moveTo(right, top + offset);
2570 c.lineTo(right, bottom + offset);
2572 c.moveTo(right, bottom + offset);
2574 c.lineTo(left, bottom + offset);
2576 c.moveTo(left, bottom + offset);
2581 function drawSeriesBars(series) {
2582 function plotBars(datapoints, barLeft, barRight, offset, fillStyleCallback, axisx, axisy) {
2583 var points = datapoints.points, ps = datapoints.pointsize;
2585 for (var i = 0; i < points.length; i += ps) {
2586 if (points[i] == null)
2588 drawBar(points[i], points[i + 1], points[i + 2], barLeft, barRight, offset, fillStyleCallback, axisx, axisy, ctx, series.bars.horizontal, series.bars.lineWidth);
2593 ctx.translate(plotOffset.left, plotOffset.top);
2595 // FIXME: figure out a way to add shadows (for instance along the right edge)
2596 ctx.lineWidth = series.bars.lineWidth;
2597 ctx.strokeStyle = series.color;
2601 switch (series.bars.align) {
2606 barLeft = -series.bars.barWidth;
2609 barLeft = -series.bars.barWidth / 2;
2612 throw new Error("Invalid bar alignment: " + series.bars.align);
2615 var fillStyleCallback = series.bars.fill ? function (bottom, top) { return getFillStyle(series.bars, series.color, bottom, top); } : null;
2616 plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, 0, fillStyleCallback, series.xaxis, series.yaxis);
2620 function getFillStyle(filloptions, seriesColor, bottom, top) {
2621 var fill = filloptions.fill;
2625 if (filloptions.fillColor)
2626 return getColorOrGradient(filloptions.fillColor, bottom, top, seriesColor);
2628 var c = $.color.parse(seriesColor);
2629 c.a = typeof fill == "number" ? fill : 0.4;
2631 return c.toString();
2634 function insertLegend() {
2636 placeholder.find(".legend").remove();
2638 if (!options.legend.show)
2641 var fragments = [], entries = [], rowStarted = false,
2642 lf = options.legend.labelFormatter, s, label;
2644 // Build a list of legend entries, with each having a label and a color
2646 for (var i = 0; i < series.length; ++i) {
2649 label = lf ? lf(s.label, s) : s.label;
2659 // Sort the legend using either the default or a custom comparator
2661 if (options.legend.sorted) {
2662 if ($.isFunction(options.legend.sorted)) {
2663 entries.sort(options.legend.sorted);
2664 } else if (options.legend.sorted == "reverse") {
2667 var ascending = options.legend.sorted != "descending";
2668 entries.sort(function(a, b) {
2669 return a.label == b.label ? 0 : (
2670 (a.label < b.label) != ascending ? 1 : -1 // Logical XOR
2676 // Generate markup for the list of entries, in their final order
2678 for (var i = 0; i < entries.length; ++i) {
2680 var entry = entries[i];
2682 if (i % options.legend.noColumns == 0) {
2684 fragments.push('</tr>');
2685 fragments.push('<tr>');
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>' +
2691 '<td class="legendLabel">' + entry.label + '</td>'
2696 fragments.push('</tr>');
2698 if (fragments.length == 0)
2701 var table = '<table style="font-size:smaller;color:' + options.grid.color + '">' + fragments.join("") + '</table>';
2702 if (options.legend.container != null)
2703 $(options.legend.container).html(table);
2706 p = options.legend.position,
2707 m = options.legend.margin;
2710 if (p.charAt(0) == "n")
2711 pos += 'top:' + (m[1] + plotOffset.top) + 'px;';
2712 else if (p.charAt(0) == "s")
2713 pos += 'bottom:' + (m[1] + plotOffset.bottom) + 'px;';
2714 if (p.charAt(1) == "e")
2715 pos += 'right:' + (m[0] + plotOffset.right) + 'px;';
2716 else if (p.charAt(1) == "w")
2717 pos += 'left:' + (m[0] + plotOffset.left) + 'px;';
2718 var legend = $('<div class="legend">' + table.replace('style="', 'style="position:absolute;' + pos +';') + '</div>').appendTo(placeholder);
2719 if (options.legend.backgroundOpacity != 0.0) {
2720 // put in the transparent background
2721 // separately to avoid blended labels and
2723 var c = options.legend.backgroundColor;
2725 c = options.grid.backgroundColor;
2726 if (c && typeof c == "string")
2727 c = $.color.parse(c);
2729 c = $.color.extract(legend, 'background-color');
2733 var div = legend.children();
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);
2740 // interactive features
2742 var highlights = [],
2743 redrawTimeout = null;
2745 // returns the data item the mouse is over, or null if none is found
2746 function findNearbyItem(mouseX, mouseY, seriesFilter) {
2747 var maxDistance = options.grid.mouseActiveRadius,
2748 smallestDistance = maxDistance * maxDistance + 1,
2749 item = null, foundPoint = false, i, j, ps;
2751 for (i = series.length - 1; i >= 0; --i) {
2752 if (!seriesFilter(series[i]))
2758 points = s.datapoints.points,
2759 mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster
2760 my = axisy.c2p(mouseY),
2761 maxx = maxDistance / axisx.scale,
2762 maxy = maxDistance / axisy.scale;
2764 ps = s.datapoints.pointsize;
2765 // with inverse transforms, we can't use the maxx/maxy
2766 // optimization, sadly
2767 if (axisx.options.inverseTransform)
2768 maxx = Number.MAX_VALUE;
2769 if (axisy.options.inverseTransform)
2770 maxy = Number.MAX_VALUE;
2772 if (s.lines.show || s.points.show) {
2773 for (j = 0; j < points.length; j += ps) {
2774 var x = points[j], y = points[j + 1];
2778 // For points and lines, the cursor must be within a
2779 // certain distance to the data point
2780 if (x - mx > maxx || x - mx < -maxx ||
2781 y - my > maxy || y - my < -maxy)
2784 // We have to calculate distances in pixels, not in
2785 // data units, because the scales of the axes may be different
2786 var dx = Math.abs(axisx.p2c(x) - mouseX),
2787 dy = Math.abs(axisy.p2c(y) - mouseY),
2788 dist = dx * dx + dy * dy; // we save the sqrt
2790 // use <= to ensure last point takes precedence
2791 // (last generally means on top of)
2792 if (dist < smallestDistance) {
2793 smallestDistance = dist;
2799 if (s.bars.show && !item) { // no other point can be nearby
2800 var barLeft = s.bars.align == "left" ? 0 : -s.bars.barWidth/2,
2801 barRight = barLeft + s.bars.barWidth;
2803 for (j = 0; j < points.length; j += ps) {
2804 var x = points[j], y = points[j + 1], b = points[j + 2];
2808 // for a bar graph, the cursor must be inside the bar
2809 if (series[i].bars.horizontal ?
2810 (mx <= Math.max(b, x) && mx >= Math.min(b, x) &&
2811 my >= y + barLeft && my <= y + barRight) :
2812 (mx >= x + barLeft && mx <= x + barRight &&
2813 my >= Math.min(b, y) && my <= Math.max(b, y)))
2822 ps = series[i].datapoints.pointsize;
2824 return { datapoint: series[i].datapoints.points.slice(j * ps, (j + 1) * ps),
2833 function onMouseMove(e) {
2834 if (options.grid.hoverable)
2835 triggerClickHoverEvent("plothover", e,
2836 function (s) { return s["hoverable"] != false; });
2839 function onMouseLeave(e) {
2840 if (options.grid.hoverable)
2841 triggerClickHoverEvent("plothover", e,
2842 function (s) { return false; });
2845 function onClick(e) {
2846 triggerClickHoverEvent("plotclick", e,
2847 function (s) { return s["clickable"] != false; });
2850 // trigger click or hover event (they send the same parameters
2851 // so we share their code)
2852 function triggerClickHoverEvent(eventname, event, seriesFilter) {
2853 var offset = eventHolder.offset(),
2854 canvasX = event.pageX - offset.left - plotOffset.left,
2855 canvasY = event.pageY - offset.top - plotOffset.top,
2856 pos = canvasToAxisCoords({ left: canvasX, top: canvasY });
2858 pos.pageX = event.pageX;
2859 pos.pageY = event.pageY;
2861 var item = findNearbyItem(canvasX, canvasY, seriesFilter);
2864 // fill in mouse pos for any listeners out there
2865 item.pageX = parseInt(item.series.xaxis.p2c(item.datapoint[0]) + offset.left + plotOffset.left, 10);
2866 item.pageY = parseInt(item.series.yaxis.p2c(item.datapoint[1]) + offset.top + plotOffset.top, 10);
2869 if (options.grid.autoHighlight) {
2870 // clear auto-highlights
2871 for (var i = 0; i < highlights.length; ++i) {
2872 var h = highlights[i];
2873 if (h.auto == eventname &&
2874 !(item && h.series == item.series &&
2875 h.point[0] == item.datapoint[0] &&
2876 h.point[1] == item.datapoint[1]))
2877 unhighlight(h.series, h.point);
2881 highlight(item.series, item.datapoint, eventname);
2884 placeholder.trigger(eventname, [ pos, item ]);
2887 function triggerRedrawOverlay() {
2888 var t = options.interaction.redrawOverlayInterval;
2889 if (t == -1) { // skip event queue
2895 redrawTimeout = setTimeout(drawOverlay, t);
2898 function drawOverlay() {
2899 redrawTimeout = null;
2904 octx.translate(plotOffset.left, plotOffset.top);
2907 for (i = 0; i < highlights.length; ++i) {
2910 if (hi.series.bars.show)
2911 drawBarHighlight(hi.series, hi.point);
2913 drawPointHighlight(hi.series, hi.point);
2917 executeHooks(hooks.drawOverlay, [octx]);
2920 function highlight(s, point, auto) {
2921 if (typeof s == "number")
2924 if (typeof point == "number") {
2925 var ps = s.datapoints.pointsize;
2926 point = s.datapoints.points.slice(ps * point, ps * (point + 1));
2929 var i = indexOfHighlight(s, point);
2931 highlights.push({ series: s, point: point, auto: auto });
2933 triggerRedrawOverlay();
2936 highlights[i].auto = false;
2939 function unhighlight(s, point) {
2940 if (s == null && point == null) {
2942 triggerRedrawOverlay();
2946 if (typeof s == "number")
2949 if (typeof point == "number") {
2950 var ps = s.datapoints.pointsize;
2951 point = s.datapoints.points.slice(ps * point, ps * (point + 1));
2954 var i = indexOfHighlight(s, point);
2956 highlights.splice(i, 1);
2958 triggerRedrawOverlay();
2962 function indexOfHighlight(s, p) {
2963 for (var i = 0; i < highlights.length; ++i) {
2964 var h = highlights[i];
2965 if (h.series == s && h.point[0] == p[0]
2966 && h.point[1] == p[1])
2972 function drawPointHighlight(series, point) {
2973 var x = point[0], y = point[1],
2974 axisx = series.xaxis, axisy = series.yaxis,
2975 highlightColor = (typeof series.highlightColor === "string") ? series.highlightColor : $.color.parse(series.color).scale('a', 0.5).toString();
2977 if (x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
2980 var pointRadius = series.points.radius + series.points.lineWidth / 2;
2981 octx.lineWidth = pointRadius;
2982 octx.strokeStyle = highlightColor;
2983 var radius = 1.5 * pointRadius;
2988 if (series.points.symbol == "circle")
2989 octx.arc(x, y, radius, 0, 2 * Math.PI, false);
2991 series.points.symbol(octx, x, y, radius, false);
2996 function drawBarHighlight(series, point) {
2997 var highlightColor = (typeof series.highlightColor === "string") ? series.highlightColor : $.color.parse(series.color).scale('a', 0.5).toString(),
2998 fillStyle = highlightColor,
2999 barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
3001 octx.lineWidth = series.bars.lineWidth;
3002 octx.strokeStyle = highlightColor;
3004 drawBar(point[0], point[1], point[2] || 0, barLeft, barLeft + series.bars.barWidth,
3005 0, function () { return fillStyle; }, series.xaxis, series.yaxis, octx, series.bars.horizontal, series.bars.lineWidth);
3008 function getColorOrGradient(spec, bottom, top, defaultColor) {
3009 if (typeof spec == "string")
3012 // assume this is a gradient spec; IE currently only
3013 // supports a simple vertical gradient properly, so that's
3014 // what we support too
3015 var gradient = ctx.createLinearGradient(0, top, 0, bottom);
3017 for (var i = 0, l = spec.colors.length; i < l; ++i) {
3018 var c = spec.colors[i];
3019 if (typeof c != "string") {
3020 var co = $.color.parse(defaultColor);
3021 if (c.brightness != null)
3022 co = co.scale('rgb', c.brightness);
3023 if (c.opacity != null)
3027 gradient.addColorStop(i / (l - 1), c);
3035 // Add the plot function to the top level of the jQuery object
3037 $.plot = function(placeholder, data, options) {
3038 //var t0 = new Date();
3039 var plot = new Plot($(placeholder), data, options, $.plot.plugins);
3040 //(window.console ? console.log : alert)("time used (msecs): " + ((new Date()).getTime() - t0.getTime()));
3044 $.plot.version = "0.8.1";
3046 $.plot.plugins = [];
3048 // Also add the plot function as a chainable property
3050 $.fn.plot = function(data, options) {
3051 return this.each(function() {
3052 $.plot(this, data, options);
3056 // round to nearby lower multiple of base
3057 function floorInBase(n, base) {
3058 return base * Math.floor(n / base);