3 * Pure JavaScript plotting plugin using jQuery
8 * Copyright (c) 2009-2013 Chris Leonello
9 * jqPlot is currently available for use in all personal or commercial projects
10 * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
11 * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
12 * choose the license that best suits your project and use it accordingly.
14 * Although not required, the author would appreciate an email letting him
15 * know of any substantial use of jqPlot. You can reach the author at:
16 * chris at jqplot dot com or see http://www.jqplot.com/info.php .
18 * If you are feeling kind and generous, consider supporting the project by
19 * making a donation at: http://www.jqplot.com/donate.php .
21 * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
25 * http://hexmen.com/blog/2007/03/printf-sprintf/
26 * http://hexmen.com/js/sprintf.js
27 * The author (Ash Searle) has placed this code in the public domain:
28 * "This code is unrestricted: you are free to use it however you like."
33 * Class: $.jqplot.DateAxisRenderer
34 * A plugin for a jqPlot to render an axis as a series of date values.
35 * This renderer has no options beyond those supplied by the <Axis> class.
36 * It supplies its own tick formatter, so the tickOptions.formatter option
37 * should not be overridden.
39 * Thanks to Ken Synder for his enhanced Date instance methods which are
40 * included with this code <http://kendsnyder.com/sandbox/date/>.
42 * To use this renderer, include the plugin in your source
43 * > <script type="text/javascript" language="javascript" src="plugins/jqplot.dateAxisRenderer.js"></script>
45 * and supply the appropriate options to your plot
47 * > {axes:{xaxis:{renderer:$.jqplot.DateAxisRenderer}}}
49 * Dates can be passed into the axis in almost any recognizable value and
50 * will be parsed. They will be rendered on the axis in the format
51 * specified by tickOptions.formatString. e.g. tickOptions.formatString = '%Y-%m-%d'.
53 * Accecptable format codes
56 * > Code Result Description
58 * > %Y 2008 Four-digit year
59 * > %y 08 Two-digit year
61 * > %m 09 Two-digit month
62 * > %#m 9 One or two-digit month
63 * > %B September Full month name
64 * > %b Sep Abbreviated month name
66 * > %d 05 Two-digit day of month
67 * > %#d 5 One or two-digit day of month
68 * > %e 5 One or two-digit day of month
69 * > %A Sunday Full name of the day of the week
70 * > %a Sun Abbreviated name of the day of the week
71 * > %w 0 Number of the day of the week (0 = Sunday, 6 = Saturday)
72 * > %o th The ordinal suffix string following the day of the month
74 * > %H 23 Hours in 24-hour format (two digits)
75 * > %#H 3 Hours in 24-hour integer format (one or two digits)
76 * > %I 11 Hours in 12-hour format (two digits)
77 * > %#I 3 Hours in 12-hour integer format (one or two digits)
80 * > %M 09 Minutes (two digits)
81 * > %#M 9 Minutes (one or two digits)
83 * > %S 02 Seconds (two digits)
84 * > %#S 2 Seconds (one or two digits)
85 * > %s 1206567625723 Unix timestamp (Seconds past 1970-01-01 00:00:00)
86 * > == Milliseconds ==
87 * > %N 008 Milliseconds (three digits)
88 * > %#N 8 Milliseconds (one to three digits)
90 * > %O 360 difference in minutes between local time and GMT
91 * > %Z Mountain Standard Time Name of timezone as reported by browser
92 * > %G -06:00 Hours and minutes between GMT
94 * > %F 2008-03-26 %Y-%m-%d
95 * > %T 05:06:30 %H:%M:%S
96 * > %X 05:06:30 %H:%M:%S
97 * > %x 03/26/08 %m/%d/%y
98 * > %D 03/26/08 %m/%d/%y
99 * > %#c Wed Mar 26 15:31:00 2008 %a %b %e %H:%M:%S %Y
100 * > %v 3-Sep-2008 %e-%b-%Y
102 * > %r 3:31:00 PM %I:%M:%S %p
106 * > %% % Percent Symbol
108 $.jqplot.DateAxisRenderer = function() {
109 $.jqplot.LinearAxisRenderer.call(this);
110 this.date = new $.jsDate();
114 var minute = 60 * second;
115 var hour = 60 * minute;
119 // these are less definitive
120 var month = 30.4368499 * day;
121 var year = 365.242199 * day;
123 var daysInMonths = [31,28,31,30,31,30,31,30,31,30,31,30];
124 // array of consistent nice intervals. Longer intervals
125 // will depend on days in month, days in year, etc.
126 var niceFormatStrings = ['%M:%S.%#N', '%M:%S.%#N', '%M:%S.%#N', '%M:%S', '%M:%S', '%M:%S', '%M:%S', '%H:%M:%S', '%H:%M:%S', '%H:%M', '%H:%M', '%H:%M', '%H:%M', '%H:%M', '%H:%M', '%a %H:%M', '%a %H:%M', '%b %e %H:%M', '%b %e %H:%M', '%b %e %H:%M', '%b %e %H:%M', '%v', '%v', '%v', '%v', '%v', '%v', '%v'];
127 var niceIntervals = [0.1*second, 0.2*second, 0.5*second, second, 2*second, 5*second, 10*second, 15*second, 30*second, minute, 2*minute, 5*minute, 10*minute, 15*minute, 30*minute, hour, 2*hour, 4*hour, 6*hour, 8*hour, 12*hour, day, 2*day, 3*day, 4*day, 5*day, week, 2*week];
129 var niceMonthlyIntervals = [];
131 function bestDateInterval(min, max, titarget) {
132 // iterate through niceIntervals to find one closest to titarget
133 var badness = Number.MAX_VALUE;
134 var temp, bestTi, bestfmt;
135 for (var i=0, l=niceIntervals.length; i < l; i++) {
136 temp = Math.abs(titarget - niceIntervals[i]);
137 if (temp < badness) {
139 bestTi = niceIntervals[i];
140 bestfmt = niceFormatStrings[i];
144 return [bestTi, bestfmt];
147 $.jqplot.DateAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
148 $.jqplot.DateAxisRenderer.prototype.constructor = $.jqplot.DateAxisRenderer;
150 $.jqplot.DateTickFormatter = function(format, val) {
154 return $.jsDate.strftime(val, format);
157 $.jqplot.DateAxisRenderer.prototype.init = function(options){
158 // prop: tickRenderer
159 // A class of a rendering engine for creating the ticks labels displayed on the plot,
160 // See <$.jqplot.AxisTickRenderer>.
161 // this.tickRenderer = $.jqplot.AxisTickRenderer;
162 // this.labelRenderer = $.jqplot.AxisLabelRenderer;
163 this.tickOptions.formatter = $.jqplot.DateTickFormatter;
165 // Controls the amount to inset the first and last ticks from
166 // the edges of the grid, in multiples of the tick interval.
167 // 0 is no inset, 0.5 is one half a tick interval, 1 is a full
168 // tick interval, etc.
170 // prop: drawBaseline
171 // True to draw the axis baseline.
172 this.drawBaseline = true;
173 // prop: baselineWidth
174 // width of the baseline in pixels.
175 this.baselineWidth = null;
176 // prop: baselineColor
177 // CSS color spec for the baseline.
178 this.baselineColor = null;
179 this.daTickInterval = null;
180 this._daTickInterval = null;
182 $.extend(true, this, options);
184 var db = this._dataBounds,
193 // Go through all the series attached to this axis and find
194 // the min/max bounds for this axis.
195 for (var i=0; i<this._series.length; i++) {
196 stats = {intervals:[], frequencies:{}, sortedIntervals:[], min:null, max:null, mean:null};
204 for (var j=0; j<d.length; j++) {
205 if (this.name == 'xaxis' || this.name == 'x2axis') {
206 d[j][0] = new $.jsDate(d[j][0]).getTime();
207 pd[j][0] = new $.jsDate(d[j][0]).getTime();
208 sd[j][0] = new $.jsDate(d[j][0]).getTime();
209 if ((d[j][0] != null && d[j][0] < db.min) || db.min == null) {
212 if ((d[j][0] != null && d[j][0] > db.max) || db.max == null) {
216 intv = Math.abs(d[j][0] - d[j-1][0]);
217 stats.intervals.push(intv);
218 if (stats.frequencies.hasOwnProperty(intv)) {
219 stats.frequencies[intv] += 1;
222 stats.frequencies[intv] = 1;
229 d[j][1] = new $.jsDate(d[j][1]).getTime();
230 pd[j][1] = new $.jsDate(d[j][1]).getTime();
231 sd[j][1] = new $.jsDate(d[j][1]).getTime();
232 if ((d[j][1] != null && d[j][1] < db.min) || db.min == null) {
235 if ((d[j][1] != null && d[j][1] > db.max) || db.max == null) {
239 intv = Math.abs(d[j][1] - d[j-1][1]);
240 stats.intervals.push(intv);
241 if (stats.frequencies.hasOwnProperty(intv)) {
242 stats.frequencies[intv] += 1;
245 stats.frequencies[intv] = 1;
252 if (s.renderer.bands) {
253 if (s.renderer.bands.hiData.length) {
254 var bd = s.renderer.bands.hiData;
255 for (var j=0, l=bd.length; j < l; j++) {
256 if (this.name === 'xaxis' || this.name === 'x2axis') {
257 bd[j][0] = new $.jsDate(bd[j][0]).getTime();
258 if ((bd[j][0] != null && bd[j][0] > db.max) || db.max == null) {
263 bd[j][1] = new $.jsDate(bd[j][1]).getTime();
264 if ((bd[j][1] != null && bd[j][1] > db.max) || db.max == null) {
270 if (s.renderer.bands.lowData.length) {
271 var bd = s.renderer.bands.lowData;
272 for (var j=0, l=bd.length; j < l; j++) {
273 if (this.name === 'xaxis' || this.name === 'x2axis') {
274 bd[j][0] = new $.jsDate(bd[j][0]).getTime();
275 if ((bd[j][0] != null && bd[j][0] < db.min) || db.min == null) {
280 bd[j][1] = new $.jsDate(bd[j][1]).getTime();
281 if ((bd[j][1] != null && bd[j][1] < db.min) || db.min == null) {
291 for (var n in stats.frequencies) {
292 stats.sortedIntervals.push({interval:n, frequency:stats.frequencies[n]});
294 stats.sortedIntervals.sort(function(a, b){
295 return b.frequency - a.frequency;
298 stats.min = $.jqplot.arrayMin(stats.intervals);
299 stats.max = $.jqplot.arrayMax(stats.intervals);
300 stats.mean = sum/d.length;
301 this._intervalStats.push(stats);
302 stats = sum = s = d = pd = sd = null;
308 // called with scope of an axis
309 $.jqplot.DateAxisRenderer.prototype.reset = function() {
310 this.min = this._options.min;
311 this.max = this._options.max;
312 this.tickInterval = this._options.tickInterval;
313 this.numberTicks = this._options.numberTicks;
314 this._autoFormatString = '';
315 if (this._overrideFormatString && this.tickOptions && this.tickOptions.formatString) {
316 this.tickOptions.formatString = '';
318 this.daTickInterval = this._daTickInterval;
319 // this._ticks = this.__ticks;
322 $.jqplot.DateAxisRenderer.prototype.createTicks = function(plot) {
323 // we're are operating on an axis here
324 var ticks = this._ticks;
325 var userTicks = this.ticks;
326 var name = this.name;
327 // databounds were set on axis initialization.
328 var db = this._dataBounds;
329 var iv = this._intervalStats;
330 var dim = (this.name.charAt(0) === 'x') ? this._plotDimensions.width : this._plotDimensions.height;
337 var daTickInterval = null;
339 // if user specified a tick interval, convert to usable.
340 if (this.tickInterval != null)
342 // if interval is a number or can be converted to one, use it.
343 // Assume it is in SECONDS!!!
344 if (Number(this.tickInterval)) {
345 daTickInterval = [Number(this.tickInterval), 'seconds'];
347 // else, parse out something we can build from.
348 else if (typeof this.tickInterval == "string") {
349 var parts = this.tickInterval.split(' ');
350 if (parts.length == 1) {
351 daTickInterval = [1, parts[0]];
353 else if (parts.length == 2) {
354 daTickInterval = [parts[0], parts[1]];
359 var tickInterval = this.tickInterval;
361 // if we already have ticks, use them.
362 // ticks must be in order of increasing value.
364 min = new $.jsDate((this.min != null) ? this.min : db.min).getTime();
365 max = new $.jsDate((this.max != null) ? this.max : db.max).getTime();
367 // see if we're zooming. if we are, don't use the min and max we're given,
368 // but compute some nice ones. They will be reset later.
370 var cursor = plot.plugins.cursor;
372 if (cursor && cursor._zoom && cursor._zoom.zooming) {
377 var range = max - min;
379 if (this.tickOptions == null || !this.tickOptions.formatString) {
380 this._overrideFormatString = true;
383 if (userTicks.length) {
384 // ticks could be 1D or 2D array of [val, val, ,,,] or [[val, label], [val, label], ...] or mixed
385 for (i=0; i<userTicks.length; i++){
386 var ut = userTicks[i];
387 var t = new this.tickRenderer(this.tickOptions);
388 if (ut.constructor == Array) {
389 t.value = new $.jsDate(ut[0]).getTime();
391 if (!this.showTicks) {
395 else if (!this.showTickMarks) {
398 t.setTick(t.value, this.name);
403 t.value = new $.jsDate(ut).getTime();
404 if (!this.showTicks) {
408 else if (!this.showTickMarks) {
411 t.setTick(t.value, this.name);
415 this.numberTicks = userTicks.length;
416 this.min = this._ticks[0].value;
417 this.max = this._ticks[this.numberTicks-1].value;
418 this.daTickInterval = [(this.max - this.min) / (this.numberTicks - 1)/1000, 'seconds'];
422 // We don't have any ticks yet, let's make some!
425 // special case when there is only one point, make three tick marks to center the point
426 else if (this.min == null && this.max == null && db.min == db.max)
428 var onePointOpts = $.extend(true, {}, this.tickOptions, {name: this.name, value: null});
430 this.min = db.min - delta;
431 this.max = db.max + delta;
432 this.numberTicks = 3;
434 for(var i=this.min;i<=this.max;i+= delta)
436 onePointOpts.value = i;
438 var t = new this.tickRenderer(onePointOpts);
440 if (this._overrideFormatString && this._autoFormatString != '') {
441 t.formatString = this._autoFormatString;
451 this._ticks[1].showLabel = true;
453 if(this.showTickMarks) {
454 this._ticks[1].showTickMarks = true;
457 // if user specified min and max are null, we set those to make best ticks.
458 else if (this.min == null && this.max == null) {
460 var opts = $.extend(true, {}, this.tickOptions, {name: this.name, value: null});
462 // want to find a nice interval
466 // if no tickInterval or numberTicks options specified, make a good guess.
467 if (!this.tickInterval && !this.numberTicks) {
468 var tdim = Math.max(dim, threshold+1);
469 // how many ticks to put on the axis?
470 // date labels tend to be long. If ticks not rotated,
471 // don't use too many and have a high spacing factor.
472 // If we are rotating ticks, use a lower factor.
473 var spacingFactor = 115;
474 if (this.tickRenderer === $.jqplot.CanvasAxisTickRenderer && this.tickOptions.angle) {
475 spacingFactor = 115 - 40 * Math.abs(Math.sin(this.tickOptions.angle/180*Math.PI));
478 nttarget = Math.ceil((tdim-threshold)/spacingFactor + 1);
479 titarget = (max - min) / (nttarget - 1);
482 // If tickInterval is specified, we'll try to honor it.
483 // Not guaranteed to get this interval, but we'll get as close as
485 // tickInterval will be used before numberTicks, that is if
486 // both are specified, numberTicks will be ignored.
487 else if (this.tickInterval) {
488 titarget = new $.jsDate(0).add(daTickInterval[0], daTickInterval[1]).getTime();
491 // if numberTicks specified, try to honor it.
492 // Not guaranteed, but will try to get close.
493 else if (this.numberTicks) {
494 nttarget = this.numberTicks;
495 titarget = (max - min) / (nttarget - 1);
498 // If we can use an interval of 2 weeks or less, pick best one
499 if (titarget <= 19*day) {
500 var ret = bestDateInterval(min, max, titarget);
502 this._autoFormatString = ret[1];
504 min = new $.jsDate(min);
505 min = Math.floor((min.getTime() - min.getUtcOffset())/tempti) * tempti + min.getUtcOffset();
507 nttarget = Math.ceil((max - min) / tempti) + 1;
509 this.max = min + (nttarget - 1) * tempti;
511 // if max is less than max, add an interval
512 if (this.max < max) {
516 this.tickInterval = tempti;
517 this.numberTicks = nttarget;
519 for (var i=0; i<nttarget; i++) {
520 opts.value = this.min + i * tempti;
521 t = new this.tickRenderer(opts);
523 if (this._overrideFormatString && this._autoFormatString != '') {
524 t.formatString = this._autoFormatString;
526 if (!this.showTicks) {
530 else if (!this.showTickMarks) {
536 insetMult = this.tickInterval;
539 // should we use a monthly interval?
540 else if (titarget <= 9 * month) {
542 this._autoFormatString = '%v';
544 // how many months in an interval?
545 var intv = Math.round(titarget/month);
553 // figure out the starting month and ending month.
554 var mstart = new $.jsDate(min).setDate(1).setHours(0,0,0,0);
556 // See if max ends exactly on a month
557 var tempmend = new $.jsDate(max);
558 var mend = new $.jsDate(max).setDate(1).setHours(0,0,0,0);
560 if (tempmend.getTime() !== mend.getTime()) {
561 mend = mend.add(1, 'month');
564 var nmonths = mend.diff(mstart, 'month');
566 nttarget = Math.ceil(nmonths/intv) + 1;
568 this.min = mstart.getTime();
569 this.max = mstart.clone().add((nttarget - 1) * intv, 'month').getTime();
570 this.numberTicks = nttarget;
572 for (var i=0; i<nttarget; i++) {
574 opts.value = mstart.getTime();
577 opts.value = mstart.add(intv, 'month').getTime();
579 t = new this.tickRenderer(opts);
581 if (this._overrideFormatString && this._autoFormatString != '') {
582 t.formatString = this._autoFormatString;
584 if (!this.showTicks) {
588 else if (!this.showTickMarks) {
594 insetMult = intv * month;
597 // use yearly intervals
600 this._autoFormatString = '%v';
602 // how many years in an interval?
603 var intv = Math.round(titarget/year);
608 // figure out the starting and ending years.
609 var mstart = new $.jsDate(min).setMonth(0, 1).setHours(0,0,0,0);
610 var mend = new $.jsDate(max).add(1, 'year').setMonth(0, 1).setHours(0,0,0,0);
612 var nyears = mend.diff(mstart, 'year');
614 nttarget = Math.ceil(nyears/intv) + 1;
616 this.min = mstart.getTime();
617 this.max = mstart.clone().add((nttarget - 1) * intv, 'year').getTime();
618 this.numberTicks = nttarget;
620 for (var i=0; i<nttarget; i++) {
622 opts.value = mstart.getTime();
625 opts.value = mstart.add(intv, 'year').getTime();
627 t = new this.tickRenderer(opts);
629 if (this._overrideFormatString && this._autoFormatString != '') {
630 t.formatString = this._autoFormatString;
632 if (!this.showTicks) {
636 else if (!this.showTickMarks) {
642 insetMult = intv * year;
647 // Some option(s) specified, work around that.
651 if (name == 'xaxis' || name == 'x2axis') {
652 dim = this._plotDimensions.width;
655 dim = this._plotDimensions.height;
658 // if min, max and number of ticks specified, user can't specify interval.
659 if (this.min != null && this.max != null && this.numberTicks != null) {
660 this.tickInterval = null;
663 if (this.tickInterval != null && daTickInterval != null) {
664 this.daTickInterval = daTickInterval;
667 // if min and max are same, space them out a bit
669 var adj = 24*60*60*500; // 1/2 day
676 var optNumTicks = 2 + parseInt(Math.max(0, dim-100)/100, 10);
681 rmin = (this.min != null) ? new $.jsDate(this.min).getTime() : min - range/2*(this.padMin - 1);
682 rmax = (this.max != null) ? new $.jsDate(this.max).getTime() : max + range/2*(this.padMax - 1);
685 range = this.max - this.min;
687 if (this.numberTicks == null){
688 // if tickInterval is specified by user, we will ignore computed maximum.
689 // max will be equal or greater to fit even # of ticks.
690 if (this.daTickInterval != null) {
691 var nc = new $.jsDate(this.max).diff(this.min, this.daTickInterval[1], true);
692 this.numberTicks = Math.ceil(nc/this.daTickInterval[0]) +1;
693 // this.max = new $.jsDate(this.min).add(this.numberTicks-1, this.daTickInterval[1]).getTime();
694 this.max = new $.jsDate(this.min).add((this.numberTicks-1) * this.daTickInterval[0], this.daTickInterval[1]).getTime();
696 else if (dim > 200) {
697 this.numberTicks = parseInt(3+(dim-200)/100, 10);
700 this.numberTicks = 2;
704 insetMult = range / (this.numberTicks-1)/1000;
706 if (this.daTickInterval == null) {
707 this.daTickInterval = [insetMult, 'seconds'];
711 for (var i=0; i<this.numberTicks; i++){
712 var min = new $.jsDate(this.min);
713 tt = min.add(i*this.daTickInterval[0], this.daTickInterval[1]).getTime();
714 var t = new this.tickRenderer(this.tickOptions);
715 // var t = new $.jqplot.AxisTickRenderer(this.tickOptions);
716 if (!this.showTicks) {
720 else if (!this.showTickMarks) {
723 t.setTick(tt, this.name);
728 if (this.tickInset) {
729 this.min = this.min - this.tickInset * insetMult;
730 this.max = this.max + this.tickInset * insetMult;
733 if (this._daTickInterval == null) {
734 this._daTickInterval = this.daTickInterval;