1 /* Javascript plotting library for jQuery, v. 0.6.
3 * Released under the MIT license by IOLA, December 2007.
7 // first an inline dependency, jquery.colorhelpers.js, we inline it here
10 /* Plugin for jQuery for working with colors.
14 * Inspiration from jQuery color animation plugin by John Resig.
16 * Released under the MIT license by Ole Laursen, October 2009.
20 * $.color.parse("#fff").scale('rgb', 0.25).add('a', -0.5).toString()
21 * var c = $.color.extract($("#mydiv"), 'background-color');
22 * console.log(c.r, c.g, c.b, c.a);
23 * $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)"
25 * Note that .scale() and .add() work in-place instead of returning
28 (function(){jQuery.color={};jQuery.color.make=function(E,D,B,C){var F={};F.r=E||0;F.g=D||0;F.b=B||0;F.a=C!=null?C:1;F.add=function(I,H){for(var G=0;G<I.length;++G){F[I.charAt(G)]+=H}return F.normalize()};F.scale=function(I,H){for(var G=0;G<I.length;++G){F[I.charAt(G)]*=H}return F.normalize()};F.toString=function(){if(F.a>=1){return"rgb("+[F.r,F.g,F.b].join(",")+")"}else{return"rgba("+[F.r,F.g,F.b,F.a].join(",")+")"}};F.normalize=function(){function G(I,J,H){return J<I?I:(J>H?H:J)}F.r=G(0,parseInt(F.r),255);F.g=G(0,parseInt(F.g),255);F.b=G(0,parseInt(F.b),255);F.a=G(0,F.a,1);return F};F.clone=function(){return jQuery.color.make(F.r,F.b,F.g,F.a)};return F.normalize()};jQuery.color.extract=function(C,B){var D;do{D=C.css(B).toLowerCase();if(D!=""&&D!="transparent"){break}C=C.parent()}while(!jQuery.nodeName(C.get(0),"body"));if(D=="rgba(0, 0, 0, 0)"){D="transparent"}return jQuery.color.parse(D)};jQuery.color.parse=function(E){var D,B=jQuery.color.make;if(D=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(E)){return B(parseInt(D[1],10),parseInt(D[2],10),parseInt(D[3],10))}if(D=/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(E)){return B(parseInt(D[1],10),parseInt(D[2],10),parseInt(D[3],10),parseFloat(D[4]))}if(D=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(E)){return B(parseFloat(D[1])*2.55,parseFloat(D[2])*2.55,parseFloat(D[3])*2.55)}if(D=/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(E)){return B(parseFloat(D[1])*2.55,parseFloat(D[2])*2.55,parseFloat(D[3])*2.55,parseFloat(D[4]))}if(D=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(E)){return B(parseInt(D[1],16),parseInt(D[2],16),parseInt(D[3],16))}if(D=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(E)){return B(parseInt(D[1]+D[1],16),parseInt(D[2]+D[2],16),parseInt(D[3]+D[3],16))}var C=jQuery.trim(E).toLowerCase();if(C=="transparent"){return B(255,255,255,0)}else{D=A[C];return B(D[0],D[1],D[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]}})();
30 // the actual Flot code
32 function Plot(placeholder, data_, options_, plugins) {
33 // data is on the form:
34 // [ series1, series2 ... ]
35 // where series is either just the data as [ [x1, y1], [x2, y2], ... ]
36 // or { data: [ [x1, y1], [x2, y2], ... ], label: "some label", ... }
40 // the color theme used for graphs
41 colors: ["#edc240", "#afd8f8", "#cb4b4b", "#4da74d", "#9440ed"],
44 noColumns: 1, // number of colums in legend table
45 labelFormatter: null, // fn: string -> string
46 labelBoxBorderColor: "#ccc", // border color for the little label boxes
47 container: null, // container (as jQuery object) to put legend in, null means default on top of graph
48 position: "ne", // position of default legend container within plot
49 margin: 5, // distance from grid edge to default legend container within plot
50 backgroundColor: null, // null means auto-detect
51 backgroundOpacity: 0.85 // set to 0 to avoid background
54 mode: null, // null or "time"
55 transform: null, // null or f: number -> number to transform axis
56 inverseTransform: null, // if transform is set, this should be the inverse function
57 min: null, // min. value to show, null means set automatically
58 max: null, // max. value to show, null means set automatically
59 autoscaleMargin: null, // margin in % to add if auto-setting min/max
60 ticks: null, // either [1, 3] or [[1, "a"], 3] or (fn: axis info -> ticks) or app. number of ticks for auto-ticks
61 tickFormatter: null, // fn: number -> string
62 labelWidth: null, // size of tick labels in pixels
65 // mode specific options
66 tickDecimals: null, // no. of decimals, null means auto
67 tickSize: null, // number or [number, "unit"]
68 minTickSize: null, // number or [number, "unit"]
69 monthNames: null, // list of names of months
70 timeformat: null, // format string to use
71 twelveHourClock: false // 12 or 24 time in time mode
86 lineWidth: 2, // in pixels
91 // we don't put in show: false so we can see
92 // whether lines were actively disabled
93 lineWidth: 2, // in pixels
100 lineWidth: 2, // in pixels
101 barWidth: 1, // in units of the x axis
104 align: "left", // or "center"
105 horizontal: false // when horizontal, left is now top
112 color: "#545454", // primary color used for outline and labels
113 backgroundColor: null, // null for transparent, else color
114 tickColor: "rgba(0,0,0,0.15)", // color used for the ticks
115 labelMargin: 5, // in pixels
116 borderWidth: 2, // in pixels
117 borderColor: null, // set if different from the grid color
118 markings: null, // array of ranges or fn: axes -> array of ranges
119 markingsColor: "#f4f4f4",
120 markingsLineWidth: 2,
124 autoHighlight: true, // highlight in case mouse is near
125 mouseActiveRadius: 10 // how far the mouse can be away to activate an item
129 canvas = null, // the canvas for the plot itself
130 overlay = null, // canvas for interactive stuff on top of plot
131 eventHolder = null, // jQuery object that events should be bound to
132 ctx = null, octx = null,
133 axes = { xaxis: {}, yaxis: {}, x2axis: {}, y2axis: {} },
134 plotOffset = { left: 0, right: 0, top: 0, bottom: 0},
135 canvasWidth = 0, canvasHeight = 0,
136 plotWidth = 0, plotHeight = 0,
140 processDatapoints: [],
148 plot.setData = setData;
149 plot.setupGrid = setupGrid;
151 plot.getPlaceholder = function() { return placeholder; };
152 plot.getCanvas = function() { return canvas; };
153 plot.getPlotOffset = function() { return plotOffset; };
154 plot.width = function () { return plotWidth; };
155 plot.height = function () { return plotHeight; };
156 plot.offset = function () {
157 var o = eventHolder.offset();
158 o.left += plotOffset.left;
159 o.top += plotOffset.top;
162 plot.getData = function() { return series; };
163 plot.getAxes = function() { return axes; };
164 plot.getOptions = function() { return options; };
165 plot.highlight = highlight;
166 plot.unhighlight = unhighlight;
167 plot.triggerRedrawOverlay = triggerRedrawOverlay;
168 plot.pointOffset = function(point) {
169 return { left: parseInt(axisSpecToRealAxis(point, "xaxis").p2c(+point.x) + plotOffset.left),
170 top: parseInt(axisSpecToRealAxis(point, "yaxis").p2c(+point.y) + plotOffset.top) };
179 parseOptions(options_);
187 function executeHooks(hook, args) {
188 args = [plot].concat(args);
189 for (var i = 0; i < hook.length; ++i)
190 hook[i].apply(this, args);
193 function initPlugins() {
194 for (var i = 0; i < plugins.length; ++i) {
198 $.extend(true, options, p.options);
202 function parseOptions(opts) {
203 $.extend(true, options, opts);
204 if (options.grid.borderColor == null)
205 options.grid.borderColor = options.grid.color;
206 // backwards compatibility, to be removed in future
207 if (options.xaxis.noTicks && options.xaxis.ticks == null)
208 options.xaxis.ticks = options.xaxis.noTicks;
209 if (options.yaxis.noTicks && options.yaxis.ticks == null)
210 options.yaxis.ticks = options.yaxis.noTicks;
211 if (options.grid.coloredAreas)
212 options.grid.markings = options.grid.coloredAreas;
213 if (options.grid.coloredAreasColor)
214 options.grid.markingsColor = options.grid.coloredAreasColor;
216 $.extend(true, options.series.lines, options.lines);
218 $.extend(true, options.series.points, options.points);
220 $.extend(true, options.series.bars, options.bars);
221 if (options.shadowSize)
222 options.series.shadowSize = options.shadowSize;
225 if (options.hooks[n] && options.hooks[n].length)
226 hooks[n] = hooks[n].concat(options.hooks[n]);
228 executeHooks(hooks.processOptions, [options]);
231 function setData(d) {
232 series = parseData(d);
233 fillInSeriesOptions();
237 function parseData(d) {
239 for (var i = 0; i < d.length; ++i) {
240 var s = $.extend(true, {}, options.series);
243 s.data = d[i].data; // move the data instead of deep-copy
246 $.extend(true, s, d[i]);
258 function axisSpecToRealAxis(obj, attr) {
262 if (typeof a == "number")
263 return axes[attr.charAt(0) + a + attr.slice(1)];
264 return a; // assume it's OK
267 function fillInSeriesOptions() {
270 // collect what we already got of colors
271 var neededColors = series.length,
274 for (i = 0; i < series.length; ++i) {
275 var sc = series[i].color;
278 if (typeof sc == "number")
279 assignedColors.push(sc);
281 usedColors.push($.color.parse(series[i].color));
285 // we might need to generate more colors if higher indices
287 for (i = 0; i < assignedColors.length; ++i) {
288 neededColors = Math.max(neededColors, assignedColors[i] + 1);
291 // produce colors as needed
292 var colors = [], variation = 0;
294 while (colors.length < neededColors) {
296 if (options.colors.length == i) // check degenerate case
297 c = $.color.make(100, 100, 100);
299 c = $.color.parse(options.colors[i]);
301 // vary color if needed
302 var sign = variation % 2 == 1 ? -1 : 1;
303 c.scale('rgb', 1 + sign * Math.ceil(variation / 2) * 0.2)
305 // FIXME: if we're getting to close to something else,
306 // we should probably skip this one
310 if (i >= options.colors.length) {
316 // fill in the options
318 for (i = 0; i < series.length; ++i) {
322 if (s.color == null) {
323 s.color = colors[colori].toString();
326 else if (typeof s.color == "number")
327 s.color = colors[s.color].toString();
329 // turn on lines automatically in case nothing is set
330 if (s.lines.show == null) {
342 s.xaxis = axisSpecToRealAxis(s, "xaxis");
343 s.yaxis = axisSpecToRealAxis(s, "yaxis");
347 function processData() {
348 var topSentry = Number.POSITIVE_INFINITY,
349 bottomSentry = Number.NEGATIVE_INFINITY,
351 s, points, ps, x, y, axis, val, f, p;
354 axes[axis].datamin = topSentry;
355 axes[axis].datamax = bottomSentry;
356 axes[axis].used = false;
359 function updateAxis(axis, min, max) {
360 if (min < axis.datamin)
362 if (max > axis.datamax)
366 for (i = 0; i < series.length; ++i) {
368 s.datapoints = { points: [] };
370 executeHooks(hooks.processRawData, [ s, s.data, s.datapoints ]);
373 // first pass: clean and copy data
374 for (i = 0; i < series.length; ++i) {
377 var data = s.data, format = s.datapoints.format;
381 // find out how to copy
382 format.push({ x: true, number: true, required: true });
383 format.push({ y: true, number: true, required: true });
386 format.push({ y: true, number: true, required: false, defaultValue: 0 });
388 s.datapoints.format = format;
391 if (s.datapoints.pointsize != null)
392 continue; // already filled in
394 if (s.datapoints.pointsize == null)
395 s.datapoints.pointsize = format.length;
397 ps = s.datapoints.pointsize;
398 points = s.datapoints.points;
400 insertSteps = s.lines.show && s.lines.steps;
401 s.xaxis.used = s.yaxis.used = true;
403 for (j = k = 0; j < data.length; ++j, k += ps) {
406 var nullify = p == null;
408 for (m = 0; m < ps; ++m) {
413 if (f.number && val != null) {
414 val = +val; // convert to number
423 if (f.defaultValue != null)
424 val = f.defaultValue;
433 for (m = 0; m < ps; ++m) {
437 // extract min/max info
439 updateAxis(s.xaxis, val, val);
441 updateAxis(s.yaxis, val, val);
443 points[k + m] = null;
447 // a little bit of line specific stuff that
448 // perhaps shouldn't be here, but lacking
450 if (insertSteps && k > 0
451 && points[k - ps] != null
452 && points[k - ps] != points[k]
453 && points[k - ps + 1] != points[k + 1]) {
454 // copy the point to make room for a middle point
455 for (m = 0; m < ps; ++m)
456 points[k + ps + m] = points[k + m];
458 // middle point has same y
459 points[k + 1] = points[k - ps + 1];
461 // we've added a point, better reflect that
468 // give the hooks a chance to run
469 for (i = 0; i < series.length; ++i) {
472 executeHooks(hooks.processDatapoints, [ s, s.datapoints]);
475 // second pass: find datamax/datamin for auto-scaling
476 for (i = 0; i < series.length; ++i) {
478 points = s.datapoints.points,
479 ps = s.datapoints.pointsize;
481 var xmin = topSentry, ymin = topSentry,
482 xmax = bottomSentry, ymax = bottomSentry;
484 for (j = 0; j < points.length; j += ps) {
485 if (points[j] == null)
488 for (m = 0; m < ps; ++m) {
510 // make sure we got room for the bar on the dancing floor
511 var delta = s.bars.align == "left" ? 0 : -s.bars.barWidth/2;
512 if (s.bars.horizontal) {
514 ymax += delta + s.bars.barWidth;
518 xmax += delta + s.bars.barWidth;
522 updateAxis(s.xaxis, xmin, xmax);
523 updateAxis(s.yaxis, ymin, ymax);
527 if (axes[axis].datamin == topSentry)
528 axes[axis].datamin = null;
529 if (axes[axis].datamax == bottomSentry)
530 axes[axis].datamax = null;
534 function constructCanvas() {
535 function makeCanvas(width, height) {
536 var c = document.createElement('canvas');
539 if ($.browser.msie) // excanvas hack
540 c = window.G_vmlCanvasManager.initElement(c);
544 canvasWidth = placeholder.width();
545 canvasHeight = placeholder.height();
546 placeholder.html(""); // clear placeholder
547 if (placeholder.css("position") == 'static')
548 placeholder.css("position", "relative"); // for positioning labels and overlay
550 if (canvasWidth <= 0 || canvasHeight <= 0)
551 throw "Invalid dimensions for plot, width = " + canvasWidth + ", height = " + canvasHeight;
553 if ($.browser.msie) // excanvas hack
554 window.G_vmlCanvasManager.init_(document); // make sure everything is setup
557 canvas = $(makeCanvas(canvasWidth, canvasHeight)).appendTo(placeholder).get(0);
558 ctx = canvas.getContext("2d");
560 // overlay canvas for interactive features
561 overlay = $(makeCanvas(canvasWidth, canvasHeight)).css({ position: 'absolute', left: 0, top: 0 }).appendTo(placeholder).get(0);
562 octx = overlay.getContext("2d");
566 function bindEvents() {
567 // we include the canvas in the event holder too, because IE 7
568 // sometimes has trouble with the stacking order
569 eventHolder = $([overlay, canvas]);
572 if (options.grid.hoverable)
573 eventHolder.mousemove(onMouseMove);
575 if (options.grid.clickable)
576 eventHolder.click(onClick);
578 executeHooks(hooks.bindEvents, [eventHolder]);
581 function setupGrid() {
582 function setTransformationHelpers(axis, o) {
583 function identity(x) { return x; }
585 var s, m, t = o.transform || identity,
586 it = o.inverseTransform;
588 // add transformation helpers
589 if (axis == axes.xaxis || axis == axes.x2axis) {
590 // precompute how much the axis is scaling a point
592 s = axis.scale = plotWidth / (t(axis.max) - t(axis.min));
595 // data point to canvas coordinate
596 if (t == identity) // slight optimization
597 axis.p2c = function (p) { return (p - m) * s; };
599 axis.p2c = function (p) { return (t(p) - m) * s; };
600 // canvas coordinate to data point
602 axis.c2p = function (c) { return m + c / s; };
604 axis.c2p = function (c) { return it(m + c / s); };
607 s = axis.scale = plotHeight / (t(axis.max) - t(axis.min));
611 axis.p2c = function (p) { return (m - p) * s; };
613 axis.p2c = function (p) { return (m - t(p)) * s; };
615 axis.c2p = function (c) { return m - c / s; };
617 axis.c2p = function (c) { return it(m - c / s); };
621 function measureLabels(axis, axisOptions) {
622 var i, labels = [], l;
624 axis.labelWidth = axisOptions.labelWidth;
625 axis.labelHeight = axisOptions.labelHeight;
627 if (axis == axes.xaxis || axis == axes.x2axis) {
628 // to avoid measuring the widths of the labels, we
629 // construct fixed-size boxes and put the labels inside
630 // them, we don't need the exact figures and the
631 // fixed-size box content is easy to center
632 if (axis.labelWidth == null)
633 axis.labelWidth = canvasWidth / (axis.ticks.length > 0 ? axis.ticks.length : 1);
635 // measure x label heights
636 if (axis.labelHeight == null) {
638 for (i = 0; i < axis.ticks.length; ++i) {
639 l = axis.ticks[i].label;
641 labels.push('<div class="tickLabel" style="float:left;width:' + axis.labelWidth + 'px">' + l + '</div>');
644 if (labels.length > 0) {
645 var dummyDiv = $('<div style="position:absolute;top:-10000px;width:10000px;font-size:smaller">'
646 + labels.join("") + '<div style="clear:left"></div></div>').appendTo(placeholder);
647 axis.labelHeight = dummyDiv.height();
652 else if (axis.labelWidth == null || axis.labelHeight == null) {
653 // calculate y label dimensions
654 for (i = 0; i < axis.ticks.length; ++i) {
655 l = axis.ticks[i].label;
657 labels.push('<div class="tickLabel">' + l + '</div>');
660 if (labels.length > 0) {
661 var dummyDiv = $('<div style="position:absolute;top:-10000px;font-size:smaller">'
662 + labels.join("") + '</div>').appendTo(placeholder);
663 if (axis.labelWidth == null)
664 axis.labelWidth = dummyDiv.width();
665 if (axis.labelHeight == null)
666 axis.labelHeight = dummyDiv.find("div").height();
672 if (axis.labelWidth == null)
674 if (axis.labelHeight == null)
675 axis.labelHeight = 0;
678 function setGridSpacing() {
679 // get the most space needed around the grid for things
680 // that may stick out
681 var maxOutset = options.grid.borderWidth;
682 for (i = 0; i < series.length; ++i)
683 maxOutset = Math.max(maxOutset, 2 * (series[i].points.radius + series[i].points.lineWidth/2));
685 plotOffset.left = plotOffset.right = plotOffset.top = plotOffset.bottom = maxOutset;
687 var margin = options.grid.labelMargin + options.grid.borderWidth;
689 if (axes.xaxis.labelHeight > 0)
690 plotOffset.bottom = Math.max(maxOutset, axes.xaxis.labelHeight + margin);
691 if (axes.yaxis.labelWidth > 0)
692 plotOffset.left = Math.max(maxOutset, axes.yaxis.labelWidth + margin);
693 if (axes.x2axis.labelHeight > 0)
694 plotOffset.top = Math.max(maxOutset, axes.x2axis.labelHeight + margin);
695 if (axes.y2axis.labelWidth > 0)
696 plotOffset.right = Math.max(maxOutset, axes.y2axis.labelWidth + margin);
698 plotWidth = canvasWidth - plotOffset.left - plotOffset.right;
699 plotHeight = canvasHeight - plotOffset.bottom - plotOffset.top;
704 setRange(axes[axis], options[axis]);
706 if (options.grid.show) {
708 prepareTickGeneration(axes[axis], options[axis]);
709 setTicks(axes[axis], options[axis]);
710 measureLabels(axes[axis], options[axis]);
716 plotOffset.left = plotOffset.right = plotOffset.top = plotOffset.bottom = 0;
717 plotWidth = canvasWidth;
718 plotHeight = canvasHeight;
722 setTransformationHelpers(axes[axis], options[axis]);
724 if (options.grid.show)
730 function setRange(axis, axisOptions) {
731 var min = +(axisOptions.min != null ? axisOptions.min : axis.datamin),
732 max = +(axisOptions.max != null ? axisOptions.max : axis.datamax),
737 var widen = max == 0 ? 1 : 0.01;
739 if (axisOptions.min == null)
741 // alway widen max if we couldn't widen min to ensure we
742 // don't fall into min == max which doesn't work
743 if (axisOptions.max == null || axisOptions.min != null)
747 // consider autoscaling
748 var margin = axisOptions.autoscaleMargin;
749 if (margin != null) {
750 if (axisOptions.min == null) {
751 min -= delta * margin;
752 // make sure we don't go below zero if all values
754 if (min < 0 && axis.datamin != null && axis.datamin >= 0)
757 if (axisOptions.max == null) {
758 max += delta * margin;
759 if (max > 0 && axis.datamax != null && axis.datamax <= 0)
768 function prepareTickGeneration(axis, axisOptions) {
769 // estimate number of ticks
771 if (typeof axisOptions.ticks == "number" && axisOptions.ticks > 0)
772 noTicks = axisOptions.ticks;
773 else if (axis == axes.xaxis || axis == axes.x2axis)
774 // heuristic based on the model a*sqrt(x) fitted to
775 // some reasonable data points
776 noTicks = 0.3 * Math.sqrt(canvasWidth);
778 noTicks = 0.3 * Math.sqrt(canvasHeight);
780 var delta = (axis.max - axis.min) / noTicks,
781 size, generator, unit, formatter, i, magn, norm;
783 if (axisOptions.mode == "time") {
784 // pretty handling of time
786 // map of app. size of time units in milliseconds
790 "hour": 60 * 60 * 1000,
791 "day": 24 * 60 * 60 * 1000,
792 "month": 30 * 24 * 60 * 60 * 1000,
793 "year": 365.2425 * 24 * 60 * 60 * 1000
797 // the allowed tick sizes, after 1 year we use
798 // an integer algorithm
800 [1, "second"], [2, "second"], [5, "second"], [10, "second"],
802 [1, "minute"], [2, "minute"], [5, "minute"], [10, "minute"],
804 [1, "hour"], [2, "hour"], [4, "hour"],
805 [8, "hour"], [12, "hour"],
806 [1, "day"], [2, "day"], [3, "day"],
807 [0.25, "month"], [0.5, "month"], [1, "month"],
808 [2, "month"], [3, "month"], [6, "month"],
813 if (axisOptions.minTickSize != null) {
814 if (typeof axisOptions.tickSize == "number")
815 minSize = axisOptions.tickSize;
817 minSize = axisOptions.minTickSize[0] * timeUnitSize[axisOptions.minTickSize[1]];
820 for (i = 0; i < spec.length - 1; ++i)
821 if (delta < (spec[i][0] * timeUnitSize[spec[i][1]]
822 + spec[i + 1][0] * timeUnitSize[spec[i + 1][1]]) / 2
823 && spec[i][0] * timeUnitSize[spec[i][1]] >= minSize)
828 // special-case the possibility of several years
829 if (unit == "year") {
830 magn = Math.pow(10, Math.floor(Math.log(delta / timeUnitSize.year) / Math.LN10));
831 norm = (delta / timeUnitSize.year) / magn;
844 if (axisOptions.tickSize) {
845 size = axisOptions.tickSize[0];
846 unit = axisOptions.tickSize[1];
849 generator = function(axis) {
851 tickSize = axis.tickSize[0], unit = axis.tickSize[1],
852 d = new Date(axis.min);
854 var step = tickSize * timeUnitSize[unit];
856 if (unit == "second")
857 d.setUTCSeconds(floorInBase(d.getUTCSeconds(), tickSize));
858 if (unit == "minute")
859 d.setUTCMinutes(floorInBase(d.getUTCMinutes(), tickSize));
861 d.setUTCHours(floorInBase(d.getUTCHours(), tickSize));
863 d.setUTCMonth(floorInBase(d.getUTCMonth(), tickSize));
865 d.setUTCFullYear(floorInBase(d.getUTCFullYear(), tickSize));
867 // reset smaller components
868 d.setUTCMilliseconds(0);
869 if (step >= timeUnitSize.minute)
871 if (step >= timeUnitSize.hour)
873 if (step >= timeUnitSize.day)
875 if (step >= timeUnitSize.day * 4)
877 if (step >= timeUnitSize.year)
881 var carry = 0, v = Number.NaN, prev;
885 ticks.push({ v: v, label: axis.tickFormatter(v, axis) });
886 if (unit == "month") {
888 // a bit complicated - we'll divide the month
889 // up but we need to take care of fractions
890 // so we don't end up in the middle of a day
892 var start = d.getTime();
893 d.setUTCMonth(d.getUTCMonth() + 1);
894 var end = d.getTime();
895 d.setTime(v + carry * timeUnitSize.hour + (end - start) * tickSize);
896 carry = d.getUTCHours();
900 d.setUTCMonth(d.getUTCMonth() + tickSize);
902 else if (unit == "year") {
903 d.setUTCFullYear(d.getUTCFullYear() + tickSize);
907 } while (v < axis.max && v != prev);
912 formatter = function (v, axis) {
915 // first check global format
916 if (axisOptions.timeformat != null)
917 return $.plot.formatDate(d, axisOptions.timeformat, axisOptions.monthNames);
919 var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]];
920 var span = axis.max - axis.min;
921 var suffix = (axisOptions.twelveHourClock) ? " %p" : "";
923 if (t < timeUnitSize.minute)
924 fmt = "%h:%M:%S" + suffix;
925 else if (t < timeUnitSize.day) {
926 if (span < 2 * timeUnitSize.day)
927 fmt = "%h:%M" + suffix;
929 fmt = "%b %d %h:%M" + suffix;
931 else if (t < timeUnitSize.month)
933 else if (t < timeUnitSize.year) {
934 if (span < timeUnitSize.year)
942 return $.plot.formatDate(d, fmt, axisOptions.monthNames);
946 // pretty rounding of base-10 numbers
947 var maxDec = axisOptions.tickDecimals;
948 var dec = -Math.floor(Math.log(delta) / Math.LN10);
949 if (maxDec != null && dec > maxDec)
952 magn = Math.pow(10, -dec);
953 norm = delta / magn; // norm is between 1.0 and 10.0
959 // special case for 2.5, requires an extra decimal
960 if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
972 if (axisOptions.minTickSize != null && size < axisOptions.minTickSize)
973 size = axisOptions.minTickSize;
975 if (axisOptions.tickSize != null)
976 size = axisOptions.tickSize;
978 axis.tickDecimals = Math.max(0, (maxDec != null) ? maxDec : dec);
980 generator = function (axis) {
983 // spew out all possible ticks
984 var start = floorInBase(axis.min, axis.tickSize),
985 i = 0, v = Number.NaN, prev;
988 v = start + i * axis.tickSize;
989 ticks.push({ v: v, label: axis.tickFormatter(v, axis) });
991 } while (v < axis.max && v != prev);
995 formatter = function (v, axis) {
996 return v.toFixed(axis.tickDecimals);
1000 axis.tickSize = unit ? [size, unit] : size;
1001 axis.tickGenerator = generator;
1002 if ($.isFunction(axisOptions.tickFormatter))
1003 axis.tickFormatter = function (v, axis) { return "" + axisOptions.tickFormatter(v, axis); };
1005 axis.tickFormatter = formatter;
1008 function setTicks(axis, axisOptions) {
1014 if (axisOptions.ticks == null)
1015 axis.ticks = axis.tickGenerator(axis);
1016 else if (typeof axisOptions.ticks == "number") {
1017 if (axisOptions.ticks > 0)
1018 axis.ticks = axis.tickGenerator(axis);
1020 else if (axisOptions.ticks) {
1021 var ticks = axisOptions.ticks;
1023 if ($.isFunction(ticks))
1024 // generate the ticks
1025 ticks = ticks({ min: axis.min, max: axis.max });
1027 // clean up the user-supplied ticks, copy them over
1029 for (i = 0; i < ticks.length; ++i) {
1032 if (typeof t == "object") {
1040 label = axis.tickFormatter(v, axis);
1041 axis.ticks[i] = { v: v, label: label };
1045 if (axisOptions.autoscaleMargin != null && axis.ticks.length > 0) {
1047 if (axisOptions.min == null)
1048 axis.min = Math.min(axis.min, axis.ticks[0].v);
1049 if (axisOptions.max == null && axis.ticks.length > 1)
1050 axis.max = Math.max(axis.max, axis.ticks[axis.ticks.length - 1].v);
1055 ctx.clearRect(0, 0, canvasWidth, canvasHeight);
1057 var grid = options.grid;
1059 if (grid.show && !grid.aboveData)
1062 for (var i = 0; i < series.length; ++i)
1063 drawSeries(series[i]);
1065 executeHooks(hooks.draw, [ctx]);
1067 if (grid.show && grid.aboveData)
1071 function extractRange(ranges, coord) {
1072 var firstAxis = coord + "axis",
1073 secondaryAxis = coord + "2axis",
1074 axis, from, to, reverse;
1076 if (ranges[firstAxis]) {
1077 axis = axes[firstAxis];
1078 from = ranges[firstAxis].from;
1079 to = ranges[firstAxis].to;
1081 else if (ranges[secondaryAxis]) {
1082 axis = axes[secondaryAxis];
1083 from = ranges[secondaryAxis].from;
1084 to = ranges[secondaryAxis].to;
1087 // backwards-compat stuff - to be removed in future
1088 axis = axes[firstAxis];
1089 from = ranges[coord + "1"];
1090 to = ranges[coord + "2"];
1093 // auto-reverse as an added bonus
1094 if (from != null && to != null && from > to)
1095 return { from: to, to: from, axis: axis };
1097 return { from: from, to: to, axis: axis };
1100 function drawGrid() {
1104 ctx.translate(plotOffset.left, plotOffset.top);
1106 // draw background, if any
1107 if (options.grid.backgroundColor) {
1108 ctx.fillStyle = getColorOrGradient(options.grid.backgroundColor, plotHeight, 0, "rgba(255, 255, 255, 0)");
1109 ctx.fillRect(0, 0, plotWidth, plotHeight);
1113 var markings = options.grid.markings;
1115 if ($.isFunction(markings))
1116 // xmin etc. are backwards-compatible, to be removed in future
1117 markings = markings({ xmin: axes.xaxis.min, xmax: axes.xaxis.max, ymin: axes.yaxis.min, ymax: axes.yaxis.max, xaxis: axes.xaxis, yaxis: axes.yaxis, x2axis: axes.x2axis, y2axis: axes.y2axis });
1119 for (i = 0; i < markings.length; ++i) {
1120 var m = markings[i],
1121 xrange = extractRange(m, "x"),
1122 yrange = extractRange(m, "y");
1125 if (xrange.from == null)
1126 xrange.from = xrange.axis.min;
1127 if (xrange.to == null)
1128 xrange.to = xrange.axis.max;
1129 if (yrange.from == null)
1130 yrange.from = yrange.axis.min;
1131 if (yrange.to == null)
1132 yrange.to = yrange.axis.max;
1135 if (xrange.to < xrange.axis.min || xrange.from > xrange.axis.max ||
1136 yrange.to < yrange.axis.min || yrange.from > yrange.axis.max)
1139 xrange.from = Math.max(xrange.from, xrange.axis.min);
1140 xrange.to = Math.min(xrange.to, xrange.axis.max);
1141 yrange.from = Math.max(yrange.from, yrange.axis.min);
1142 yrange.to = Math.min(yrange.to, yrange.axis.max);
1144 if (xrange.from == xrange.to && yrange.from == yrange.to)
1148 xrange.from = xrange.axis.p2c(xrange.from);
1149 xrange.to = xrange.axis.p2c(xrange.to);
1150 yrange.from = yrange.axis.p2c(yrange.from);
1151 yrange.to = yrange.axis.p2c(yrange.to);
1153 if (xrange.from == xrange.to || yrange.from == yrange.to) {
1156 ctx.strokeStyle = m.color || options.grid.markingsColor;
1157 ctx.lineWidth = m.lineWidth || options.grid.markingsLineWidth;
1158 //ctx.moveTo(Math.floor(xrange.from), yrange.from);
1159 //ctx.lineTo(Math.floor(xrange.to), yrange.to);
1160 ctx.moveTo(xrange.from, yrange.from);
1161 ctx.lineTo(xrange.to, yrange.to);
1166 ctx.fillStyle = m.color || options.grid.markingsColor;
1167 ctx.fillRect(xrange.from, yrange.to,
1168 xrange.to - xrange.from,
1169 yrange.from - yrange.to);
1174 // draw the inner grid
1176 ctx.strokeStyle = options.grid.tickColor;
1178 var v, axis = axes.xaxis;
1179 for (i = 0; i < axis.ticks.length; ++i) {
1180 v = axis.ticks[i].v;
1181 if (v <= axis.min || v >= axes.xaxis.max)
1182 continue; // skip those lying on the axes
1184 ctx.moveTo(Math.floor(axis.p2c(v)) + ctx.lineWidth/2, 0);
1185 ctx.lineTo(Math.floor(axis.p2c(v)) + ctx.lineWidth/2, plotHeight);
1189 for (i = 0; i < axis.ticks.length; ++i) {
1190 v = axis.ticks[i].v;
1191 if (v <= axis.min || v >= axis.max)
1194 ctx.moveTo(0, Math.floor(axis.p2c(v)) + ctx.lineWidth/2);
1195 ctx.lineTo(plotWidth, Math.floor(axis.p2c(v)) + ctx.lineWidth/2);
1199 for (i = 0; i < axis.ticks.length; ++i) {
1200 v = axis.ticks[i].v;
1201 if (v <= axis.min || v >= axis.max)
1204 ctx.moveTo(Math.floor(axis.p2c(v)) + ctx.lineWidth/2, -5);
1205 ctx.lineTo(Math.floor(axis.p2c(v)) + ctx.lineWidth/2, 5);
1209 for (i = 0; i < axis.ticks.length; ++i) {
1210 v = axis.ticks[i].v;
1211 if (v <= axis.min || v >= axis.max)
1214 ctx.moveTo(plotWidth-5, Math.floor(axis.p2c(v)) + ctx.lineWidth/2);
1215 ctx.lineTo(plotWidth+5, Math.floor(axis.p2c(v)) + ctx.lineWidth/2);
1220 if (options.grid.borderWidth) {
1222 var bw = options.grid.borderWidth;
1224 ctx.strokeStyle = options.grid.borderColor;
1225 ctx.strokeRect(-bw/2, -bw/2, plotWidth + bw, plotHeight + bw);
1231 function insertLabels() {
1232 placeholder.find(".tickLabels").remove();
1234 var html = ['<div class="tickLabels" style="font-size:smaller;color:' + options.grid.color + '">'];
1236 function addLabels(axis, labelGenerator) {
1237 for (var i = 0; i < axis.ticks.length; ++i) {
1238 var tick = axis.ticks[i];
1239 if (!tick.label || tick.v < axis.min || tick.v > axis.max)
1241 html.push(labelGenerator(tick, axis));
1245 var margin = options.grid.labelMargin + options.grid.borderWidth;
1247 addLabels(axes.xaxis, function (tick, axis) {
1248 return '<div style="position:absolute;top:' + (plotOffset.top + plotHeight + margin) + 'px;left:' + Math.round(plotOffset.left + axis.p2c(tick.v) - axis.labelWidth/2) + 'px;width:' + axis.labelWidth + 'px;text-align:center" class="tickLabel">' + tick.label + "</div>";
1252 addLabels(axes.yaxis, function (tick, axis) {
1253 return '<div style="position:absolute;top:' + Math.round(plotOffset.top + axis.p2c(tick.v) - axis.labelHeight/2) + 'px;right:' + (plotOffset.right + plotWidth + margin) + 'px;width:' + axis.labelWidth + 'px;text-align:right" class="tickLabel">' + tick.label + "</div>";
1256 addLabels(axes.x2axis, function (tick, axis) {
1257 return '<div style="position:absolute;bottom:' + (plotOffset.bottom + plotHeight + margin) + 'px;left:' + Math.round(plotOffset.left + axis.p2c(tick.v) - axis.labelWidth/2) + 'px;width:' + axis.labelWidth + 'px;text-align:center" class="tickLabel">' + tick.label + "</div>";
1260 addLabels(axes.y2axis, function (tick, axis) {
1261 return '<div style="position:absolute;top:' + Math.round(plotOffset.top + axis.p2c(tick.v) - axis.labelHeight/2) + 'px;left:' + (plotOffset.left + plotWidth + margin) +'px;width:' + axis.labelWidth + 'px;text-align:left" class="tickLabel">' + tick.label + "</div>";
1264 html.push('</div>');
1266 placeholder.append(html.join(""));
1269 function drawSeries(series) {
1270 if (series.lines.show)
1271 drawSeriesLines(series);
1272 if (series.bars.show)
1273 drawSeriesBars(series);
1274 if (series.points.show)
1275 drawSeriesPoints(series);
1278 function drawSeriesLines(series) {
1279 function plotLine(datapoints, xoffset, yoffset, axisx, axisy) {
1280 var points = datapoints.points,
1281 ps = datapoints.pointsize,
1282 prevx = null, prevy = null;
1285 for (var i = ps; i < points.length; i += ps) {
1286 var x1 = points[i - ps], y1 = points[i - ps + 1],
1287 x2 = points[i], y2 = points[i + 1];
1289 if (x1 == null || x2 == null)
1293 if (y1 <= y2 && y1 < axisy.min) {
1295 continue; // line segment is outside
1296 // compute new intersection point
1297 x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
1300 else if (y2 <= y1 && y2 < axisy.min) {
1303 x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
1308 if (y1 >= y2 && y1 > axisy.max) {
1311 x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
1314 else if (y2 >= y1 && y2 > axisy.max) {
1317 x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
1322 if (x1 <= x2 && x1 < axisx.min) {
1325 y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
1328 else if (x2 <= x1 && x2 < axisx.min) {
1331 y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
1336 if (x1 >= x2 && x1 > axisx.max) {
1339 y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
1342 else if (x2 >= x1 && x2 > axisx.max) {
1345 y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
1349 if (x1 != prevx || y1 != prevy)
1350 ctx.moveTo(axisx.p2c(x1) + xoffset, axisy.p2c(y1) + yoffset);
1354 ctx.lineTo(axisx.p2c(x2) + xoffset, axisy.p2c(y2) + yoffset);
1359 function plotLineArea(datapoints, axisx, axisy) {
1360 var points = datapoints.points,
1361 ps = datapoints.pointsize,
1362 bottom = Math.min(Math.max(0, axisy.min), axisy.max),
1363 top, lastX = 0, areaOpen = false;
1365 for (var i = ps; i < points.length; i += ps) {
1366 var x1 = points[i - ps], y1 = points[i - ps + 1],
1367 x2 = points[i], y2 = points[i + 1];
1369 if (areaOpen && x1 != null && x2 == null) {
1371 ctx.lineTo(axisx.p2c(lastX), axisy.p2c(bottom));
1377 if (x1 == null || x2 == null)
1383 if (x1 <= x2 && x1 < axisx.min) {
1386 y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
1389 else if (x2 <= x1 && x2 < axisx.min) {
1392 y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
1397 if (x1 >= x2 && x1 > axisx.max) {
1400 y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
1403 else if (x2 >= x1 && x2 > axisx.max) {
1406 y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
1413 ctx.moveTo(axisx.p2c(x1), axisy.p2c(bottom));
1417 // now first check the case where both is outside
1418 if (y1 >= axisy.max && y2 >= axisy.max) {
1419 ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.max));
1420 ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.max));
1424 else if (y1 <= axisy.min && y2 <= axisy.min) {
1425 ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.min));
1426 ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.min));
1431 // else it's a bit more complicated, there might
1432 // be two rectangles and two triangles we need to fill
1433 // in; to find these keep track of the current x values
1434 var x1old = x1, x2old = x2;
1436 // and clip the y values, without shortcutting
1439 if (y1 <= y2 && y1 < axisy.min && y2 >= axisy.min) {
1440 x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
1443 else if (y2 <= y1 && y2 < axisy.min && y1 >= axisy.min) {
1444 x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
1449 if (y1 >= y2 && y1 > axisy.max && y2 <= axisy.max) {
1450 x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
1453 else if (y2 >= y1 && y2 > axisy.max && y1 <= axisy.max) {
1454 x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
1459 // if the x value was changed we got a rectangle
1462 if (y1 <= axisy.min)
1467 ctx.lineTo(axisx.p2c(x1old), axisy.p2c(top));
1468 ctx.lineTo(axisx.p2c(x1), axisy.p2c(top));
1471 // fill the triangles
1472 ctx.lineTo(axisx.p2c(x1), axisy.p2c(y1));
1473 ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
1475 // fill the other rectangle if it's there
1477 if (y2 <= axisy.min)
1482 ctx.lineTo(axisx.p2c(x2), axisy.p2c(top));
1483 ctx.lineTo(axisx.p2c(x2old), axisy.p2c(top));
1486 lastX = Math.max(x2, x2old);
1490 ctx.lineTo(axisx.p2c(lastX), axisy.p2c(bottom));
1496 ctx.translate(plotOffset.left, plotOffset.top);
1497 ctx.lineJoin = "round";
1499 var lw = series.lines.lineWidth,
1500 sw = series.shadowSize;
1501 // FIXME: consider another form of shadow when filling is turned on
1502 if (lw > 0 && sw > 0) {
1503 // draw shadow as a thick and thin line with transparency
1505 ctx.strokeStyle = "rgba(0,0,0,0.1)";
1506 // position shadow at angle from the mid of line
1507 var angle = Math.PI/18;
1508 plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/2), Math.cos(angle) * (lw/2 + sw/2), series.xaxis, series.yaxis);
1509 ctx.lineWidth = sw/2;
1510 plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/4), Math.cos(angle) * (lw/2 + sw/4), series.xaxis, series.yaxis);
1514 ctx.strokeStyle = series.color;
1515 var fillStyle = getFillStyle(series.lines, series.color, 0, plotHeight);
1517 ctx.fillStyle = fillStyle;
1518 plotLineArea(series.datapoints, series.xaxis, series.yaxis);
1522 plotLine(series.datapoints, 0, 0, series.xaxis, series.yaxis);
1526 function drawSeriesPoints(series) {
1527 function plotPoints(datapoints, radius, fillStyle, offset, circumference, axisx, axisy) {
1528 var points = datapoints.points, ps = datapoints.pointsize;
1530 for (var i = 0; i < points.length; i += ps) {
1531 var x = points[i], y = points[i + 1];
1532 if (x == null || x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
1536 ctx.arc(axisx.p2c(x), axisy.p2c(y) + offset, radius, 0, circumference, false);
1538 ctx.fillStyle = fillStyle;
1546 ctx.translate(plotOffset.left, plotOffset.top);
1548 var lw = series.lines.lineWidth,
1549 sw = series.shadowSize,
1550 radius = series.points.radius;
1551 if (lw > 0 && sw > 0) {
1552 // draw shadow in two steps
1555 ctx.strokeStyle = "rgba(0,0,0,0.1)";
1556 plotPoints(series.datapoints, radius, null, w + w/2, Math.PI,
1557 series.xaxis, series.yaxis);
1559 ctx.strokeStyle = "rgba(0,0,0,0.2)";
1560 plotPoints(series.datapoints, radius, null, w/2, Math.PI,
1561 series.xaxis, series.yaxis);
1565 ctx.strokeStyle = series.color;
1566 plotPoints(series.datapoints, radius,
1567 getFillStyle(series.points, series.color), 0, 2 * Math.PI,
1568 series.xaxis, series.yaxis);
1572 function drawBar(x, y, b, barLeft, barRight, offset, fillStyleCallback, axisx, axisy, c, horizontal) {
1573 var left, right, bottom, top,
1574 drawLeft, drawRight, drawTop, drawBottom,
1578 drawBottom = drawRight = drawTop = true;
1583 bottom = y + barRight;
1585 // account for negative bars
1595 drawLeft = drawRight = drawTop = true;
1598 right = x + barRight;
1602 // account for negative bars
1613 if (right < axisx.min || left > axisx.max ||
1614 top < axisy.min || bottom > axisy.max)
1617 if (left < axisx.min) {
1622 if (right > axisx.max) {
1627 if (bottom < axisy.min) {
1632 if (top > axisy.max) {
1637 left = axisx.p2c(left);
1638 bottom = axisy.p2c(bottom);
1639 right = axisx.p2c(right);
1640 top = axisy.p2c(top);
1643 if (fillStyleCallback) {
1645 c.moveTo(left, bottom);
1646 c.lineTo(left, top);
1647 c.lineTo(right, top);
1648 c.lineTo(right, bottom);
1649 c.fillStyle = fillStyleCallback(bottom, top);
1654 if (drawLeft || drawRight || drawTop || drawBottom) {
1657 // FIXME: inline moveTo is buggy with excanvas
1658 c.moveTo(left, bottom + offset);
1660 c.lineTo(left, top + offset);
1662 c.moveTo(left, top + offset);
1664 c.lineTo(right, top + offset);
1666 c.moveTo(right, top + offset);
1668 c.lineTo(right, bottom + offset);
1670 c.moveTo(right, bottom + offset);
1672 c.lineTo(left, bottom + offset);
1674 c.moveTo(left, bottom + offset);
1679 function drawSeriesBars(series) {
1680 function plotBars(datapoints, barLeft, barRight, offset, fillStyleCallback, axisx, axisy) {
1681 var points = datapoints.points, ps = datapoints.pointsize;
1683 for (var i = 0; i < points.length; i += ps) {
1684 if (points[i] == null)
1686 drawBar(points[i], points[i + 1], points[i + 2], barLeft, barRight, offset, fillStyleCallback, axisx, axisy, ctx, series.bars.horizontal);
1691 ctx.translate(plotOffset.left, plotOffset.top);
1693 // FIXME: figure out a way to add shadows (for instance along the right edge)
1694 ctx.lineWidth = series.bars.lineWidth;
1695 ctx.strokeStyle = series.color;
1696 var barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
1697 var fillStyleCallback = series.bars.fill ? function (bottom, top) { return getFillStyle(series.bars, series.color, bottom, top); } : null;
1698 plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, 0, fillStyleCallback, series.xaxis, series.yaxis);
1702 function getFillStyle(filloptions, seriesColor, bottom, top) {
1703 var fill = filloptions.fill;
1707 if (filloptions.fillColor)
1708 return getColorOrGradient(filloptions.fillColor, bottom, top, seriesColor);
1710 var c = $.color.parse(seriesColor);
1711 c.a = typeof fill == "number" ? fill : 0.4;
1713 return c.toString();
1716 function insertLegend() {
1717 placeholder.find(".legend").remove();
1719 if (!options.legend.show)
1722 var fragments = [], rowStarted = false,
1723 lf = options.legend.labelFormatter, s, label;
1724 for (i = 0; i < series.length; ++i) {
1730 if (i % options.legend.noColumns == 0) {
1732 fragments.push('</tr>');
1733 fragments.push('<tr>');
1738 label = lf(label, s);
1741 '<td class="legendColorBox"><div style="border:1px solid ' + options.legend.labelBoxBorderColor + ';padding:1px"><div style="width:4px;height:0;border:5px solid ' + s.color + ';overflow:hidden"></div></div></td>' +
1742 '<td class="legendLabel">' + label + '</td>');
1745 fragments.push('</tr>');
1747 if (fragments.length == 0)
1750 var table = '<table style="font-size:smaller;color:' + options.grid.color + '">' + fragments.join("") + '</table>';
1751 if (options.legend.container != null)
1752 $(options.legend.container).html(table);
1755 p = options.legend.position,
1756 m = options.legend.margin;
1759 if (p.charAt(0) == "n")
1760 pos += 'top:' + (m[1] + plotOffset.top) + 'px;';
1761 else if (p.charAt(0) == "s")
1762 pos += 'bottom:' + (m[1] + plotOffset.bottom) + 'px;';
1763 if (p.charAt(1) == "e")
1764 pos += 'right:' + (m[0] + plotOffset.right) + 'px;';
1765 else if (p.charAt(1) == "w")
1766 pos += 'left:' + (m[0] + plotOffset.left) + 'px;';
1767 var legend = $('<div class="legend">' + table.replace('style="', 'style="position:absolute;' + pos +';') + '</div>').appendTo(placeholder);
1768 if (options.legend.backgroundOpacity != 0.0) {
1769 // put in the transparent background
1770 // separately to avoid blended labels and
1772 var c = options.legend.backgroundColor;
1774 c = options.grid.backgroundColor;
1775 if (c && typeof c == "string")
1776 c = $.color.parse(c);
1778 c = $.color.extract(legend, 'background-color');
1782 var div = legend.children();
1783 $('<div style="position:absolute;width:' + div.width() + 'px;height:' + div.height() + 'px;' + pos +'background-color:' + c + ';"> </div>').prependTo(legend).css('opacity', options.legend.backgroundOpacity);
1789 // interactive features
1791 var highlights = [],
1792 redrawTimeout = null;
1794 // returns the data item the mouse is over, or null if none is found
1795 function findNearbyItem(mouseX, mouseY, seriesFilter) {
1796 var maxDistance = options.grid.mouseActiveRadius,
1797 smallestDistance = maxDistance * maxDistance + 1,
1798 item = null, foundPoint = false, i, j;
1800 for (i = 0; i < series.length; ++i) {
1801 if (!seriesFilter(series[i]))
1807 points = s.datapoints.points,
1808 ps = s.datapoints.pointsize,
1809 mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster
1810 my = axisy.c2p(mouseY),
1811 maxx = maxDistance / axisx.scale,
1812 maxy = maxDistance / axisy.scale;
1814 if (s.lines.show || s.points.show) {
1815 for (j = 0; j < points.length; j += ps) {
1816 var x = points[j], y = points[j + 1];
1820 // For points and lines, the cursor must be within a
1821 // certain distance to the data point
1822 if (x - mx > maxx || x - mx < -maxx ||
1823 y - my > maxy || y - my < -maxy)
1826 // We have to calculate distances in pixels, not in
1827 // data units, because the scales of the axes may be different
1828 var dx = Math.abs(axisx.p2c(x) - mouseX),
1829 dy = Math.abs(axisy.p2c(y) - mouseY),
1830 dist = dx * dx + dy * dy; // we save the sqrt
1832 // use <= to ensure last point takes precedence
1833 // (last generally means on top of)
1834 if (dist <= smallestDistance) {
1835 smallestDistance = dist;
1841 if (s.bars.show && !item) { // no other point can be nearby
1842 var barLeft = s.bars.align == "left" ? 0 : -s.bars.barWidth/2,
1843 barRight = barLeft + s.bars.barWidth;
1845 for (j = 0; j < points.length; j += ps) {
1846 var x = points[j], y = points[j + 1], b = points[j + 2];
1850 // for a bar graph, the cursor must be inside the bar
1851 if (series[i].bars.horizontal ?
1852 (mx <= Math.max(b, x) && mx >= Math.min(b, x) &&
1853 my >= y + barLeft && my <= y + barRight) :
1854 (mx >= x + barLeft && mx <= x + barRight &&
1855 my >= Math.min(b, y) && my <= Math.max(b, y)))
1864 ps = series[i].datapoints.pointsize;
1866 return { datapoint: series[i].datapoints.points.slice(j * ps, (j + 1) * ps),
1875 function onMouseMove(e) {
1876 if (options.grid.hoverable)
1877 triggerClickHoverEvent("plothover", e,
1878 function (s) { return s["hoverable"] != false; });
1881 function onClick(e) {
1882 triggerClickHoverEvent("plotclick", e,
1883 function (s) { return s["clickable"] != false; });
1886 // trigger click or hover event (they send the same parameters
1887 // so we share their code)
1888 function triggerClickHoverEvent(eventname, event, seriesFilter) {
1889 var offset = eventHolder.offset(),
1890 pos = { pageX: event.pageX, pageY: event.pageY },
1891 canvasX = event.pageX - offset.left - plotOffset.left,
1892 canvasY = event.pageY - offset.top - plotOffset.top;
1894 if (axes.xaxis.used)
1895 pos.x = axes.xaxis.c2p(canvasX);
1896 if (axes.yaxis.used)
1897 pos.y = axes.yaxis.c2p(canvasY);
1898 if (axes.x2axis.used)
1899 pos.x2 = axes.x2axis.c2p(canvasX);
1900 if (axes.y2axis.used)
1901 pos.y2 = axes.y2axis.c2p(canvasY);
1903 var item = findNearbyItem(canvasX, canvasY, seriesFilter);
1906 // fill in mouse pos for any listeners out there
1907 item.pageX = parseInt(item.series.xaxis.p2c(item.datapoint[0]) + offset.left + plotOffset.left);
1908 item.pageY = parseInt(item.series.yaxis.p2c(item.datapoint[1]) + offset.top + plotOffset.top);
1911 if (options.grid.autoHighlight) {
1912 // clear auto-highlights
1913 for (var i = 0; i < highlights.length; ++i) {
1914 var h = highlights[i];
1915 if (h.auto == eventname &&
1916 !(item && h.series == item.series && h.point == item.datapoint))
1917 unhighlight(h.series, h.point);
1921 highlight(item.series, item.datapoint, eventname);
1924 placeholder.trigger(eventname, [ pos, item ]);
1927 function triggerRedrawOverlay() {
1929 redrawTimeout = setTimeout(drawOverlay, 30);
1932 function drawOverlay() {
1933 redrawTimeout = null;
1937 octx.clearRect(0, 0, canvasWidth, canvasHeight);
1938 octx.translate(plotOffset.left, plotOffset.top);
1941 for (i = 0; i < highlights.length; ++i) {
1944 if (hi.series.bars.show)
1945 drawBarHighlight(hi.series, hi.point);
1947 drawPointHighlight(hi.series, hi.point);
1951 executeHooks(hooks.drawOverlay, [octx]);
1954 function highlight(s, point, auto) {
1955 if (typeof s == "number")
1958 if (typeof point == "number")
1959 point = s.data[point];
1961 var i = indexOfHighlight(s, point);
1963 highlights.push({ series: s, point: point, auto: auto });
1965 triggerRedrawOverlay();
1968 highlights[i].auto = false;
1971 function unhighlight(s, point) {
1972 if (s == null && point == null) {
1974 triggerRedrawOverlay();
1977 if (typeof s == "number")
1980 if (typeof point == "number")
1981 point = s.data[point];
1983 var i = indexOfHighlight(s, point);
1985 highlights.splice(i, 1);
1987 triggerRedrawOverlay();
1991 function indexOfHighlight(s, p) {
1992 for (var i = 0; i < highlights.length; ++i) {
1993 var h = highlights[i];
1994 if (h.series == s && h.point[0] == p[0]
1995 && h.point[1] == p[1])
2001 function drawPointHighlight(series, point) {
2002 var x = point[0], y = point[1],
2003 axisx = series.xaxis, axisy = series.yaxis;
2005 if (x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
2008 var pointRadius = series.points.radius + series.points.lineWidth / 2;
2009 octx.lineWidth = pointRadius;
2010 octx.strokeStyle = $.color.parse(series.color).scale('a', 0.5).toString();
2011 var radius = 1.5 * pointRadius;
2013 octx.arc(axisx.p2c(x), axisy.p2c(y), radius, 0, 2 * Math.PI, false);
2017 function drawBarHighlight(series, point) {
2018 octx.lineWidth = series.bars.lineWidth;
2019 octx.strokeStyle = $.color.parse(series.color).scale('a', 0.5).toString();
2020 var fillStyle = $.color.parse(series.color).scale('a', 0.5).toString();
2021 var barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
2022 drawBar(point[0], point[1], point[2] || 0, barLeft, barLeft + series.bars.barWidth,
2023 0, function () { return fillStyle; }, series.xaxis, series.yaxis, octx, series.bars.horizontal);
2026 function getColorOrGradient(spec, bottom, top, defaultColor) {
2027 if (typeof spec == "string")
2030 // assume this is a gradient spec; IE currently only
2031 // supports a simple vertical gradient properly, so that's
2032 // what we support too
2033 var gradient = ctx.createLinearGradient(0, top, 0, bottom);
2035 for (var i = 0, l = spec.colors.length; i < l; ++i) {
2036 var c = spec.colors[i];
2037 if (typeof c != "string") {
2038 c = $.color.parse(defaultColor).scale('rgb', c.brightness);
2042 gradient.addColorStop(i / (l - 1), c);
2050 $.plot = function(placeholder, data, options) {
2051 var plot = new Plot($(placeholder), data, options, $.plot.plugins);
2052 /*var t0 = new Date();
2053 var t1 = new Date();
2054 var tstr = "time used (msecs): " + (t1.getTime() - t0.getTime())
2062 $.plot.plugins = [];
2064 // returns a string with the date d formatted according to fmt
2065 $.plot.formatDate = function(d, fmt, monthNames) {
2066 var leftPad = function(n) {
2068 return n.length == 1 ? "0" + n : n;
2073 var hours = d.getUTCHours();
2074 var isAM = hours < 12;
2075 if (monthNames == null)
2076 monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
2078 if (fmt.search(/%p|%P/) != -1) {
2081 } else if (hours == 0) {
2085 for (var i = 0; i < fmt.length; ++i) {
2086 var c = fmt.charAt(i);
2090 case 'h': c = "" + hours; break;
2091 case 'H': c = leftPad(hours); break;
2092 case 'M': c = leftPad(d.getUTCMinutes()); break;
2093 case 'S': c = leftPad(d.getUTCSeconds()); break;
2094 case 'd': c = "" + d.getUTCDate(); break;
2095 case 'm': c = "" + (d.getUTCMonth() + 1); break;
2096 case 'y': c = "" + d.getUTCFullYear(); break;
2097 case 'b': c = "" + monthNames[d.getUTCMonth()]; break;
2098 case 'p': c = (isAM) ? ("" + "am") : ("" + "pm"); break;
2099 case 'P': c = (isAM) ? ("" + "AM") : ("" + "PM"); break;
2114 // round to nearby lower multiple of base
2115 function floorInBase(n, base) {
2116 return base * Math.floor(n / base);