jqplot.dateAxisRenderer.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: 1.0.2
  6. * Revision: 1108
  7. *
  8. * Copyright (c) 2009-2011 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.
  13. *
  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 .
  17. *
  18. * If you are feeling kind and generous, consider supporting the project by
  19. * making a donation at: http://www.jqplot.com/donate.php .
  20. *
  21. * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
  22. *
  23. * version 2007.04.27
  24. * author 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."
  29. *
  30. */
  31. (function($) {
  32. /**
  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 it's own tick formatter, so the tickOptions.formatter option
  37. * should not be overridden.
  38. *
  39. * Thanks to Ken Synder for his enhanced Date instance methods which are
  40. * included with this code <http://kendsnyder.com/sandbox/date/>.
  41. *
  42. * To use this renderer, include the plugin in your source
  43. * > <script type="text/javascript" language="javascript" src="plugins/jqplot.dateAxisRenderer.js"></script>
  44. *
  45. * and supply the appropriate options to your plot
  46. *
  47. * > {axes:{xaxis:{renderer:$.jqplot.DateAxisRenderer}}}
  48. *
  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'.
  52. *
  53. * Accecptable format codes
  54. * are:
  55. *
  56. * > Code Result Description
  57. * > == Years ==
  58. * > %Y 2008 Four-digit year
  59. * > %y 08 Two-digit year
  60. * > == Months ==
  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
  65. * > == Days ==
  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
  73. * > == Hours ==
  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)
  78. * > %p PM AM or PM
  79. * > == Minutes ==
  80. * > %M 09 Minutes (two digits)
  81. * > %#M 9 Minutes (one or two digits)
  82. * > == Seconds ==
  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)
  89. * > == Timezone ==
  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
  93. * > == Shortcuts ==
  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
  101. * > %R 15:31 %H:%M
  102. * > %r 3:31:00 PM %I:%M:%S %p
  103. * > == Characters ==
  104. * > %n \n Newline
  105. * > %t \t Tab
  106. * > %% % Percent Symbol
  107. */
  108. $.jqplot.DateAxisRenderer = function() {
  109. $.jqplot.LinearAxisRenderer.call(this);
  110. this.date = new $.jsDate();
  111. };
  112. var second = 1000;
  113. var minute = 60 * second;
  114. var hour = 60 * minute;
  115. var day = 24 * hour;
  116. var week = 7 * day;
  117. // these are less definitive
  118. var month = 30.4368499 * day;
  119. var year = 365.242199 * day;
  120. var daysInMonths = [31,28,31,30,31,30,31,30,31,30,31,30];
  121. // array of consistent nice intervals. Longer intervals
  122. // will depend on days in month, days in year, etc.
  123. 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'];
  124. 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];
  125. var niceMonthlyIntervals = [];
  126. function bestDateInterval(min, max, titarget) {
  127. // iterate through niceIntervals to find one closest to titarget
  128. var badness = Number.MAX_VALUE;
  129. var temp, bestTi, bestfmt;
  130. for (var i=0, l=niceIntervals.length; i < l; i++) {
  131. temp = Math.abs(titarget - niceIntervals[i]);
  132. if (temp < badness) {
  133. badness = temp;
  134. bestTi = niceIntervals[i];
  135. bestfmt = niceFormatStrings[i];
  136. }
  137. }
  138. return [bestTi, bestfmt];
  139. }
  140. $.jqplot.DateAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
  141. $.jqplot.DateAxisRenderer.prototype.constructor = $.jqplot.DateAxisRenderer;
  142. $.jqplot.DateTickFormatter = function(format, val) {
  143. if (!format) {
  144. format = '%Y/%m/%d';
  145. }
  146. return $.jsDate.strftime(val, format);
  147. };
  148. $.jqplot.DateAxisRenderer.prototype.init = function(options){
  149. // prop: tickRenderer
  150. // A class of a rendering engine for creating the ticks labels displayed on the plot,
  151. // See <$.jqplot.AxisTickRenderer>.
  152. // this.tickRenderer = $.jqplot.AxisTickRenderer;
  153. // this.labelRenderer = $.jqplot.AxisLabelRenderer;
  154. this.tickOptions.formatter = $.jqplot.DateTickFormatter;
  155. // prop: tickInset
  156. // Controls the amount to inset the first and last ticks from
  157. // the edges of the grid, in multiples of the tick interval.
  158. // 0 is no inset, 0.5 is one half a tick interval, 1 is a full
  159. // tick interval, etc.
  160. this.tickInset = 0;
  161. // prop: drawBaseline
  162. // True to draw the axis baseline.
  163. this.drawBaseline = true;
  164. // prop: baselineWidth
  165. // width of the baseline in pixels.
  166. this.baselineWidth = null;
  167. // prop: baselineColor
  168. // CSS color spec for the baseline.
  169. this.baselineColor = null;
  170. this.daTickInterval = null;
  171. this._daTickInterval = null;
  172. $.extend(true, this, options);
  173. var db = this._dataBounds,
  174. stats,
  175. sum,
  176. s,
  177. d,
  178. pd,
  179. sd,
  180. intv;
  181. // Go through all the series attached to this axis and find
  182. // the min/max bounds for this axis.
  183. for (var i=0; i<this._series.length; i++) {
  184. stats = {intervals:[], frequencies:{}, sortedIntervals:[], min:null, max:null, mean:null};
  185. sum = 0;
  186. s = this._series[i];
  187. d = s.data;
  188. pd = s._plotData;
  189. sd = s._stackData;
  190. intv = 0;
  191. for (var j=0; j<d.length; j++) {
  192. if (this.name == 'xaxis' || this.name == 'x2axis') {
  193. d[j][0] = new $.jsDate(d[j][0]).getTime();
  194. pd[j][0] = new $.jsDate(d[j][0]).getTime();
  195. sd[j][0] = new $.jsDate(d[j][0]).getTime();
  196. if ((d[j][0] != null && d[j][0] < db.min) || db.min == null) {
  197. db.min = d[j][0];
  198. }
  199. if ((d[j][0] != null && d[j][0] > db.max) || db.max == null) {
  200. db.max = d[j][0];
  201. }
  202. if (j>0) {
  203. intv = Math.abs(d[j][0] - d[j-1][0]);
  204. stats.intervals.push(intv);
  205. if (stats.frequencies.hasOwnProperty(intv)) {
  206. stats.frequencies[intv] += 1;
  207. }
  208. else {
  209. stats.frequencies[intv] = 1;
  210. }
  211. }
  212. sum += intv;
  213. }
  214. else {
  215. d[j][1] = new $.jsDate(d[j][1]).getTime();
  216. pd[j][1] = new $.jsDate(d[j][1]).getTime();
  217. sd[j][1] = new $.jsDate(d[j][1]).getTime();
  218. if ((d[j][1] != null && d[j][1] < db.min) || db.min == null) {
  219. db.min = d[j][1];
  220. }
  221. if ((d[j][1] != null && d[j][1] > db.max) || db.max == null) {
  222. db.max = d[j][1];
  223. }
  224. if (j>0) {
  225. intv = Math.abs(d[j][1] - d[j-1][1]);
  226. stats.intervals.push(intv);
  227. if (stats.frequencies.hasOwnProperty(intv)) {
  228. stats.frequencies[intv] += 1;
  229. }
  230. else {
  231. stats.frequencies[intv] = 1;
  232. }
  233. }
  234. }
  235. sum += intv;
  236. }
  237. if (s.renderer.bands) {
  238. if (s.renderer.bands.hiData.length) {
  239. var bd = s.renderer.bands.hiData;
  240. for (var j=0, l=bd.length; j < l; j++) {
  241. if (this.name === 'xaxis' || this.name === 'x2axis') {
  242. bd[j][0] = new $.jsDate(bd[j][0]).getTime();
  243. if ((bd[j][0] != null && bd[j][0] > db.max) || db.max == null) {
  244. db.max = bd[j][0];
  245. }
  246. }
  247. else {
  248. bd[j][1] = new $.jsDate(bd[j][1]).getTime();
  249. if ((bd[j][1] != null && bd[j][1] > db.max) || db.max == null) {
  250. db.max = bd[j][1];
  251. }
  252. }
  253. }
  254. }
  255. if (s.renderer.bands.lowData.length) {
  256. var bd = s.renderer.bands.lowData;
  257. for (var j=0, l=bd.length; j < l; j++) {
  258. if (this.name === 'xaxis' || this.name === 'x2axis') {
  259. bd[j][0] = new $.jsDate(bd[j][0]).getTime();
  260. if ((bd[j][0] != null && bd[j][0] < db.min) || db.min == null) {
  261. db.min = bd[j][0];
  262. }
  263. }
  264. else {
  265. bd[j][1] = new $.jsDate(bd[j][1]).getTime();
  266. if ((bd[j][1] != null && bd[j][1] < db.min) || db.min == null) {
  267. db.min = bd[j][1];
  268. }
  269. }
  270. }
  271. }
  272. }
  273. var tempf = 0,
  274. tempn=0;
  275. for (var n in stats.frequencies) {
  276. stats.sortedIntervals.push({interval:n, frequency:stats.frequencies[n]});
  277. }
  278. stats.sortedIntervals.sort(function(a, b){
  279. return b.frequency - a.frequency;
  280. });
  281. stats.min = $.jqplot.arrayMin(stats.intervals);
  282. stats.max = $.jqplot.arrayMax(stats.intervals);
  283. stats.mean = sum/d.length;
  284. this._intervalStats.push(stats);
  285. stats = sum = s = d = pd = sd = null;
  286. }
  287. db = null;
  288. };
  289. // called with scope of an axis
  290. $.jqplot.DateAxisRenderer.prototype.reset = function() {
  291. this.min = this._options.min;
  292. this.max = this._options.max;
  293. this.tickInterval = this._options.tickInterval;
  294. this.numberTicks = this._options.numberTicks;
  295. this._autoFormatString = '';
  296. if (this._overrideFormatString && this.tickOptions && this.tickOptions.formatString) {
  297. this.tickOptions.formatString = '';
  298. }
  299. this.daTickInterval = this._daTickInterval;
  300. // this._ticks = this.__ticks;
  301. };
  302. $.jqplot.DateAxisRenderer.prototype.createTicks = function(plot) {
  303. // we're are operating on an axis here
  304. var ticks = this._ticks;
  305. var userTicks = this.ticks;
  306. var name = this.name;
  307. // databounds were set on axis initialization.
  308. var db = this._dataBounds;
  309. var iv = this._intervalStats;
  310. var dim = (this.name.charAt(0) === 'x') ? this._plotDimensions.width : this._plotDimensions.height;
  311. var interval;
  312. var min, max;
  313. var pos1, pos2;
  314. var tt, i;
  315. var threshold = 30;
  316. var insetMult = 1;
  317. var tickInterval = this.tickInterval;
  318. // if we already have ticks, use them.
  319. // ticks must be in order of increasing value.
  320. min = ((this.min != null) ? new $.jsDate(this.min).getTime() : db.min);
  321. max = ((this.max != null) ? new $.jsDate(this.max).getTime() : db.max);
  322. // see if we're zooming. if we are, don't use the min and max we're given,
  323. // but compute some nice ones. They will be reset later.
  324. var cursor = plot.plugins.cursor;
  325. if (cursor && cursor._zoom && cursor._zoom.zooming) {
  326. this.min = null;
  327. this.max = null;
  328. }
  329. var range = max - min;
  330. if (this.tickOptions == null || !this.tickOptions.formatString) {
  331. this._overrideFormatString = true;
  332. }
  333. if (userTicks.length) {
  334. // ticks could be 1D or 2D array of [val, val, ,,,] or [[val, label], [val, label], ...] or mixed
  335. for (i=0; i<userTicks.length; i++){
  336. var ut = userTicks[i];
  337. var t = new this.tickRenderer(this.tickOptions);
  338. if (ut.constructor == Array) {
  339. t.value = new $.jsDate(ut[0]).getTime();
  340. t.label = ut[1];
  341. if (!this.showTicks) {
  342. t.showLabel = false;
  343. t.showMark = false;
  344. }
  345. else if (!this.showTickMarks) {
  346. t.showMark = false;
  347. }
  348. t.setTick(t.value, this.name);
  349. this._ticks.push(t);
  350. }
  351. else {
  352. t.value = new $.jsDate(ut).getTime();
  353. if (!this.showTicks) {
  354. t.showLabel = false;
  355. t.showMark = false;
  356. }
  357. else if (!this.showTickMarks) {
  358. t.showMark = false;
  359. }
  360. t.setTick(t.value, this.name);
  361. this._ticks.push(t);
  362. }
  363. }
  364. this.numberTicks = userTicks.length;
  365. this.min = this._ticks[0].value;
  366. this.max = this._ticks[this.numberTicks-1].value;
  367. this.daTickInterval = [(this.max - this.min) / (this.numberTicks - 1)/1000, 'seconds'];
  368. }
  369. ////////
  370. // We don't have any ticks yet, let's make some!
  371. ////////
  372. // special case when there is only one point, make three tick marks to center the point
  373. else if (this.min == null && this.max == null && db.min == db.max)
  374. {
  375. var onePointOpts = $.extend(true, {}, this.tickOptions, {name: this.name, value: null});
  376. var delta = 300000;
  377. this.min = db.min - delta;
  378. this.max = db.max + delta;
  379. this.numberTicks = 3;
  380. for(var i=this.min;i<=this.max;i+= delta)
  381. {
  382. onePointOpts.value = i;
  383. var t = new this.tickRenderer(onePointOpts);
  384. if (this._overrideFormatString && this._autoFormatString != '') {
  385. t.formatString = this._autoFormatString;
  386. }
  387. t.showLabel = false;
  388. t.showMark = false;
  389. this._ticks.push(t);
  390. }
  391. if(this.showTicks) {
  392. this._ticks[1].showLabel = true;
  393. }
  394. if(this.showTickMarks) {
  395. this._ticks[1].showTickMarks = true;
  396. }
  397. }
  398. // if user specified min and max are null, we set those to make best ticks.
  399. else if (this.min == null && this.max == null) {
  400. var opts = $.extend(true, {}, this.tickOptions, {name: this.name, value: null});
  401. // want to find a nice interval
  402. var nttarget,
  403. titarget;
  404. // if no tickInterval or numberTicks options specified, make a good guess.
  405. if (!this.tickInterval && !this.numberTicks) {
  406. var tdim = Math.max(dim, threshold+1);
  407. // how many ticks to put on the axis?
  408. // date labels tend to be long. If ticks not rotated,
  409. // don't use too many and have a high spacing factor.
  410. // If we are rotating ticks, use a lower factor.
  411. var spacingFactor = 115;
  412. if (this.tickRenderer === $.jqplot.CanvasAxisTickRenderer && this.tickOptions.angle) {
  413. spacingFactor = 115 - 40 * Math.abs(Math.sin(this.tickOptions.angle/180*Math.PI));
  414. }
  415. nttarget = Math.ceil((tdim-threshold)/spacingFactor + 1);
  416. titarget = (max - min) / (nttarget - 1);
  417. }
  418. // If tickInterval is specified, we'll try to honor it.
  419. // Not gauranteed to get this interval, but we'll get as close as
  420. // we can.
  421. // tickInterval will be used before numberTicks, that is if
  422. // both are specified, numberTicks will be ignored.
  423. else if (this.tickInterval) {
  424. titarget = this.tickInterval;
  425. }
  426. // if numberTicks specified, try to honor it.
  427. // Not gauranteed, but will try to get close.
  428. else if (this.numberTicks) {
  429. nttarget = this.numberTicks;
  430. titarget = (max - min) / (nttarget - 1);
  431. }
  432. // If we can use an interval of 2 weeks or less, pick best one
  433. if (titarget <= 19*day) {
  434. var ret = bestDateInterval(min, max, titarget);
  435. var tempti = ret[0];
  436. this._autoFormatString = ret[1];
  437. min = Math.floor(min/tempti) * tempti;
  438. min = new $.jsDate(min);
  439. min = min.getTime() + min.getUtcOffset();
  440. nttarget = Math.ceil((max - min) / tempti) + 1;
  441. this.min = min;
  442. this.max = min + (nttarget - 1) * tempti;
  443. // if max is less than max, add an interval
  444. if (this.max < max) {
  445. this.max += tempti;
  446. nttarget += 1;
  447. }
  448. this.tickInterval = tempti;
  449. this.numberTicks = nttarget;
  450. for (var i=0; i<nttarget; i++) {
  451. opts.value = this.min + i * tempti;
  452. t = new this.tickRenderer(opts);
  453. if (this._overrideFormatString && this._autoFormatString != '') {
  454. t.formatString = this._autoFormatString;
  455. }
  456. if (!this.showTicks) {
  457. t.showLabel = false;
  458. t.showMark = false;
  459. }
  460. else if (!this.showTickMarks) {
  461. t.showMark = false;
  462. }
  463. this._ticks.push(t);
  464. }
  465. insetMult = this.tickInterval;
  466. }
  467. // should we use a monthly interval?
  468. else if (titarget <= 9 * month) {
  469. this._autoFormatString = '%v';
  470. // how many months in an interval?
  471. var intv = Math.round(titarget/month);
  472. if (intv < 1) {
  473. intv = 1;
  474. }
  475. else if (intv > 6) {
  476. intv = 6;
  477. }
  478. // figure out the starting month and ending month.
  479. var mstart = new $.jsDate(min).setDate(1).setHours(0,0,0,0);
  480. // See if max ends exactly on a month
  481. var tempmend = new $.jsDate(max);
  482. var mend = new $.jsDate(max).setDate(1).setHours(0,0,0,0);
  483. if (tempmend.getTime() !== mend.getTime()) {
  484. mend = mend.add(1, 'month');
  485. }
  486. var nmonths = mend.diff(mstart, 'month');
  487. nttarget = Math.ceil(nmonths/intv) + 1;
  488. this.min = mstart.getTime();
  489. this.max = mstart.clone().add((nttarget - 1) * intv, 'month').getTime();
  490. this.numberTicks = nttarget;
  491. for (var i=0; i<nttarget; i++) {
  492. if (i === 0) {
  493. opts.value = mstart.getTime();
  494. }
  495. else {
  496. opts.value = mstart.add(intv, 'month').getTime();
  497. }
  498. t = new this.tickRenderer(opts);
  499. if (this._overrideFormatString && this._autoFormatString != '') {
  500. t.formatString = this._autoFormatString;
  501. }
  502. if (!this.showTicks) {
  503. t.showLabel = false;
  504. t.showMark = false;
  505. }
  506. else if (!this.showTickMarks) {
  507. t.showMark = false;
  508. }
  509. this._ticks.push(t);
  510. }
  511. insetMult = intv * month;
  512. }
  513. // use yearly intervals
  514. else {
  515. this._autoFormatString = '%v';
  516. // how many years in an interval?
  517. var intv = Math.round(titarget/year);
  518. if (intv < 1) {
  519. intv = 1;
  520. }
  521. // figure out the starting and ending years.
  522. var mstart = new $.jsDate(min).setMonth(0, 1).setHours(0,0,0,0);
  523. var mend = new $.jsDate(max).add(1, 'year').setMonth(0, 1).setHours(0,0,0,0);
  524. var nyears = mend.diff(mstart, 'year');
  525. nttarget = Math.ceil(nyears/intv) + 1;
  526. this.min = mstart.getTime();
  527. this.max = mstart.clone().add((nttarget - 1) * intv, 'year').getTime();
  528. this.numberTicks = nttarget;
  529. for (var i=0; i<nttarget; i++) {
  530. if (i === 0) {
  531. opts.value = mstart.getTime();
  532. }
  533. else {
  534. opts.value = mstart.add(intv, 'year').getTime();
  535. }
  536. t = new this.tickRenderer(opts);
  537. if (this._overrideFormatString && this._autoFormatString != '') {
  538. t.formatString = this._autoFormatString;
  539. }
  540. if (!this.showTicks) {
  541. t.showLabel = false;
  542. t.showMark = false;
  543. }
  544. else if (!this.showTickMarks) {
  545. t.showMark = false;
  546. }
  547. this._ticks.push(t);
  548. }
  549. insetMult = intv * year;
  550. }
  551. }
  552. ////////
  553. // Some option(s) specified, work around that.
  554. ////////
  555. else {
  556. if (name == 'xaxis' || name == 'x2axis') {
  557. dim = this._plotDimensions.width;
  558. }
  559. else {
  560. dim = this._plotDimensions.height;
  561. }
  562. // if min, max and number of ticks specified, user can't specify interval.
  563. if (this.min != null && this.max != null && this.numberTicks != null) {
  564. this.tickInterval = null;
  565. }
  566. // if user specified a tick interval, convert to usable.
  567. if (this.tickInterval != null)
  568. {
  569. // if interval is a number or can be converted to one, use it.
  570. // Assume it is in SECONDS!!!
  571. if (Number(this.tickInterval)) {
  572. this.daTickInterval = [Number(this.tickInterval), 'seconds'];
  573. }
  574. // else, parse out something we can build from.
  575. else if (typeof this.tickInterval == "string") {
  576. var parts = this.tickInterval.split(' ');
  577. if (parts.length == 1) {
  578. this.daTickInterval = [1, parts[0]];
  579. }
  580. else if (parts.length == 2) {
  581. this.daTickInterval = [parts[0], parts[1]];
  582. }
  583. }
  584. }
  585. // if min and max are same, space them out a bit
  586. if (min == max) {
  587. var adj = 24*60*60*500; // 1/2 day
  588. min -= adj;
  589. max += adj;
  590. }
  591. range = max - min;
  592. var optNumTicks = 2 + parseInt(Math.max(0, dim-100)/100, 10);
  593. var rmin, rmax;
  594. rmin = (this.min != null) ? new $.jsDate(this.min).getTime() : min - range/2*(this.padMin - 1);
  595. rmax = (this.max != null) ? new $.jsDate(this.max).getTime() : max + range/2*(this.padMax - 1);
  596. this.min = rmin;
  597. this.max = rmax;
  598. range = this.max - this.min;
  599. if (this.numberTicks == null){
  600. // if tickInterval is specified by user, we will ignore computed maximum.
  601. // max will be equal or greater to fit even # of ticks.
  602. if (this.daTickInterval != null) {
  603. var nc = new $.jsDate(this.max).diff(this.min, this.daTickInterval[1], true);
  604. this.numberTicks = Math.ceil(nc/this.daTickInterval[0]) +1;
  605. // this.max = new $.jsDate(this.min).add(this.numberTicks-1, this.daTickInterval[1]).getTime();
  606. this.max = new $.jsDate(this.min).add((this.numberTicks-1) * this.daTickInterval[0], this.daTickInterval[1]).getTime();
  607. }
  608. else if (dim > 200) {
  609. this.numberTicks = parseInt(3+(dim-200)/100, 10);
  610. }
  611. else {
  612. this.numberTicks = 2;
  613. }
  614. }
  615. insetMult = range / (this.numberTicks-1)/1000;
  616. if (this.daTickInterval == null) {
  617. this.daTickInterval = [insetMult, 'seconds'];
  618. }
  619. for (var i=0; i<this.numberTicks; i++){
  620. var min = new $.jsDate(this.min);
  621. tt = min.add(i*this.daTickInterval[0], this.daTickInterval[1]).getTime();
  622. var t = new this.tickRenderer(this.tickOptions);
  623. // var t = new $.jqplot.AxisTickRenderer(this.tickOptions);
  624. if (!this.showTicks) {
  625. t.showLabel = false;
  626. t.showMark = false;
  627. }
  628. else if (!this.showTickMarks) {
  629. t.showMark = false;
  630. }
  631. t.setTick(tt, this.name);
  632. this._ticks.push(t);
  633. }
  634. }
  635. if (this.tickInset) {
  636. this.min = this.min - this.tickInset * insetMult;
  637. this.max = this.max + this.tickInset * insetMult;
  638. }
  639. if (this._daTickInterval == null) {
  640. this._daTickInterval = this.daTickInterval;
  641. }
  642. ticks = null;
  643. };
  644. })(jQuery);