jqplot.dateAxisRenderer.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: @VERSION
  6. * Revision: @REVISION
  7. *
  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.
  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 its 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 daTickInterval = null;
  318. // if user specified a tick interval, convert to usable.
  319. if (this.tickInterval != null)
  320. {
  321. // if interval is a number or can be converted to one, use it.
  322. // Assume it is in SECONDS!!!
  323. if (Number(this.tickInterval)) {
  324. daTickInterval = [Number(this.tickInterval), 'seconds'];
  325. }
  326. // else, parse out something we can build from.
  327. else if (typeof this.tickInterval == "string") {
  328. var parts = this.tickInterval.split(' ');
  329. if (parts.length == 1) {
  330. daTickInterval = [1, parts[0]];
  331. }
  332. else if (parts.length == 2) {
  333. daTickInterval = [parts[0], parts[1]];
  334. }
  335. }
  336. }
  337. var tickInterval = this.tickInterval;
  338. // if we already have ticks, use them.
  339. // ticks must be in order of increasing value.
  340. min = new $.jsDate((this.min != null) ? this.min : db.min).getTime();
  341. max = new $.jsDate((this.max != null) ? this.max : db.max).getTime();
  342. // see if we're zooming. if we are, don't use the min and max we're given,
  343. // but compute some nice ones. They will be reset later.
  344. var cursor = plot.plugins.cursor;
  345. if (cursor && cursor._zoom && cursor._zoom.zooming) {
  346. this.min = null;
  347. this.max = null;
  348. }
  349. var range = max - min;
  350. if (this.tickOptions == null || !this.tickOptions.formatString) {
  351. this._overrideFormatString = true;
  352. }
  353. if (userTicks.length) {
  354. // ticks could be 1D or 2D array of [val, val, ,,,] or [[val, label], [val, label], ...] or mixed
  355. for (i=0; i<userTicks.length; i++){
  356. var ut = userTicks[i];
  357. var t = new this.tickRenderer(this.tickOptions);
  358. if (ut.constructor == Array) {
  359. t.value = new $.jsDate(ut[0]).getTime();
  360. t.label = ut[1];
  361. if (!this.showTicks) {
  362. t.showLabel = false;
  363. t.showMark = false;
  364. }
  365. else if (!this.showTickMarks) {
  366. t.showMark = false;
  367. }
  368. t.setTick(t.value, this.name);
  369. this._ticks.push(t);
  370. }
  371. else {
  372. t.value = new $.jsDate(ut).getTime();
  373. if (!this.showTicks) {
  374. t.showLabel = false;
  375. t.showMark = false;
  376. }
  377. else if (!this.showTickMarks) {
  378. t.showMark = false;
  379. }
  380. t.setTick(t.value, this.name);
  381. this._ticks.push(t);
  382. }
  383. }
  384. this.numberTicks = userTicks.length;
  385. this.min = this._ticks[0].value;
  386. this.max = this._ticks[this.numberTicks-1].value;
  387. this.daTickInterval = [(this.max - this.min) / (this.numberTicks - 1)/1000, 'seconds'];
  388. }
  389. ////////
  390. // We don't have any ticks yet, let's make some!
  391. ////////
  392. // special case when there is only one point, make three tick marks to center the point
  393. else if (this.min == null && this.max == null && db.min == db.max)
  394. {
  395. var onePointOpts = $.extend(true, {}, this.tickOptions, {name: this.name, value: null});
  396. var delta = 300000;
  397. this.min = db.min - delta;
  398. this.max = db.max + delta;
  399. this.numberTicks = 3;
  400. for(var i=this.min;i<=this.max;i+= delta)
  401. {
  402. onePointOpts.value = i;
  403. var t = new this.tickRenderer(onePointOpts);
  404. if (this._overrideFormatString && this._autoFormatString != '') {
  405. t.formatString = this._autoFormatString;
  406. }
  407. t.showLabel = false;
  408. t.showMark = false;
  409. this._ticks.push(t);
  410. }
  411. if(this.showTicks) {
  412. this._ticks[1].showLabel = true;
  413. }
  414. if(this.showTickMarks) {
  415. this._ticks[1].showTickMarks = true;
  416. }
  417. }
  418. // if user specified min and max are null, we set those to make best ticks.
  419. else if (this.min == null && this.max == null) {
  420. var opts = $.extend(true, {}, this.tickOptions, {name: this.name, value: null});
  421. // want to find a nice interval
  422. var nttarget,
  423. titarget;
  424. // if no tickInterval or numberTicks options specified, make a good guess.
  425. if (!this.tickInterval && !this.numberTicks) {
  426. var tdim = Math.max(dim, threshold+1);
  427. // how many ticks to put on the axis?
  428. // date labels tend to be long. If ticks not rotated,
  429. // don't use too many and have a high spacing factor.
  430. // If we are rotating ticks, use a lower factor.
  431. var spacingFactor = 115;
  432. if (this.tickRenderer === $.jqplot.CanvasAxisTickRenderer && this.tickOptions.angle) {
  433. spacingFactor = 115 - 40 * Math.abs(Math.sin(this.tickOptions.angle/180*Math.PI));
  434. }
  435. nttarget = Math.ceil((tdim-threshold)/spacingFactor + 1);
  436. titarget = (max - min) / (nttarget - 1);
  437. }
  438. // If tickInterval is specified, we'll try to honor it.
  439. // Not guaranteed to get this interval, but we'll get as close as
  440. // we can.
  441. // tickInterval will be used before numberTicks, that is if
  442. // both are specified, numberTicks will be ignored.
  443. else if (this.tickInterval) {
  444. titarget = new $.jsDate(0).add(daTickInterval[0], daTickInterval[1]).getTime();
  445. }
  446. // if numberTicks specified, try to honor it.
  447. // Not guaranteed, but will try to get close.
  448. else if (this.numberTicks) {
  449. nttarget = this.numberTicks;
  450. titarget = (max - min) / (nttarget - 1);
  451. }
  452. // If we can use an interval of 2 weeks or less, pick best one
  453. if (titarget <= 19*day) {
  454. var ret = bestDateInterval(min, max, titarget);
  455. var tempti = ret[0];
  456. this._autoFormatString = ret[1];
  457. min = new $.jsDate(min);
  458. min = Math.floor((min.getTime() - min.getUtcOffset())/tempti) * tempti + min.getUtcOffset();
  459. nttarget = Math.ceil((max - min) / tempti) + 1;
  460. this.min = min;
  461. this.max = min + (nttarget - 1) * tempti;
  462. // if max is less than max, add an interval
  463. if (this.max < max) {
  464. this.max += tempti;
  465. nttarget += 1;
  466. }
  467. this.tickInterval = tempti;
  468. this.numberTicks = nttarget;
  469. for (var i=0; i<nttarget; i++) {
  470. opts.value = this.min + i * tempti;
  471. t = new this.tickRenderer(opts);
  472. if (this._overrideFormatString && this._autoFormatString != '') {
  473. t.formatString = this._autoFormatString;
  474. }
  475. if (!this.showTicks) {
  476. t.showLabel = false;
  477. t.showMark = false;
  478. }
  479. else if (!this.showTickMarks) {
  480. t.showMark = false;
  481. }
  482. this._ticks.push(t);
  483. }
  484. insetMult = this.tickInterval;
  485. }
  486. // should we use a monthly interval?
  487. else if (titarget <= 9 * month) {
  488. this._autoFormatString = '%v';
  489. // how many months in an interval?
  490. var intv = Math.round(titarget/month);
  491. if (intv < 1) {
  492. intv = 1;
  493. }
  494. else if (intv > 6) {
  495. intv = 6;
  496. }
  497. // figure out the starting month and ending month.
  498. var mstart = new $.jsDate(min).setDate(1).setHours(0,0,0,0);
  499. // See if max ends exactly on a month
  500. var tempmend = new $.jsDate(max);
  501. var mend = new $.jsDate(max).setDate(1).setHours(0,0,0,0);
  502. if (tempmend.getTime() !== mend.getTime()) {
  503. mend = mend.add(1, 'month');
  504. }
  505. var nmonths = mend.diff(mstart, 'month');
  506. nttarget = Math.ceil(nmonths/intv) + 1;
  507. this.min = mstart.getTime();
  508. this.max = mstart.clone().add((nttarget - 1) * intv, 'month').getTime();
  509. this.numberTicks = nttarget;
  510. for (var i=0; i<nttarget; i++) {
  511. if (i === 0) {
  512. opts.value = mstart.getTime();
  513. }
  514. else {
  515. opts.value = mstart.add(intv, 'month').getTime();
  516. }
  517. t = new this.tickRenderer(opts);
  518. if (this._overrideFormatString && this._autoFormatString != '') {
  519. t.formatString = this._autoFormatString;
  520. }
  521. if (!this.showTicks) {
  522. t.showLabel = false;
  523. t.showMark = false;
  524. }
  525. else if (!this.showTickMarks) {
  526. t.showMark = false;
  527. }
  528. this._ticks.push(t);
  529. }
  530. insetMult = intv * month;
  531. }
  532. // use yearly intervals
  533. else {
  534. this._autoFormatString = '%v';
  535. // how many years in an interval?
  536. var intv = Math.round(titarget/year);
  537. if (intv < 1) {
  538. intv = 1;
  539. }
  540. // figure out the starting and ending years.
  541. var mstart = new $.jsDate(min).setMonth(0, 1).setHours(0,0,0,0);
  542. var mend = new $.jsDate(max).add(1, 'year').setMonth(0, 1).setHours(0,0,0,0);
  543. var nyears = mend.diff(mstart, 'year');
  544. nttarget = Math.ceil(nyears/intv) + 1;
  545. this.min = mstart.getTime();
  546. this.max = mstart.clone().add((nttarget - 1) * intv, 'year').getTime();
  547. this.numberTicks = nttarget;
  548. for (var i=0; i<nttarget; i++) {
  549. if (i === 0) {
  550. opts.value = mstart.getTime();
  551. }
  552. else {
  553. opts.value = mstart.add(intv, 'year').getTime();
  554. }
  555. t = new this.tickRenderer(opts);
  556. if (this._overrideFormatString && this._autoFormatString != '') {
  557. t.formatString = this._autoFormatString;
  558. }
  559. if (!this.showTicks) {
  560. t.showLabel = false;
  561. t.showMark = false;
  562. }
  563. else if (!this.showTickMarks) {
  564. t.showMark = false;
  565. }
  566. this._ticks.push(t);
  567. }
  568. insetMult = intv * year;
  569. }
  570. }
  571. ////////
  572. // Some option(s) specified, work around that.
  573. ////////
  574. else {
  575. if (name == 'xaxis' || name == 'x2axis') {
  576. dim = this._plotDimensions.width;
  577. }
  578. else {
  579. dim = this._plotDimensions.height;
  580. }
  581. // if min, max and number of ticks specified, user can't specify interval.
  582. if (this.min != null && this.max != null && this.numberTicks != null) {
  583. this.tickInterval = null;
  584. }
  585. if (this.tickInterval != null && daTickInterval != null) {
  586. this.daTickInterval = daTickInterval;
  587. }
  588. // if min and max are same, space them out a bit
  589. if (min == max) {
  590. var adj = 24*60*60*500; // 1/2 day
  591. min -= adj;
  592. max += adj;
  593. }
  594. range = max - min;
  595. var optNumTicks = 2 + parseInt(Math.max(0, dim-100)/100, 10);
  596. var rmin, rmax;
  597. rmin = (this.min != null) ? new $.jsDate(this.min).getTime() : min - range/2*(this.padMin - 1);
  598. rmax = (this.max != null) ? new $.jsDate(this.max).getTime() : max + range/2*(this.padMax - 1);
  599. this.min = rmin;
  600. this.max = rmax;
  601. range = this.max - this.min;
  602. if (this.numberTicks == null){
  603. // if tickInterval is specified by user, we will ignore computed maximum.
  604. // max will be equal or greater to fit even # of ticks.
  605. if (this.daTickInterval != null) {
  606. var nc = new $.jsDate(this.max).diff(this.min, this.daTickInterval[1], true);
  607. this.numberTicks = Math.ceil(nc/this.daTickInterval[0]) +1;
  608. // this.max = new $.jsDate(this.min).add(this.numberTicks-1, this.daTickInterval[1]).getTime();
  609. this.max = new $.jsDate(this.min).add((this.numberTicks-1) * this.daTickInterval[0], this.daTickInterval[1]).getTime();
  610. }
  611. else if (dim > 200) {
  612. this.numberTicks = parseInt(3+(dim-200)/100, 10);
  613. }
  614. else {
  615. this.numberTicks = 2;
  616. }
  617. }
  618. insetMult = range / (this.numberTicks-1)/1000;
  619. if (this.daTickInterval == null) {
  620. this.daTickInterval = [insetMult, 'seconds'];
  621. }
  622. for (var i=0; i<this.numberTicks; i++){
  623. var min = new $.jsDate(this.min);
  624. tt = min.add(i*this.daTickInterval[0], this.daTickInterval[1]).getTime();
  625. var t = new this.tickRenderer(this.tickOptions);
  626. // var t = new $.jqplot.AxisTickRenderer(this.tickOptions);
  627. if (!this.showTicks) {
  628. t.showLabel = false;
  629. t.showMark = false;
  630. }
  631. else if (!this.showTickMarks) {
  632. t.showMark = false;
  633. }
  634. t.setTick(tt, this.name);
  635. this._ticks.push(t);
  636. }
  637. }
  638. if (this.tickInset) {
  639. this.min = this.min - this.tickInset * insetMult;
  640. this.max = this.max + this.tickInset * insetMult;
  641. }
  642. if (this._daTickInterval == null) {
  643. this._daTickInterval = this.daTickInterval;
  644. }
  645. ticks = null;
  646. };
  647. })(jQuery);