1
/*! Javascript plotting library for jQuery, v. 0.7.
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() return the same modified object
26
* instead of making a new one.
28
* V. 1.1: Fix error handling so e.g. parsing an empty string does
29
* produce a color rather than just crashing.
31
(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);
33
// the actual Flot code
35
function Plot(placeholder, data_, options_, plugins) {
36
// data is on the form:
37
// [ series1, series2 ... ]
38
// where series is either just the data as [ [x1, y1], [x2, y2], ... ]
39
// or { data: [ [x1, y1], [x2, y2], ... ], label: "some label", ... }
43
// the color theme used for graphs
44
colors: ["#92d5ea", "#666699", "#be1e2d", "#ee8310", "#8d10ee"],
47
noColumns: 0, // number of colums in legend table
48
labelFormatter: null, // fn: string -> string
49
labelBoxBorderColor: "none", // border color for the little label boxes
50
container: null, // container (as jQuery object) to put legend in, null means default on top of graph
51
position: "ne", // position of default legend container within plot
52
margin: 0, // distance from grid edge to default legend container within plot
53
backgroundColor: null, // null means auto-detect
54
backgroundOpacity: 0 // set to 0 to avoid background
57
show: null, // null = auto-detect, true = always, false = never
58
position: "bottom", // or "top"
59
mode: null, // null or "time"
60
color: null, // base color, labels, ticks
61
tickColor: null, // possibly different color of ticks, e.g. "rgba(0,0,0,0.15)"
62
transform: null, // null or f: number -> number to transform axis
63
inverseTransform: null, // if transform is set, this should be the inverse function
64
min: null, // min. value to show, null means set automatically
65
max: null, // max. value to show, null means set automatically
66
autoscaleMargin: null, // margin in % to add if auto-setting min/max
67
ticks: null, // either [1, 3] or [[1, "a"], 3] or (fn: axis info -> ticks) or app. number of ticks for auto-ticks
68
tickFormatter: null, // fn: number -> string
69
labelWidth: null, // size of tick labels in pixels
71
reserveSpace: null, // whether to reserve space even if axis isn't shown
72
tickLength: null, // size in pixels of ticks, or "full" for whole line
73
alignTicksWithAxis: null, // axis number or null for no sync
75
// mode specific options
76
tickDecimals: 0, // no. of decimals, null means auto
77
tickSize: null, // number or [number, "unit"]
78
minTickSize: null, // number or [number, "unit"]
79
monthNames: null, // list of names of months
80
timeformat: null, // format string to use
81
twelveHourClock: false // 12 or 24 time in time mode
84
autoscaleMargin: 0.02,
85
position: "left" // or "right"
93
lineWidth: 2, // in pixels
96
symbol: "circle" // or callback
99
// we don't put in show: false so we can see
100
// whether lines were actively disabled
101
lineWidth: 2, // in pixels
109
lineWidth: 2, // in pixels
110
barWidth: 1, // in units of the x axis
113
align: "left", // or "center"
121
color: "#545454", // primary color used for outline and labels
122
backgroundColor: null, // null for transparent, else color
123
borderColor: null, // set if different from the grid color
124
tickColor: null, // color for the ticks, e.g. "rgba(0,0,0,0.15)"
125
labelMargin: 5, // in pixels
126
axisMargin: 8, // in pixels
127
borderWidth: 1, // in pixels
128
minBorderMargin: null, // in pixels, null means taken from points radius
129
markings: null, // array of ranges or fn: axes -> array of ranges
130
markingsColor: "#f4f4f4",
131
markingsLineWidth: 2,
135
autoHighlight: true, // highlight in case mouse is near
136
mouseActiveRadius: 5 // how far the mouse can be away to activate an item
140
canvas = null, // the canvas for the plot itself
141
overlay = null, // canvas for interactive stuff on top of plot
142
eventHolder = null, // jQuery object that events should be bound to
143
ctx = null, octx = null,
144
xaxes = [], yaxes = [],
145
plotOffset = { left: 0, right: 0, top: 0, bottom: 0},
146
canvasWidth = 0, canvasHeight = 0,
147
plotWidth = 0, plotHeight = 0,
151
processDatapoints: [],
161
plot.setData = setData;
162
plot.setupGrid = setupGrid;
164
plot.getPlaceholder = function() { return placeholder; };
165
plot.getCanvas = function() { return canvas; };
166
plot.getPlotOffset = function() { return plotOffset; };
167
plot.width = function () { return plotWidth; };
168
plot.height = function () { return plotHeight; };
169
plot.offset = function () {
170
var o = eventHolder.offset();
171
o.left += plotOffset.left;
172
o.top += plotOffset.top;
175
plot.getData = function () { return series; };
176
plot.getAxes = function () {
178
$.each(xaxes.concat(yaxes), function (_, axis) {
180
res[axis.direction + (axis.n != 1 ? axis.n : "") + "axis"] = axis;
184
plot.getXAxes = function () { return xaxes; };
185
plot.getYAxes = function () { return yaxes; };
186
plot.c2p = canvasToAxisCoords;
187
plot.p2c = axisToCanvasCoords;
188
plot.getOptions = function () { return options; };
189
plot.highlight = highlight;
190
plot.unhighlight = unhighlight;
191
plot.triggerRedrawOverlay = triggerRedrawOverlay;
192
plot.pointOffset = function(point) {
194
left: parseInt(xaxes[axisNumber(point, "x") - 1].p2c(+point.x) + plotOffset.left),
195
top: parseInt(yaxes[axisNumber(point, "y") - 1].p2c(+point.y) + plotOffset.top)
198
plot.shutdown = shutdown;
199
plot.resize = function () {
200
getCanvasDimensions();
201
resizeCanvas(canvas);
202
resizeCanvas(overlay);
210
parseOptions(options_);
218
function executeHooks(hook, args) {
219
args = [plot].concat(args);
220
for (var i = 0; i < hook.length; ++i)
221
hook[i].apply(this, args);
224
function initPlugins() {
225
for (var i = 0; i < plugins.length; ++i) {
229
$.extend(true, options, p.options);
233
function parseOptions(opts) {
236
$.extend(true, options, opts);
238
if (options.xaxis.color == null)
239
options.xaxis.color = options.grid.color;
240
if (options.yaxis.color == null)
241
options.yaxis.color = options.grid.color;
243
if (options.xaxis.tickColor == null) // backwards-compatibility
244
options.xaxis.tickColor = options.grid.tickColor;
245
if (options.yaxis.tickColor == null) // backwards-compatibility
246
options.yaxis.tickColor = options.grid.tickColor;
248
if (options.grid.borderColor == null)
249
options.grid.borderColor = options.grid.color;
250
if (options.grid.tickColor == null)
251
options.grid.tickColor = $.color.parse(options.grid.color).scale('a', 0.22).toString();
253
// fill in defaults in axes, copy at least always the
254
// first as the rest of the code assumes it'll be there
255
for (i = 0; i < Math.max(1, options.xaxes.length); ++i)
256
options.xaxes[i] = $.extend(true, {}, options.xaxis, options.xaxes[i]);
257
for (i = 0; i < Math.max(1, options.yaxes.length); ++i)
258
options.yaxes[i] = $.extend(true, {}, options.yaxis, options.yaxes[i]);
260
// backwards compatibility, to be removed in future
261
if (options.xaxis.noTicks && options.xaxis.ticks == null)
262
options.xaxis.ticks = options.xaxis.noTicks;
263
if (options.yaxis.noTicks && options.yaxis.ticks == null)
264
options.yaxis.ticks = options.yaxis.noTicks;
265
if (options.x2axis) {
266
options.xaxes[1] = $.extend(true, {}, options.xaxis, options.x2axis);
267
options.xaxes[1].position = "top";
269
if (options.y2axis) {
270
options.yaxes[1] = $.extend(true, {}, options.yaxis, options.y2axis);
271
options.yaxes[1].position = "right";
273
if (options.grid.coloredAreas)
274
options.grid.markings = options.grid.coloredAreas;
275
if (options.grid.coloredAreasColor)
276
options.grid.markingsColor = options.grid.coloredAreasColor;
278
$.extend(true, options.series.lines, options.lines);
280
$.extend(true, options.series.points, options.points);
282
$.extend(true, options.series.bars, options.bars);
283
if (options.shadowSize != null)
284
options.series.shadowSize = options.shadowSize;
286
// save options on axes for future reference
287
for (i = 0; i < options.xaxes.length; ++i)
288
getOrCreateAxis(xaxes, i + 1).options = options.xaxes[i];
289
for (i = 0; i < options.yaxes.length; ++i)
290
getOrCreateAxis(yaxes, i + 1).options = options.yaxes[i];
292
// add hooks from options
294
if (options.hooks[n] && options.hooks[n].length)
295
hooks[n] = hooks[n].concat(options.hooks[n]);
297
executeHooks(hooks.processOptions, [options]);
300
function setData(d) {
301
series = parseData(d);
302
fillInSeriesOptions();
306
function parseData(d) {
308
for (var i = 0; i < d.length; ++i) {
309
var s = $.extend(true, {}, options.series);
311
if (d[i].data != null) {
312
s.data = d[i].data; // move the data instead of deep-copy
315
$.extend(true, s, d[i]);
327
function axisNumber(obj, coord) {
328
var a = obj[coord + "axis"];
329
if (typeof a == "object") // if we got a real axis, extract number
331
if (typeof a != "number")
332
a = 1; // default to first axis
337
// return flat array without annoying null entries
338
return $.grep(xaxes.concat(yaxes), function (a) { return a; });
341
function canvasToAxisCoords(pos) {
342
// return an object with x/y corresponding to all used axes
343
var res = {}, i, axis;
344
for (i = 0; i < xaxes.length; ++i) {
346
if (axis && axis.used)
347
res["x" + axis.n] = axis.c2p(pos.left);
350
for (i = 0; i < yaxes.length; ++i) {
352
if (axis && axis.used)
353
res["y" + axis.n] = axis.c2p(pos.top);
356
if (res.x1 !== undefined)
358
if (res.y1 !== undefined)
364
function axisToCanvasCoords(pos) {
365
// get canvas coords from the first pair of x/y found in pos
366
var res = {}, i, axis, key;
368
for (i = 0; i < xaxes.length; ++i) {
370
if (axis && axis.used) {
372
if (pos[key] == null && axis.n == 1)
375
if (pos[key] != null) {
376
res.left = axis.p2c(pos[key]);
382
for (i = 0; i < yaxes.length; ++i) {
384
if (axis && axis.used) {
386
if (pos[key] == null && axis.n == 1)
389
if (pos[key] != null) {
390
res.top = axis.p2c(pos[key]);
399
function getOrCreateAxis(axes, number) {
400
if (!axes[number - 1])
402
n: number, // save the number for future reference
403
direction: axes == xaxes ? "x" : "y",
404
options: $.extend(true, {}, axes == xaxes ? options.xaxis : options.yaxis)
407
return axes[number - 1];
410
function fillInSeriesOptions() {
413
// collect what we already got of colors
414
var neededColors = series.length,
417
for (i = 0; i < series.length; ++i) {
418
var sc = series[i].color;
421
if (typeof sc == "number")
422
assignedColors.push(sc);
424
usedColors.push($.color.parse(series[i].color));
428
// we might need to generate more colors if higher indices
430
for (i = 0; i < assignedColors.length; ++i) {
431
neededColors = Math.max(neededColors, assignedColors[i] + 1);
434
// produce colors as needed
435
var colors = [], variation = 0;
437
while (colors.length < neededColors) {
439
if (options.colors.length == i) // check degenerate case
440
c = $.color.make(100, 100, 100);
442
c = $.color.parse(options.colors[i]);
444
// vary color if needed
445
var sign = variation % 2 == 1 ? -1 : 1;
446
c.scale('rgb', 1 + sign * Math.ceil(variation / 2) * 0.2)
448
// FIXME: if we're getting to close to something else,
449
// we should probably skip this one
453
if (i >= options.colors.length) {
459
// fill in the options
461
for (i = 0; i < series.length; ++i) {
465
if (s.color == null) {
466
s.color = colors[colori].toString();
469
else if (typeof s.color == "number")
470
s.color = colors[s.color].toString();
472
// turn on lines automatically in case nothing is set
473
if (s.lines.show == null) {
476
if (s[v] && s[v].show) {
485
s.xaxis = getOrCreateAxis(xaxes, axisNumber(s, "x"));
486
s.yaxis = getOrCreateAxis(yaxes, axisNumber(s, "y"));
490
function processData() {
491
var topSentry = Number.POSITIVE_INFINITY,
492
bottomSentry = Number.NEGATIVE_INFINITY,
493
fakeInfinity = Number.MAX_VALUE,
495
s, points, ps, x, y, axis, val, f, p;
497
function updateAxis(axis, min, max) {
498
if (min < axis.datamin && min != -fakeInfinity)
500
if (max > axis.datamax && max != fakeInfinity)
504
$.each(allAxes(), function (_, axis) {
506
axis.datamin = topSentry;
507
axis.datamax = bottomSentry;
511
for (i = 0; i < series.length; ++i) {
513
s.datapoints = { points: [] };
515
executeHooks(hooks.processRawData, [ s, s.data, s.datapoints ]);
518
// first pass: clean and copy data
519
for (i = 0; i < series.length; ++i) {
522
var data = s.data, format = s.datapoints.format;
526
// find out how to copy
527
format.push({ x: true, number: true, required: true });
528
format.push({ y: true, number: true, required: true });
530
if (s.bars.show || (s.lines.show && s.lines.fill)) {
531
format.push({ y: true, number: true, required: false, defaultValue: 0 });
532
if (s.bars.horizontal) {
533
delete format[format.length - 1].y;
534
format[format.length - 1].x = true;
538
s.datapoints.format = format;
541
if (s.datapoints.pointsize != null)
542
continue; // already filled in
544
s.datapoints.pointsize = format.length;
546
ps = s.datapoints.pointsize;
547
points = s.datapoints.points;
549
insertSteps = s.lines.show && s.lines.steps;
550
s.xaxis.used = s.yaxis.used = true;
552
for (j = k = 0; j < data.length; ++j, k += ps) {
555
var nullify = p == null;
557
for (m = 0; m < ps; ++m) {
562
if (f.number && val != null) {
563
val = +val; // convert to number
566
else if (val == Infinity)
568
else if (val == -Infinity)
576
if (f.defaultValue != null)
577
val = f.defaultValue;
586
for (m = 0; m < ps; ++m) {
590
// extract min/max info
592
updateAxis(s.xaxis, val, val);
594
updateAxis(s.yaxis, val, val);
596
points[k + m] = null;
600
// a little bit of line specific stuff that
601
// perhaps shouldn't be here, but lacking
603
if (insertSteps && k > 0
604
&& points[k - ps] != null
605
&& points[k - ps] != points[k]
606
&& points[k - ps + 1] != points[k + 1]) {
607
// copy the point to make room for a middle point
608
for (m = 0; m < ps; ++m)
609
points[k + ps + m] = points[k + m];
611
// middle point has same y
612
points[k + 1] = points[k - ps + 1];
614
// we've added a point, better reflect that
621
// give the hooks a chance to run
622
for (i = 0; i < series.length; ++i) {
625
executeHooks(hooks.processDatapoints, [ s, s.datapoints]);
628
// second pass: find datamax/datamin for auto-scaling
629
for (i = 0; i < series.length; ++i) {
631
points = s.datapoints.points,
632
ps = s.datapoints.pointsize;
634
var xmin = topSentry, ymin = topSentry,
635
xmax = bottomSentry, ymax = bottomSentry;
637
for (j = 0; j < points.length; j += ps) {
638
if (points[j] == null)
641
for (m = 0; m < ps; ++m) {
644
if (!f || val == fakeInfinity || val == -fakeInfinity)
663
// make sure we got room for the bar on the dancing floor
664
var delta = s.bars.align == "left" ? 0 : -s.bars.barWidth/2;
665
if (s.bars.horizontal) {
667
ymax += delta + s.bars.barWidth;
671
xmax += delta + s.bars.barWidth;
675
updateAxis(s.xaxis, xmin, xmax);
676
updateAxis(s.yaxis, ymin, ymax);
679
$.each(allAxes(), function (_, axis) {
680
if (axis.datamin == topSentry)
682
if (axis.datamax == bottomSentry)
687
function makeCanvas(skipPositioning, cls) {
688
var c = document.createElement('canvas');
690
c.width = canvasWidth;
691
c.height = canvasHeight;
693
if (!skipPositioning)
694
$(c).css({ position: 'absolute', left: 0, top: 0 });
696
$(c).appendTo(placeholder);
698
if (!c.getContext) // excanvas hack
699
c = window.G_vmlCanvasManager.initElement(c);
701
// used for resetting in case we get replotted
702
c.getContext("2d").save();
707
function getCanvasDimensions() {
708
canvasWidth = placeholder.width();
709
canvasHeight = placeholder.height();
711
if (canvasWidth <= 0 || canvasHeight <= 0)
712
throw "Invalid dimensions for plot, width = " + canvasWidth + ", height = " + canvasHeight;
715
function resizeCanvas(c) {
716
// resizing should reset the state (excanvas seems to be
718
if (c.width != canvasWidth)
719
c.width = canvasWidth;
721
if (c.height != canvasHeight)
722
c.height = canvasHeight;
724
// so try to get back to the initial state (even if it's
725
// gone now, this should be safe according to the spec)
726
var cctx = c.getContext("2d");
733
function setupCanvases() {
735
existingCanvas = placeholder.children("canvas.base"),
736
existingOverlay = placeholder.children("canvas.overlay");
738
if (existingCanvas.length == 0 || existingOverlay == 0) {
741
placeholder.html(""); // make sure placeholder is clear
743
placeholder.css({ padding: 0 }); // padding messes up the positioning
745
if (placeholder.css("position") == 'static')
746
placeholder.css("position", "relative"); // for positioning labels and overlay
748
getCanvasDimensions();
750
canvas = makeCanvas(true, "base");
751
overlay = makeCanvas(false, "overlay"); // overlay canvas for interactive features
756
// reuse existing elements
758
canvas = existingCanvas.get(0);
759
overlay = existingOverlay.get(0);
764
ctx = canvas.getContext("2d");
765
octx = overlay.getContext("2d");
767
// we include the canvas in the event holder too, because IE 7
768
// sometimes has trouble with the stacking order
769
eventHolder = $([overlay, canvas]);
772
// run shutdown in the old plot object
773
placeholder.data("plot").shutdown();
775
// reset reused canvases
778
// make sure overlay pixels are cleared (canvas is cleared when we redraw)
779
octx.clearRect(0, 0, canvasWidth, canvasHeight);
781
// then whack any remaining obvious garbage left
782
eventHolder.unbind();
783
placeholder.children().not([canvas, overlay]).remove();
786
// save in case we get replotted
787
placeholder.data("plot", plot);
790
function bindEvents() {
792
if (options.grid.hoverable) {
793
eventHolder.mousemove(onMouseMove);
794
eventHolder.mouseleave(onMouseLeave);
797
if (options.grid.clickable)
798
eventHolder.click(onClick);
800
executeHooks(hooks.bindEvents, [eventHolder]);
803
function shutdown() {
805
clearTimeout(redrawTimeout);
807
eventHolder.unbind("mousemove", onMouseMove);
808
eventHolder.unbind("mouseleave", onMouseLeave);
809
eventHolder.unbind("click", onClick);
811
executeHooks(hooks.shutdown, [eventHolder]);
814
function setTransformationHelpers(axis) {
815
// set helper functions on the axis, assumes plot area
816
// has been computed already
818
function identity(x) { return x; }
820
var s, m, t = axis.options.transform || identity,
821
it = axis.options.inverseTransform;
823
// precompute how much the axis is scaling a point
825
if (axis.direction == "x") {
826
s = axis.scale = plotWidth / Math.abs(t(axis.max) - t(axis.min));
827
m = Math.min(t(axis.max), t(axis.min));
830
s = axis.scale = plotHeight / Math.abs(t(axis.max) - t(axis.min));
832
m = Math.max(t(axis.max), t(axis.min));
835
// data point to canvas coordinate
836
if (t == identity) // slight optimization
837
axis.p2c = function (p) { return (p - m) * s; };
839
axis.p2c = function (p) { return (t(p) - m) * s; };
840
// canvas coordinate to data point
842
axis.c2p = function (c) { return m + c / s; };
844
axis.c2p = function (c) { return it(m + c / s); };
847
function measureTickLabels(axis) {
848
var opts = axis.options, i, ticks = axis.ticks || [], labels = [],
849
l, w = opts.labelWidth, h = opts.labelHeight, dummyDiv;
851
function makeDummyDiv(labels, width) {
852
return $('<div style="position:absolute;top:-10000px;' + width + 'font-size:smaller">' +
853
'<div class="' + axis.direction + 'Axis ' + axis.direction + axis.n + 'Axis">'
854
+ labels.join("") + '</div></div>')
855
.appendTo(placeholder);
858
if (axis.direction == "x") {
859
// to avoid measuring the widths of the labels (it's slow), we
860
// construct fixed-size boxes and put the labels inside
861
// them, we don't need the exact figures and the
862
// fixed-size box content is easy to center
864
w = Math.floor(canvasWidth / (ticks.length > 0 ? ticks.length : 1));
866
// measure x label heights
869
for (i = 0; i < ticks.length; ++i) {
872
labels.push('<div class="tickLabel" style="float:left;width:' + w + 'px">' + l + '</div>');
875
if (labels.length > 0) {
876
// stick them all in the same div and measure
878
labels.push('<div style="clear:left"></div>');
879
dummyDiv = makeDummyDiv(labels, "width:10000px;");
880
h = dummyDiv.height();
885
else if (w == null || h == null) {
886
// calculate y label dimensions
887
for (i = 0; i < ticks.length; ++i) {
890
labels.push('<div class="tickLabel">' + l + '</div>');
893
if (labels.length > 0) {
894
dummyDiv = makeDummyDiv(labels, "");
896
w = dummyDiv.children().width();
898
h = dummyDiv.find("div.tickLabel").height();
909
axis.labelHeight = h;
912
function allocateAxisBoxFirstPhase(axis) {
913
// find the bounding box of the axis by looking at label
914
// widths/heights and ticks, make room by diminishing the
917
var lw = axis.labelWidth,
918
lh = axis.labelHeight,
919
pos = axis.options.position,
920
tickLength = axis.options.tickLength,
921
axismargin = options.grid.axisMargin,
922
padding = options.grid.labelMargin,
923
all = axis.direction == "x" ? xaxes : yaxes,
926
// determine axis margin
927
var samePosition = $.grep(all, function (a) {
928
return a && a.options.position == pos && a.reserveSpace;
930
if ($.inArray(axis, samePosition) == samePosition.length - 1)
931
axismargin = 0; // outermost
933
// determine tick length - if we're innermost, we can use "full"
934
if (tickLength == null)
937
var sameDirection = $.grep(all, function (a) {
938
return a && a.reserveSpace;
941
var innermost = $.inArray(axis, sameDirection) == 0;
942
if (!innermost && tickLength == "full")
945
if (!isNaN(+tickLength))
946
padding += +tickLength;
949
if (axis.direction == "x") {
952
if (pos == "bottom") {
953
plotOffset.bottom += lh + axismargin;
954
axis.box = { top: canvasHeight - plotOffset.bottom, height: lh };
957
axis.box = { top: plotOffset.top + axismargin, height: lh };
958
plotOffset.top += lh + axismargin;
965
axis.box = { left: plotOffset.left + axismargin, width: lw };
966
plotOffset.left += lw + axismargin;
969
plotOffset.right += lw + axismargin;
970
axis.box = { left: canvasWidth - plotOffset.right, width: lw };
974
// save for future reference
976
axis.tickLength = tickLength;
977
axis.box.padding = padding;
978
axis.innermost = innermost;
981
function allocateAxisBoxSecondPhase(axis) {
982
// set remaining bounding box coordinates
983
if (axis.direction == "x") {
984
axis.box.left = plotOffset.left;
985
axis.box.width = plotWidth;
988
axis.box.top = plotOffset.top;
989
axis.box.height = plotHeight;
993
function setupGrid() {
994
var i, axes = allAxes();
996
// first calculate the plot and axis box dimensions
998
$.each(axes, function (_, axis) {
999
axis.show = axis.options.show;
1000
if (axis.show == null)
1001
axis.show = axis.used; // by default an axis is visible if it's got data
1003
axis.reserveSpace = axis.show || axis.options.reserveSpace;
1008
allocatedAxes = $.grep(axes, function (axis) { return axis.reserveSpace; });
1010
plotOffset.left = plotOffset.right = plotOffset.top = plotOffset.bottom = 0;
1011
if (options.grid.show) {
1012
$.each(allocatedAxes, function (_, axis) {
1014
setupTickGeneration(axis);
1016
snapRangeToTicks(axis, axis.ticks);
1018
// find labelWidth/Height for axis
1019
measureTickLabels(axis);
1022
// with all dimensions in house, we can compute the
1023
// axis boxes, start from the outside (reverse order)
1024
for (i = allocatedAxes.length - 1; i >= 0; --i)
1025
allocateAxisBoxFirstPhase(allocatedAxes[i]);
1027
// make sure we've got enough space for things that
1029
var minMargin = options.grid.minBorderMargin;
1030
if (minMargin == null) {
1032
for (i = 0; i < series.length; ++i)
1033
minMargin = Math.max(minMargin, series[i].points.radius + series[i].points.lineWidth/2);
1036
for (var a in plotOffset) {
1037
plotOffset[a] += options.grid.borderWidth;
1038
plotOffset[a] = Math.max(minMargin, plotOffset[a]);
1042
plotWidth = canvasWidth - plotOffset.left - plotOffset.right;
1043
plotHeight = canvasHeight - plotOffset.bottom - plotOffset.top;
1045
// now we got the proper plotWidth/Height, we can compute the scaling
1046
$.each(axes, function (_, axis) {
1047
setTransformationHelpers(axis);
1050
if (options.grid.show) {
1051
$.each(allocatedAxes, function (_, axis) {
1052
allocateAxisBoxSecondPhase(axis);
1061
function setRange(axis) {
1062
var opts = axis.options,
1063
min = +(opts.min != null ? opts.min : axis.datamin),
1064
max = +(opts.max != null ? opts.max : axis.datamax),
1069
var widen = max == 0 ? 1 : 0.01;
1071
if (opts.min == null)
1073
// always widen max if we couldn't widen min to ensure we
1074
// don't fall into min == max which doesn't work
1075
if (opts.max == null || opts.min != null)
1079
// consider autoscaling
1080
var margin = opts.autoscaleMargin;
1081
if (margin != null) {
1082
if (opts.min == null) {
1083
min -= delta * margin;
1084
// make sure we don't go below zero if all values
1086
if (min < 0 && axis.datamin != null && axis.datamin >= 0)
1089
if (opts.max == null) {
1090
max += delta * margin;
1091
if (max > 0 && axis.datamax != null && axis.datamax <= 0)
1100
function setupTickGeneration(axis) {
1101
var opts = axis.options;
1103
// estimate number of ticks
1105
if (typeof opts.ticks == "number" && opts.ticks > 0)
1106
noTicks = opts.ticks;
1108
// heuristic based on the model a*sqrt(x) fitted to
1109
// some data points that seemed reasonable
1110
noTicks = 0.3 * Math.sqrt(axis.direction == "x" ? canvasWidth : canvasHeight);
1112
var delta = (axis.max - axis.min) / noTicks,
1113
size, generator, unit, formatter, i, magn, norm;
1115
if (opts.mode == "time") {
1116
// pretty handling of time
1118
// map of app. size of time units in milliseconds
1119
var timeUnitSize = {
1121
"minute": 60 * 1000,
1122
"hour": 60 * 60 * 1000,
1123
"day": 24 * 60 * 60 * 1000,
1124
"month": 30 * 24 * 60 * 60 * 1000,
1125
"year": 365.2425 * 24 * 60 * 60 * 1000
1129
// the allowed tick sizes, after 1 year we use
1130
// an integer algorithm
1132
[1, "second"], [2, "second"], [5, "second"], [10, "second"],
1134
[1, "minute"], [2, "minute"], [5, "minute"], [10, "minute"],
1136
[1, "hour"], [2, "hour"], [4, "hour"],
1137
[8, "hour"], [12, "hour"],
1138
[1, "day"], [2, "day"], [3, "day"],
1139
[0.25, "month"], [0.5, "month"], [1, "month"],
1140
[2, "month"], [3, "month"], [6, "month"],
1145
if (opts.minTickSize != null) {
1146
if (typeof opts.tickSize == "number")
1147
minSize = opts.tickSize;
1149
minSize = opts.minTickSize[0] * timeUnitSize[opts.minTickSize[1]];
1152
for (var i = 0; i < spec.length - 1; ++i)
1153
if (delta < (spec[i][0] * timeUnitSize[spec[i][1]]
1154
+ spec[i + 1][0] * timeUnitSize[spec[i + 1][1]]) / 2
1155
&& spec[i][0] * timeUnitSize[spec[i][1]] >= minSize)
1160
// special-case the possibility of several years
1161
if (unit == "year") {
1162
magn = Math.pow(10, Math.floor(Math.log(delta / timeUnitSize.year) / Math.LN10));
1163
norm = (delta / timeUnitSize.year) / magn;
1168
else if (norm < 7.5)
1176
axis.tickSize = opts.tickSize || [size, unit];
1178
generator = function(axis) {
1180
tickSize = axis.tickSize[0], unit = axis.tickSize[1],
1181
d = new Date(axis.min);
1183
var step = tickSize * timeUnitSize[unit];
1185
if (unit == "second")
1186
d.setUTCSeconds(floorInBase(d.getUTCSeconds(), tickSize));
1187
if (unit == "minute")
1188
d.setUTCMinutes(floorInBase(d.getUTCMinutes(), tickSize));
1190
d.setUTCHours(floorInBase(d.getUTCHours(), tickSize));
1191
if (unit == "month")
1192
d.setUTCMonth(floorInBase(d.getUTCMonth(), tickSize));
1194
d.setUTCFullYear(floorInBase(d.getUTCFullYear(), tickSize));
1196
// reset smaller components
1197
d.setUTCMilliseconds(0);
1198
if (step >= timeUnitSize.minute)
1200
if (step >= timeUnitSize.hour)
1202
if (step >= timeUnitSize.day)
1204
if (step >= timeUnitSize.day * 4)
1206
if (step >= timeUnitSize.year)
1210
var carry = 0, v = Number.NaN, prev;
1215
if (unit == "month") {
1217
// a bit complicated - we'll divide the month
1218
// up but we need to take care of fractions
1219
// so we don't end up in the middle of a day
1221
var start = d.getTime();
1222
d.setUTCMonth(d.getUTCMonth() + 1);
1223
var end = d.getTime();
1224
d.setTime(v + carry * timeUnitSize.hour + (end - start) * tickSize);
1225
carry = d.getUTCHours();
1229
d.setUTCMonth(d.getUTCMonth() + tickSize);
1231
else if (unit == "year") {
1232
d.setUTCFullYear(d.getUTCFullYear() + tickSize);
1235
d.setTime(v + step);
1236
} while (v < axis.max && v != prev);
1241
formatter = function (v, axis) {
1242
var d = new Date(v);
1244
// first check global format
1245
if (opts.timeformat != null)
1246
return $.plot.formatDate(d, opts.timeformat, opts.monthNames);
1248
var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]];
1249
var span = axis.max - axis.min;
1250
var suffix = (opts.twelveHourClock) ? " %p" : "";
1252
if (t < timeUnitSize.minute)
1253
fmt = "%h:%M:%S" + suffix;
1254
else if (t < timeUnitSize.day) {
1255
if (span < 2 * timeUnitSize.day)
1256
fmt = "%h:%M" + suffix;
1258
fmt = "%b %d %h:%M" + suffix;
1260
else if (t < timeUnitSize.month)
1262
else if (t < timeUnitSize.year) {
1263
if (span < timeUnitSize.year)
1271
return $.plot.formatDate(d, fmt, opts.monthNames);
1275
// pretty rounding of base-10 numbers
1276
var maxDec = opts.tickDecimals;
1277
var dec = -Math.floor(Math.log(delta) / Math.LN10);
1278
if (maxDec != null && dec > maxDec)
1281
magn = Math.pow(10, -dec);
1282
norm = delta / magn; // norm is between 1.0 and 10.0
1286
else if (norm < 3) {
1288
// special case for 2.5, requires an extra decimal
1289
if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
1294
else if (norm < 7.5)
1301
if (opts.minTickSize != null && size < opts.minTickSize)
1302
size = opts.minTickSize;
1304
axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec);
1305
axis.tickSize = opts.tickSize || size;
1307
generator = function (axis) {
1310
// spew out all possible ticks
1311
var start = floorInBase(axis.min, axis.tickSize),
1312
i = 0, v = Number.NaN, prev;
1315
v = start + i * axis.tickSize;
1318
} while (v < axis.max && v != prev);
1322
formatter = function (v, axis) {
1323
return v.toFixed(axis.tickDecimals);
1327
if (opts.alignTicksWithAxis != null) {
1328
var otherAxis = (axis.direction == "x" ? xaxes : yaxes)[opts.alignTicksWithAxis - 1];
1329
if (otherAxis && otherAxis.used && otherAxis != axis) {
1330
// consider snapping min/max to outermost nice ticks
1331
var niceTicks = generator(axis);
1332
if (niceTicks.length > 0) {
1333
if (opts.min == null)
1334
axis.min = Math.min(axis.min, niceTicks[0]);
1335
if (opts.max == null && niceTicks.length > 1)
1336
axis.max = Math.max(axis.max, niceTicks[niceTicks.length - 1]);
1339
generator = function (axis) {
1340
// copy ticks, scaled to this axis
1341
var ticks = [], v, i;
1342
for (i = 0; i < otherAxis.ticks.length; ++i) {
1343
v = (otherAxis.ticks[i].v - otherAxis.min) / (otherAxis.max - otherAxis.min);
1344
v = axis.min + v * (axis.max - axis.min);
1350
// we might need an extra decimal since forced
1351
// ticks don't necessarily fit naturally
1352
if (axis.mode != "time" && opts.tickDecimals == null) {
1353
var extraDec = Math.max(0, -Math.floor(Math.log(delta) / Math.LN10) + 1),
1354
ts = generator(axis);
1356
// only proceed if the tick interval rounded
1357
// with an extra decimal doesn't give us a
1359
if (!(ts.length > 1 && /\..*0$/.test((ts[1] - ts[0]).toFixed(extraDec))))
1360
axis.tickDecimals = extraDec;
1365
axis.tickGenerator = generator;
1366
if ($.isFunction(opts.tickFormatter))
1367
axis.tickFormatter = function (v, axis) { return "" + opts.tickFormatter(v, axis); };
1369
axis.tickFormatter = formatter;
1372
function setTicks(axis) {
1373
var oticks = axis.options.ticks, ticks = [];
1374
if (oticks == null || (typeof oticks == "number" && oticks > 0))
1375
ticks = axis.tickGenerator(axis);
1377
if ($.isFunction(oticks))
1378
// generate the ticks
1379
ticks = oticks({ min: axis.min, max: axis.max });
1384
// clean up/labelify the supplied ticks, copy them over
1387
for (i = 0; i < ticks.length; ++i) {
1390
if (typeof t == "object") {
1398
label = axis.tickFormatter(v, axis);
1400
axis.ticks.push({ v: v, label: label });
1404
function snapRangeToTicks(axis, ticks) {
1405
if (axis.options.autoscaleMargin && ticks.length > 0) {
1407
if (axis.options.min == null)
1408
axis.min = Math.min(axis.min, ticks[0].v);
1409
if (axis.options.max == null && ticks.length > 1)
1410
axis.max = Math.max(axis.max, ticks[ticks.length - 1].v);
1415
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
1417
var grid = options.grid;
1419
// draw background, if any
1420
if (grid.show && grid.backgroundColor)
1423
if (grid.show && !grid.aboveData)
1426
for (var i = 0; i < series.length; ++i) {
1427
executeHooks(hooks.drawSeries, [ctx, series[i]]);
1428
drawSeries(series[i]);
1431
executeHooks(hooks.draw, [ctx]);
1433
if (grid.show && grid.aboveData)
1437
function extractRange(ranges, coord) {
1438
var axis, from, to, key, axes = allAxes();
1440
for (i = 0; i < axes.length; ++i) {
1442
if (axis.direction == coord) {
1443
key = coord + axis.n + "axis";
1444
if (!ranges[key] && axis.n == 1)
1445
key = coord + "axis"; // support x1axis as xaxis
1447
from = ranges[key].from;
1448
to = ranges[key].to;
1454
// backwards-compat stuff - to be removed in future
1456
axis = coord == "x" ? xaxes[0] : yaxes[0];
1457
from = ranges[coord + "1"];
1458
to = ranges[coord + "2"];
1461
// auto-reverse as an added bonus
1462
if (from != null && to != null && from > to) {
1468
return { from: from, to: to, axis: axis };
1471
function drawBackground() {
1473
ctx.translate(plotOffset.left, plotOffset.top);
1475
ctx.fillStyle = getColorOrGradient(options.grid.backgroundColor, plotHeight, 0, "rgba(255, 255, 255, 0)");
1476
ctx.fillRect(0, 0, plotWidth, plotHeight);
1480
function drawGrid() {
1484
ctx.translate(plotOffset.left, plotOffset.top);
1487
var markings = options.grid.markings;
1489
if ($.isFunction(markings)) {
1490
var axes = plot.getAxes();
1491
// xmin etc. is backwards compatibility, to be
1492
// removed in the future
1493
axes.xmin = axes.xaxis.min;
1494
axes.xmax = axes.xaxis.max;
1495
axes.ymin = axes.yaxis.min;
1496
axes.ymax = axes.yaxis.max;
1498
markings = markings(axes);
1501
for (i = 0; i < markings.length; ++i) {
1502
var m = markings[i],
1503
xrange = extractRange(m, "x"),
1504
yrange = extractRange(m, "y");
1507
if (xrange.from == null)
1508
xrange.from = xrange.axis.min;
1509
if (xrange.to == null)
1510
xrange.to = xrange.axis.max;
1511
if (yrange.from == null)
1512
yrange.from = yrange.axis.min;
1513
if (yrange.to == null)
1514
yrange.to = yrange.axis.max;
1517
if (xrange.to < xrange.axis.min || xrange.from > xrange.axis.max ||
1518
yrange.to < yrange.axis.min || yrange.from > yrange.axis.max)
1521
xrange.from = Math.max(xrange.from, xrange.axis.min);
1522
xrange.to = Math.min(xrange.to, xrange.axis.max);
1523
yrange.from = Math.max(yrange.from, yrange.axis.min);
1524
yrange.to = Math.min(yrange.to, yrange.axis.max);
1526
if (xrange.from == xrange.to && yrange.from == yrange.to)
1530
xrange.from = xrange.axis.p2c(xrange.from);
1531
xrange.to = xrange.axis.p2c(xrange.to);
1532
yrange.from = yrange.axis.p2c(yrange.from);
1533
yrange.to = yrange.axis.p2c(yrange.to);
1535
if (xrange.from == xrange.to || yrange.from == yrange.to) {
1538
ctx.strokeStyle = m.color || options.grid.markingsColor;
1539
ctx.lineWidth = m.lineWidth || options.grid.markingsLineWidth;
1540
ctx.moveTo(xrange.from, yrange.from);
1541
ctx.lineTo(xrange.to, yrange.to);
1546
ctx.fillStyle = m.color || options.grid.markingsColor;
1547
ctx.fillRect(xrange.from, yrange.to,
1548
xrange.to - xrange.from,
1549
yrange.from - yrange.to);
1555
var axes = allAxes(), bw = options.grid.borderWidth;
1557
for (var j = 0; j < axes.length; ++j) {
1558
var axis = axes[j], box = axis.box,
1559
t = axis.tickLength, x, y, xoff, yoff;
1560
if (!axis.show || axis.ticks.length == 0)
1563
ctx.strokeStyle = axis.options.tickColor || $.color.parse(axis.options.color).scale('a', 0.22).toString();
1567
if (axis.direction == "x") {
1570
y = (axis.position == "top" ? 0 : plotHeight);
1572
y = box.top - plotOffset.top + (axis.position == "top" ? box.height : 0);
1577
x = (axis.position == "left" ? 0 : plotWidth);
1579
x = box.left - plotOffset.left + (axis.position == "left" ? box.width : 0);
1583
if (!axis.innermost) {
1586
if (axis.direction == "x")
1591
if (ctx.lineWidth == 1) {
1592
x = Math.floor(x) + 0.5;
1593
y = Math.floor(y) + 0.5;
1597
ctx.lineTo(x + xoff, y + yoff);
1603
for (i = 0; i < axis.ticks.length; ++i) {
1604
var v = axis.ticks[i].v;
1608
if (v < axis.min || v > axis.max
1609
// skip those lying on the axes if we got a border
1610
|| (t == "full" && bw > 0
1611
&& (v == axis.min || v == axis.max)))
1614
if (axis.direction == "x") {
1616
yoff = t == "full" ? -plotHeight : t;
1618
if (axis.position == "top")
1623
xoff = t == "full" ? -plotWidth : t;
1625
if (axis.position == "left")
1629
if (ctx.lineWidth == 1) {
1630
if (axis.direction == "x")
1631
x = Math.floor(x) + 0.5;
1633
y = Math.floor(y) + 0.5;
1637
ctx.lineTo(x + xoff, y + yoff);
1647
ctx.strokeStyle = options.grid.borderColor;
1648
ctx.strokeRect(-bw/2, -bw/2, plotWidth + bw, plotHeight + bw);
1654
function insertAxisLabels() {
1655
placeholder.find(".tickLabels").remove();
1657
var html = ['<div class="tickLabels" style="font-size:smaller">'];
1659
var axes = allAxes();
1660
for (var j = 0; j < axes.length; ++j) {
1661
var axis = axes[j], box = axis.box;
1664
//debug: html.push('<div style="position:absolute;opacity:0.10;background-color:red;left:' + box.left + 'px;top:' + box.top + 'px;width:' + box.width + 'px;height:' + box.height + 'px"></div>')
1665
html.push('<div class="' + axis.direction + 'Axis ' + axis.direction + axis.n + 'Axis" style="color:' + axis.options.color + '">');
1666
for (var i = 0; i < axis.ticks.length; ++i) {
1667
var tick = axis.ticks[i];
1668
if (!tick.label || tick.v < axis.min || tick.v > axis.max)
1671
var pos = {}, align;
1673
if (axis.direction == "x") {
1675
pos.left = Math.round(plotOffset.left + axis.p2c(tick.v) - axis.labelWidth/2);
1676
if (axis.position == "bottom")
1677
pos.top = box.top + box.padding;
1679
pos.bottom = canvasHeight - (box.top + box.height - box.padding);
1682
pos.top = Math.round(plotOffset.top + axis.p2c(tick.v) - axis.labelHeight/2);
1683
if (axis.position == "left") {
1684
pos.right = canvasWidth - (box.left + box.width - box.padding)
1688
pos.left = box.left + box.padding;
1693
pos.width = axis.labelWidth;
1695
var style = ["position:absolute", "text-align:" + align ];
1697
style.push(a + ":" + pos[a] + "px")
1699
html.push('<div class="tickLabel" style="' + style.join(';') + '">' + tick.label + '</div>');
1701
html.push('</div>');
1704
html.push('</div>');
1706
placeholder.append(html.join(""));
1709
function drawSeries(series) {
1710
if (series.lines.show)
1711
drawSeriesLines(series);
1712
if (series.bars.show)
1713
drawSeriesBars(series);
1714
if (series.points.show)
1715
drawSeriesPoints(series);
1718
function drawSeriesLines(series) {
1719
function plotLine(datapoints, xoffset, yoffset, axisx, axisy) {
1720
var points = datapoints.points,
1721
ps = datapoints.pointsize,
1722
prevx = null, prevy = null;
1725
for (var i = ps; i < points.length; i += ps) {
1726
var x1 = points[i - ps], y1 = points[i - ps + 1],
1727
x2 = points[i], y2 = points[i + 1];
1729
if (x1 == null || x2 == null)
1733
if (y1 <= y2 && y1 < axisy.min) {
1735
continue; // line segment is outside
1736
// compute new intersection point
1737
x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
1740
else if (y2 <= y1 && y2 < axisy.min) {
1743
x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
1748
if (y1 >= y2 && y1 > axisy.max) {
1751
x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
1754
else if (y2 >= y1 && y2 > axisy.max) {
1757
x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
1762
if (x1 <= x2 && x1 < axisx.min) {
1765
y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
1768
else if (x2 <= x1 && x2 < axisx.min) {
1771
y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
1776
if (x1 >= x2 && x1 > axisx.max) {
1779
y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
1782
else if (x2 >= x1 && x2 > axisx.max) {
1785
y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
1789
if (x1 != prevx || y1 != prevy)
1790
ctx.moveTo(axisx.p2c(x1) + xoffset, axisy.p2c(y1) + yoffset);
1794
ctx.lineTo(axisx.p2c(x2) + xoffset, axisy.p2c(y2) + yoffset);
1799
function plotLineArea(datapoints, axisx, axisy) {
1800
var points = datapoints.points,
1801
ps = datapoints.pointsize,
1802
bottom = Math.min(Math.max(0, axisy.min), axisy.max),
1803
i = 0, top, areaOpen = false,
1804
ypos = 1, segmentStart = 0, segmentEnd = 0;
1806
// we process each segment in two turns, first forward
1807
// direction to sketch out top, then once we hit the
1808
// end we go backwards to sketch the bottom
1810
if (ps > 0 && i > points.length + ps)
1813
i += ps; // ps is negative if going backwards
1815
var x1 = points[i - ps],
1816
y1 = points[i - ps + ypos],
1817
x2 = points[i], y2 = points[i + ypos];
1820
if (ps > 0 && x1 != null && x2 == null) {
1828
if (ps < 0 && i == segmentStart + ps) {
1829
// done with the reverse sweep
1834
i = segmentStart = segmentEnd + ps;
1839
if (x1 == null || x2 == null)
1845
if (x1 <= x2 && x1 < axisx.min) {
1848
y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
1851
else if (x2 <= x1 && x2 < axisx.min) {
1854
y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
1859
if (x1 >= x2 && x1 > axisx.max) {
1862
y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
1865
else if (x2 >= x1 && x2 > axisx.max) {
1868
y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
1875
ctx.moveTo(axisx.p2c(x1), axisy.p2c(bottom));
1879
// now first check the case where both is outside
1880
if (y1 >= axisy.max && y2 >= axisy.max) {
1881
ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.max));
1882
ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.max));
1885
else if (y1 <= axisy.min && y2 <= axisy.min) {
1886
ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.min));
1887
ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.min));
1891
// else it's a bit more complicated, there might
1892
// be a flat maxed out rectangle first, then a
1893
// triangular cutout or reverse; to find these
1894
// keep track of the current x values
1895
var x1old = x1, x2old = x2;
1897
// clip the y values, without shortcutting, we
1898
// go through all cases in turn
1901
if (y1 <= y2 && y1 < axisy.min && y2 >= axisy.min) {
1902
x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
1905
else if (y2 <= y1 && y2 < axisy.min && y1 >= axisy.min) {
1906
x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
1911
if (y1 >= y2 && y1 > axisy.max && y2 <= axisy.max) {
1912
x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
1915
else if (y2 >= y1 && y2 > axisy.max && y1 <= axisy.max) {
1916
x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
1920
// if the x value was changed we got a rectangle
1923
ctx.lineTo(axisx.p2c(x1old), axisy.p2c(y1));
1924
// it goes to (x1, y1), but we fill that below
1927
// fill triangular section, this sometimes result
1928
// in redundant points if (x1, y1) hasn't changed
1929
// from previous line to, but we just ignore that
1930
ctx.lineTo(axisx.p2c(x1), axisy.p2c(y1));
1931
ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
1933
// fill the other rectangle if it's there
1935
ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
1936
ctx.lineTo(axisx.p2c(x2old), axisy.p2c(y2));
1942
ctx.translate(plotOffset.left, plotOffset.top);
1943
ctx.lineJoin = "round";
1945
var lw = series.lines.lineWidth,
1946
sw = series.shadowSize;
1947
// FIXME: consider another form of shadow when filling is turned on
1948
if (lw > 0 && sw > 0) {
1949
// draw shadow as a thick and thin line with transparency
1951
ctx.strokeStyle = "rgba(0,0,0,0.1)";
1952
// position shadow at angle from the mid of line
1953
var angle = Math.PI/18;
1954
plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/2), Math.cos(angle) * (lw/2 + sw/2), series.xaxis, series.yaxis);
1955
ctx.lineWidth = sw/2;
1956
plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/4), Math.cos(angle) * (lw/2 + sw/4), series.xaxis, series.yaxis);
1960
ctx.strokeStyle = series.color;
1961
var fillStyle = getFillStyle(series.lines, series.color, 0, plotHeight);
1963
ctx.fillStyle = fillStyle;
1964
plotLineArea(series.datapoints, series.xaxis, series.yaxis);
1968
plotLine(series.datapoints, 0, 0, series.xaxis, series.yaxis);
1972
function drawSeriesPoints(series) {
1973
function plotPoints(datapoints, radius, fillStyle, offset, shadow, axisx, axisy, symbol) {
1974
var points = datapoints.points, ps = datapoints.pointsize;
1976
for (var i = 0; i < points.length; i += ps) {
1977
var x = points[i], y = points[i + 1];
1978
if (x == null || x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
1983
y = axisy.p2c(y) + offset;
1984
if (symbol == "circle")
1985
ctx.arc(x, y, radius, 0, shadow ? Math.PI : Math.PI * 2, false);
1987
symbol(ctx, x, y, radius, shadow);
1991
ctx.fillStyle = fillStyle;
1999
ctx.translate(plotOffset.left, plotOffset.top);
2001
var lw = series.points.lineWidth,
2002
sw = series.shadowSize,
2003
radius = series.points.radius,
2004
symbol = series.points.symbol;
2005
if (lw > 0 && sw > 0) {
2006
// draw shadow in two steps
2009
ctx.strokeStyle = "rgba(0,0,0,0.1)";
2010
plotPoints(series.datapoints, radius, null, w + w/2, true,
2011
series.xaxis, series.yaxis, symbol);
2013
ctx.strokeStyle = "rgba(0,0,0,0.2)";
2014
plotPoints(series.datapoints, radius, null, w/2, true,
2015
series.xaxis, series.yaxis, symbol);
2019
ctx.strokeStyle = series.color;
2020
plotPoints(series.datapoints, radius,
2021
getFillStyle(series.points, series.color), 0, false,
2022
series.xaxis, series.yaxis, symbol);
2026
function drawBar(x, y, b, barLeft, barRight, offset, fillStyleCallback, axisx, axisy, c, horizontal, lineWidth) {
2027
var left, right, bottom, top,
2028
drawLeft, drawRight, drawTop, drawBottom,
2031
// in horizontal mode, we start the bar from the left
2032
// instead of from the bottom so it appears to be
2033
// horizontal rather than vertical
2035
drawBottom = drawRight = drawTop = true;
2040
bottom = y + barRight;
2042
// account for negative bars
2052
drawLeft = drawRight = drawTop = true;
2055
right = x + barRight;
2059
// account for negative bars
2070
if (right < axisx.min || left > axisx.max ||
2071
top < axisy.min || bottom > axisy.max)
2074
if (left < axisx.min) {
2079
if (right > axisx.max) {
2084
if (bottom < axisy.min) {
2089
if (top > axisy.max) {
2094
left = axisx.p2c(left);
2095
bottom = axisy.p2c(bottom);
2096
right = axisx.p2c(right);
2097
top = axisy.p2c(top);
2100
if (fillStyleCallback) {
2102
c.moveTo(left, bottom);
2103
c.lineTo(left, top);
2104
c.lineTo(right, top);
2105
c.lineTo(right, bottom);
2106
c.fillStyle = fillStyleCallback(bottom, top);
2111
if (lineWidth > 0 && (drawLeft || drawRight || drawTop || drawBottom)) {
2114
// FIXME: inline moveTo is buggy with excanvas
2115
c.moveTo(left, bottom + offset);
2117
c.lineTo(left, top + offset);
2119
c.moveTo(left, top + offset);
2121
c.lineTo(right, top + offset);
2123
c.moveTo(right, top + offset);
2125
c.lineTo(right, bottom + offset);
2127
c.moveTo(right, bottom + offset);
2129
c.lineTo(left, bottom + offset);
2131
c.moveTo(left, bottom + offset);
2136
function drawSeriesBars(series) {
2137
function plotBars(datapoints, barLeft, barRight, offset, fillStyleCallback, axisx, axisy) {
2138
var points = datapoints.points, ps = datapoints.pointsize;
2140
for (var i = 0; i < points.length; i += ps) {
2141
if (points[i] == null)
2143
drawBar(points[i], points[i + 1], points[i + 2], barLeft, barRight, offset, fillStyleCallback, axisx, axisy, ctx, series.bars.horizontal, series.bars.lineWidth);
2148
ctx.translate(plotOffset.left, plotOffset.top);
2150
// FIXME: figure out a way to add shadows (for instance along the right edge)
2151
ctx.lineWidth = series.bars.lineWidth;
2152
ctx.strokeStyle = series.color;
2153
var barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
2154
var fillStyleCallback = series.bars.fill ? function (bottom, top) { return getFillStyle(series.bars, series.color, bottom, top); } : null;
2155
plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, 0, fillStyleCallback, series.xaxis, series.yaxis);
2159
function getFillStyle(filloptions, seriesColor, bottom, top) {
2160
var fill = filloptions.fill;
2164
if (filloptions.fillColor)
2165
return getColorOrGradient(filloptions.fillColor, bottom, top, seriesColor);
2167
var c = $.color.parse(seriesColor);
2168
c.a = typeof fill == "number" ? fill : 0.1;
2170
return c.toString();
2173
function insertLegend() {
2174
placeholder.find(".legend").remove();
2176
if (!options.legend.show)
2179
var fragments = [], rowStarted = false,
2180
lf = options.legend.labelFormatter, s, label;
2181
for (var i = 0; i < series.length; ++i) {
2187
if (i % options.legend.noColumns == 0) {
2189
fragments.push('</tr>');
2190
fragments.push('<tr>');
2195
label = lf(label, s);
2198
'<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>' +
2199
'<td class="legendLabel">' + label + '</td>');
2202
fragments.push('</tr>');
2204
if (fragments.length == 0)
2207
var table = '<table style="font-size:11px;color:' + options.grid.color + '">' + fragments.join("") + '</table>';
2208
if (options.legend.container != null)
2209
$(options.legend.container).html(table);
2212
p = options.legend.position,
2213
m = options.legend.margin;
2216
if (p.charAt(0) == "n")
2217
pos += 'top:' + (m[1] + plotOffset.top) + 'px;';
2218
else if (p.charAt(0) == "s")
2219
pos += 'bottom:' + (m[1] + plotOffset.bottom) + 'px;';
2220
if (p.charAt(1) == "e")
2221
pos += 'right:' + (m[0] + plotOffset.right) + 'px;';
2222
else if (p.charAt(1) == "w")
2223
pos += 'left:' + (m[0] + plotOffset.left) + 'px;';
2224
var legend = $('<div class="legend">' + table.replace('style="', 'style="position:absolute;' + pos +'margin:-27px 0 0 -5px;') + '</div>').appendTo(placeholder);
2225
if (options.legend.backgroundOpacity != 0.0) {
2226
// put in the transparent background
2227
// separately to avoid blended labels and
2229
var c = options.legend.backgroundColor;
2231
c = options.grid.backgroundColor;
2232
if (c && typeof c == "string")
2233
c = $.color.parse(c);
2235
c = $.color.extract(legend, 'background-color');
2239
var div = legend.children();
2240
$('<div style="position:absolute;width:' + div.width() + 'px;height:' + div.height() + 'px;' + pos +'background-color:' + c + ';"> </div>').prependTo(legend).css('opacity', options.legend.backgroundOpacity);
2246
// interactive features
2248
var highlights = [],
2249
redrawTimeout = null;
2251
// returns the data item the mouse is over, or null if none is found
2252
function findNearbyItem(mouseX, mouseY, seriesFilter) {
2253
var maxDistance = options.grid.mouseActiveRadius,
2254
smallestDistance = maxDistance * maxDistance + 1,
2255
item = null, foundPoint = false, i, j;
2257
for (i = series.length - 1; i >= 0; --i) {
2258
if (!seriesFilter(series[i]))
2264
points = s.datapoints.points,
2265
ps = s.datapoints.pointsize,
2266
mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster
2267
my = axisy.c2p(mouseY),
2268
maxx = maxDistance / axisx.scale,
2269
maxy = maxDistance / axisy.scale;
2271
// with inverse transforms, we can't use the maxx/maxy
2272
// optimization, sadly
2273
if (axisx.options.inverseTransform)
2274
maxx = Number.MAX_VALUE;
2275
if (axisy.options.inverseTransform)
2276
maxy = Number.MAX_VALUE;
2278
if (s.lines.show || s.points.show) {
2279
for (j = 0; j < points.length; j += ps) {
2280
var x = points[j], y = points[j + 1];
2284
// For points and lines, the cursor must be within a
2285
// certain distance to the data point
2286
if (x - mx > maxx || x - mx < -maxx ||
2287
y - my > maxy || y - my < -maxy)
2290
// We have to calculate distances in pixels, not in
2291
// data units, because the scales of the axes may be different
2292
var dx = Math.abs(axisx.p2c(x) - mouseX),
2293
dy = Math.abs(axisy.p2c(y) - mouseY),
2294
dist = dx * dx + dy * dy; // we save the sqrt
2296
// use <= to ensure last point takes precedence
2297
// (last generally means on top of)
2298
if (dist < smallestDistance) {
2299
smallestDistance = dist;
2305
if (s.bars.show && !item) { // no other point can be nearby
2306
var barLeft = s.bars.align == "left" ? 0 : -s.bars.barWidth/2,
2307
barRight = barLeft + s.bars.barWidth;
2309
for (j = 0; j < points.length; j += ps) {
2310
var x = points[j], y = points[j + 1], b = points[j + 2];
2314
// for a bar graph, the cursor must be inside the bar
2315
if (series[i].bars.horizontal ?
2316
(mx <= Math.max(b, x) && mx >= Math.min(b, x) &&
2317
my >= y + barLeft && my <= y + barRight) :
2318
(mx >= x + barLeft && mx <= x + barRight &&
2319
my >= Math.min(b, y) && my <= Math.max(b, y)))
2328
ps = series[i].datapoints.pointsize;
2330
return { datapoint: series[i].datapoints.points.slice(j * ps, (j + 1) * ps),
2339
function onMouseMove(e) {
2340
if (options.grid.hoverable)
2341
triggerClickHoverEvent("plothover", e,
2342
function (s) { return s["hoverable"] != false; });
2345
function onMouseLeave(e) {
2346
if (options.grid.hoverable)
2347
triggerClickHoverEvent("plothover", e,
2348
function (s) { return false; });
2351
function onClick(e) {
2352
triggerClickHoverEvent("plotclick", e,
2353
function (s) { return s["clickable"] != false; });
2356
// trigger click or hover event (they send the same parameters
2357
// so we share their code)
2358
function triggerClickHoverEvent(eventname, event, seriesFilter) {
2359
var offset = eventHolder.offset(),
2360
canvasX = event.pageX - offset.left - plotOffset.left,
2361
canvasY = event.pageY - offset.top - plotOffset.top,
2362
pos = canvasToAxisCoords({ left: canvasX, top: canvasY });
2364
pos.pageX = event.pageX;
2365
pos.pageY = event.pageY;
2367
var item = findNearbyItem(canvasX, canvasY, seriesFilter);
2370
// fill in mouse pos for any listeners out there
2371
item.pageX = parseInt(item.series.xaxis.p2c(item.datapoint[0]) + offset.left + plotOffset.left);
2372
item.pageY = parseInt(item.series.yaxis.p2c(item.datapoint[1]) + offset.top + plotOffset.top);
2375
if (options.grid.autoHighlight) {
2376
// clear auto-highlights
2377
for (var i = 0; i < highlights.length; ++i) {
2378
var h = highlights[i];
2379
if (h.auto == eventname &&
2380
!(item && h.series == item.series &&
2381
h.point[0] == item.datapoint[0] &&
2382
h.point[1] == item.datapoint[1]))
2383
unhighlight(h.series, h.point);
2387
highlight(item.series, item.datapoint, eventname);
2390
placeholder.trigger(eventname, [ pos, item ]);
2393
function triggerRedrawOverlay() {
2395
redrawTimeout = setTimeout(drawOverlay, 30);
2398
function drawOverlay() {
2399
redrawTimeout = null;
2403
octx.clearRect(0, 0, canvasWidth, canvasHeight);
2404
octx.translate(plotOffset.left, plotOffset.top);
2407
for (i = 0; i < highlights.length; ++i) {
2410
if (hi.series.bars.show)
2411
drawBarHighlight(hi.series, hi.point);
2413
drawPointHighlight(hi.series, hi.point);
2417
executeHooks(hooks.drawOverlay, [octx]);
2420
function highlight(s, point, auto) {
2421
if (typeof s == "number")
2424
if (typeof point == "number") {
2425
var ps = s.datapoints.pointsize;
2426
point = s.datapoints.points.slice(ps * point, ps * (point + 1));
2429
var i = indexOfHighlight(s, point);
2431
highlights.push({ series: s, point: point, auto: auto });
2433
triggerRedrawOverlay();
2436
highlights[i].auto = false;
2439
function unhighlight(s, point) {
2440
if (s == null && point == null) {
2442
triggerRedrawOverlay();
2445
if (typeof s == "number")
2448
if (typeof point == "number")
2449
point = s.data[point];
2451
var i = indexOfHighlight(s, point);
2453
highlights.splice(i, 1);
2455
triggerRedrawOverlay();
2459
function indexOfHighlight(s, p) {
2460
for (var i = 0; i < highlights.length; ++i) {
2461
var h = highlights[i];
2462
if (h.series == s && h.point[0] == p[0]
2463
&& h.point[1] == p[1])
2469
function drawPointHighlight(series, point) {
2470
var x = point[0], y = point[1],
2471
axisx = series.xaxis, axisy = series.yaxis;
2473
if (x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
2476
var pointRadius = series.points.radius + series.points.lineWidth / 2;
2477
octx.lineWidth = pointRadius;
2478
octx.strokeStyle = $.color.parse(series.color).scale('a', 0.5).toString();
2479
var radius = 1.5 * pointRadius,
2484
if (series.points.symbol == "circle")
2485
octx.arc(x, y, radius, 0, 2 * Math.PI, false);
2487
series.points.symbol(octx, x, y, radius, false);
2492
function drawBarHighlight(series, point) {
2493
octx.lineWidth = series.bars.lineWidth;
2494
octx.strokeStyle = $.color.parse(series.color).scale('a', 0.5).toString();
2495
var fillStyle = $.color.parse(series.color).scale('a', 0.5).toString();
2496
var barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
2497
drawBar(point[0], point[1], point[2] || 0, barLeft, barLeft + series.bars.barWidth,
2498
0, function () { return fillStyle; }, series.xaxis, series.yaxis, octx, series.bars.horizontal, series.bars.lineWidth);
2501
function getColorOrGradient(spec, bottom, top, defaultColor) {
2502
if (typeof spec == "string")
2505
// assume this is a gradient spec; IE currently only
2506
// supports a simple vertical gradient properly, so that's
2507
// what we support too
2508
var gradient = ctx.createLinearGradient(0, top, 0, bottom);
2510
for (var i = 0, l = spec.colors.length; i < l; ++i) {
2511
var c = spec.colors[i];
2512
if (typeof c != "string") {
2513
var co = $.color.parse(defaultColor);
2514
if (c.brightness != null)
2515
co = co.scale('rgb', c.brightness)
2516
if (c.opacity != null)
2520
gradient.addColorStop(i / (l - 1), c);
2528
$.plot = function(placeholder, data, options) {
2529
//var t0 = new Date();
2530
var plot = new Plot($(placeholder), data, options, $.plot.plugins);
2531
//(window.console ? console.log : alert)("time used (msecs): " + ((new Date()).getTime() - t0.getTime()));
2535
$.plot.version = "0.7";
2537
$.plot.plugins = [];
2539
// returns a string with the date d formatted according to fmt
2540
$.plot.formatDate = function(d, fmt, monthNames) {
2541
var leftPad = function(n) {
2543
return n.length == 1 ? "0" + n : n;
2547
var escape = false, padNext = false;
2548
var hours = d.getUTCHours();
2549
var isAM = hours < 12;
2550
if (monthNames == null)
2551
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
2553
if (fmt.search(/%p|%P/) != -1) {
2556
} else if (hours == 0) {
2560
for (var i = 0; i < fmt.length; ++i) {
2561
var c = fmt.charAt(i);
2565
case 'h': c = "" + hours; break;
2566
case 'H': c = leftPad(hours); break;
2567
case 'M': c = leftPad(d.getUTCMinutes()); break;
2568
case 'S': c = leftPad(d.getUTCSeconds()); break;
2569
case 'd': c = "" + d.getUTCDate(); break;
2570
case 'm': c = "" + (d.getUTCMonth() + 1); break;
2571
case 'y': c = "" + d.getUTCFullYear(); break;
2572
case 'b': c = "" + monthNames[d.getUTCMonth()]; break;
2573
case 'p': c = (isAM) ? ("" + "am") : ("" + "pm"); break;
2574
case 'P': c = (isAM) ? ("" + "AM") : ("" + "PM"); break;
2575
case '0': c = ""; padNext = true; break;
2595
// round to nearby lower multiple of base
2596
function floorInBase(n, base) {
2597
return base * Math.floor(n / base);