No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

440 líneas
17 KiB

  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: 1.0.0b2_r792
  6. *
  7. * Copyright (c) 2009-2011 Chris Leonello
  8. * jqPlot is currently available for use in all personal or commercial projects
  9. * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
  10. * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
  11. * choose the license that best suits your project and use it accordingly.
  12. *
  13. * Although not required, the author would appreciate an email letting him
  14. * know of any substantial use of jqPlot. You can reach the author at:
  15. * chris at jqplot dot com or see http://www.jqplot.com/info.php .
  16. *
  17. * If you are feeling kind and generous, consider supporting the project by
  18. * making a donation at: http://www.jqplot.com/donate.php .
  19. *
  20. * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
  21. *
  22. * version 2007.04.27
  23. * author Ash Searle
  24. * http://hexmen.com/blog/2007/03/printf-sprintf/
  25. * http://hexmen.com/js/sprintf.js
  26. * The author (Ash Searle) has placed this code in the public domain:
  27. * "This code is unrestricted: you are free to use it however you like."
  28. *
  29. */
  30. (function($) {
  31. /**
  32. * Class: $.jqplot.DateAxisRenderer
  33. * A plugin for a jqPlot to render an axis as a series of date values.
  34. * This renderer has no options beyond those supplied by the <Axis> class.
  35. * It supplies it's own tick formatter, so the tickOptions.formatter option
  36. * should not be overridden.
  37. *
  38. * Thanks to Ken Synder for his enhanced Date instance methods which are
  39. * included with this code <http://kendsnyder.com/sandbox/date/>.
  40. *
  41. * To use this renderer, include the plugin in your source
  42. * > <script type="text/javascript" language="javascript" src="plugins/jqplot.dateAxisRenderer.js"></script>
  43. *
  44. * and supply the appropriate options to your plot
  45. *
  46. * > {axes:{xaxis:{renderer:$.jqplot.DateAxisRenderer}}}
  47. *
  48. * Dates can be passed into the axis in almost any recognizable value and
  49. * will be parsed. They will be rendered on the axis in the format
  50. * specified by tickOptions.formatString. e.g. tickOptions.formatString = '%Y-%m-%d'.
  51. *
  52. * Accecptable format codes
  53. * are:
  54. *
  55. * > Code Result Description
  56. * > == Years ==
  57. * > %Y 2008 Four-digit year
  58. * > %y 08 Two-digit year
  59. * > == Months ==
  60. * > %m 09 Two-digit month
  61. * > %#m 9 One or two-digit month
  62. * > %B September Full month name
  63. * > %b Sep Abbreviated month name
  64. * > == Days ==
  65. * > %d 05 Two-digit day of month
  66. * > %#d 5 One or two-digit day of month
  67. * > %e 5 One or two-digit day of month
  68. * > %A Sunday Full name of the day of the week
  69. * > %a Sun Abbreviated name of the day of the week
  70. * > %w 0 Number of the day of the week (0 = Sunday, 6 = Saturday)
  71. * > %o th The ordinal suffix string following the day of the month
  72. * > == Hours ==
  73. * > %H 23 Hours in 24-hour format (two digits)
  74. * > %#H 3 Hours in 24-hour integer format (one or two digits)
  75. * > %I 11 Hours in 12-hour format (two digits)
  76. * > %#I 3 Hours in 12-hour integer format (one or two digits)
  77. * > %p PM AM or PM
  78. * > == Minutes ==
  79. * > %M 09 Minutes (two digits)
  80. * > %#M 9 Minutes (one or two digits)
  81. * > == Seconds ==
  82. * > %S 02 Seconds (two digits)
  83. * > %#S 2 Seconds (one or two digits)
  84. * > %s 1206567625723 Unix timestamp (Seconds past 1970-01-01 00:00:00)
  85. * > == Milliseconds ==
  86. * > %N 008 Milliseconds (three digits)
  87. * > %#N 8 Milliseconds (one to three digits)
  88. * > == Timezone ==
  89. * > %O 360 difference in minutes between local time and GMT
  90. * > %Z Mountain Standard Time Name of timezone as reported by browser
  91. * > %G -06:00 Hours and minutes between GMT
  92. * > == Shortcuts ==
  93. * > %F 2008-03-26 %Y-%m-%d
  94. * > %T 05:06:30 %H:%M:%S
  95. * > %X 05:06:30 %H:%M:%S
  96. * > %x 03/26/08 %m/%d/%y
  97. * > %D 03/26/08 %m/%d/%y
  98. * > %#c Wed Mar 26 15:31:00 2008 %a %b %e %H:%M:%S %Y
  99. * > %v 3-Sep-2008 %e-%b-%Y
  100. * > %R 15:31 %H:%M
  101. * > %r 3:31:00 PM %I:%M:%S %p
  102. * > == Characters ==
  103. * > %n \n Newline
  104. * > %t \t Tab
  105. * > %% % Percent Symbol
  106. */
  107. $.jqplot.DateAxisRenderer = function() {
  108. $.jqplot.LinearAxisRenderer.call(this);
  109. this.date = new $.jsDate();
  110. };
  111. $.jqplot.DateAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
  112. $.jqplot.DateAxisRenderer.prototype.constructor = $.jqplot.DateAxisRenderer;
  113. $.jqplot.DateTickFormatter = function(format, val) {
  114. if (!format) {
  115. format = '%Y/%m/%d';
  116. }
  117. return $.jsDate.strftime(val, format);
  118. };
  119. $.jqplot.DateAxisRenderer.prototype.init = function(options){
  120. // prop: tickRenderer
  121. // A class of a rendering engine for creating the ticks labels displayed on the plot,
  122. // See <$.jqplot.AxisTickRenderer>.
  123. // this.tickRenderer = $.jqplot.AxisTickRenderer;
  124. // this.labelRenderer = $.jqplot.AxisLabelRenderer;
  125. this.tickOptions.formatter = $.jqplot.DateTickFormatter;
  126. this.daTickInterval = null;
  127. this._daTickInterval = null;
  128. $.extend(true, this, options);
  129. var db = this._dataBounds,
  130. stats,
  131. sum,
  132. s,
  133. d,
  134. pd,
  135. sd,
  136. intv;
  137. // Go through all the series attached to this axis and find
  138. // the min/max bounds for this axis.
  139. for (var i=0; i<this._series.length; i++) {
  140. stats = {intervals:[], frequencies:{}, sortedIntervals:[], min:null, max:null, mean:null};
  141. sum = 0;
  142. s = this._series[i];
  143. d = s.data;
  144. pd = s._plotData;
  145. sd = s._stackData;
  146. intv = 0;
  147. for (var j=0; j<d.length; j++) {
  148. if (this.name == 'xaxis' || this.name == 'x2axis') {
  149. d[j][0] = new $.jsDate(d[j][0]).getTime();
  150. pd[j][0] = new $.jsDate(d[j][0]).getTime();
  151. sd[j][0] = new $.jsDate(d[j][0]).getTime();
  152. if ((d[j][0] != null && d[j][0] < db.min) || db.min == null) {
  153. db.min = d[j][0];
  154. }
  155. if ((d[j][0] != null && d[j][0] > db.max) || db.max == null) {
  156. db.max = d[j][0];
  157. }
  158. if (j>0) {
  159. intv = Math.abs(d[j][0] - d[j-1][0]);
  160. stats.intervals.push(intv);
  161. if (stats.frequencies.hasOwnProperty(intv)) {
  162. stats.frequencies[intv] += 1;
  163. }
  164. else {
  165. stats.frequencies[intv] = 1;
  166. }
  167. }
  168. sum += intv;
  169. }
  170. else {
  171. d[j][1] = new $.jsDate(d[j][1]).getTime();
  172. pd[j][1] = new $.jsDate(d[j][1]).getTime();
  173. sd[j][1] = new $.jsDate(d[j][1]).getTime();
  174. if ((d[j][1] != null && d[j][1] < db.min) || db.min == null) {
  175. db.min = d[j][1];
  176. }
  177. if ((d[j][1] != null && d[j][1] > db.max) || db.max == null) {
  178. db.max = d[j][1];
  179. }
  180. if (j>0) {
  181. intv = Math.abs(d[j][1] - d[j-1][1]);
  182. stats.intervals.push(intv);
  183. if (stats.frequencies.hasOwnProperty(intv)) {
  184. stats.frequencies[intv] += 1;
  185. }
  186. else {
  187. stats.frequencies[intv] = 1;
  188. }
  189. }
  190. }
  191. sum += intv;
  192. }
  193. var tempf = 0,
  194. tempn=0;
  195. for (var n in stats.frequencies) {
  196. stats.sortedIntervals.push({interval:n, frequency:stats.frequencies[n]});
  197. }
  198. stats.sortedIntervals.sort(function(a, b){
  199. return b.frequency - a.frequency;
  200. });
  201. stats.min = $.jqplot.arrayMin(stats.intervals);
  202. stats.max = $.jqplot.arrayMax(stats.intervals);
  203. stats.mean = sum/d.length;
  204. this._intervalStats.push(stats);
  205. stats = sum = s = d = pd = sd = null;
  206. }
  207. db = null;
  208. };
  209. // called with scope of an axis
  210. $.jqplot.DateAxisRenderer.prototype.reset = function() {
  211. this.min = this._min;
  212. this.max = this._max;
  213. this.tickInterval = this._tickInterval;
  214. this.numberTicks = this._numberTicks;
  215. this.daTickInterval = this._daTickInterval;
  216. // this._ticks = this.__ticks;
  217. };
  218. $.jqplot.DateAxisRenderer.prototype.createTicks = function() {
  219. // we're are operating on an axis here
  220. var ticks = this._ticks;
  221. var userTicks = this.ticks;
  222. var name = this.name;
  223. // databounds were set on axis initialization.
  224. var db = this._dataBounds;
  225. var iv = this._intervalStats;
  226. var dim, interval;
  227. var min, max;
  228. var pos1, pos2;
  229. var tt, i;
  230. // if we already have ticks, use them.
  231. // ticks must be in order of increasing value.
  232. min = ((this.min != null) ? new $.jsDate(this.min).getTime() : db.min);
  233. max = ((this.max != null) ? new $.jsDate(this.max).getTime() : db.max);
  234. var range = max - min;
  235. if (userTicks.length) {
  236. // ticks could be 1D or 2D array of [val, val, ,,,] or [[val, label], [val, label], ...] or mixed
  237. for (i=0; i<userTicks.length; i++){
  238. var ut = userTicks[i];
  239. var t = new this.tickRenderer(this.tickOptions);
  240. if (ut.constructor == Array) {
  241. t.value = new $.jsDate(ut[0]).getTime();
  242. t.label = ut[1];
  243. if (!this.showTicks) {
  244. t.showLabel = false;
  245. t.showMark = false;
  246. }
  247. else if (!this.showTickMarks) {
  248. t.showMark = false;
  249. }
  250. t.setTick(t.value, this.name);
  251. this._ticks.push(t);
  252. }
  253. else {
  254. t.value = new $.jsDate(ut).getTime();
  255. if (!this.showTicks) {
  256. t.showLabel = false;
  257. t.showMark = false;
  258. }
  259. else if (!this.showTickMarks) {
  260. t.showMark = false;
  261. }
  262. t.setTick(t.value, this.name);
  263. this._ticks.push(t);
  264. }
  265. }
  266. this.numberTicks = userTicks.length;
  267. this.min = this._ticks[0].value;
  268. this.max = this._ticks[this.numberTicks-1].value;
  269. this.daTickInterval = [(this.max - this.min) / (this.numberTicks - 1)/1000, 'seconds'];
  270. }
  271. ////////
  272. // We don't have any ticks yet, let's make some!
  273. // Doing complete autoscaling, no user options specified
  274. ////////
  275. else if (this.tickInterval == null && this.min == null && this.max == null && this.numberTicks == null) {
  276. var ret = $.jqplot.LinearTickGenerator(min, max);
  277. // calculate a padded max and min, points should be less than these
  278. // so that they aren't too close to the edges of the plot.
  279. // User can adjust how much padding is allowed with pad, padMin and PadMax options.
  280. var tumin = min + range*(this.padMin - 1);
  281. var tumax = max - range*(this.padMax - 1);
  282. if (min <=tumin || max >= tumax) {
  283. tumin = min - range*(this.padMin - 1);
  284. tumax = max + range*(this.padMax - 1);
  285. ret = $.jqplot.LinearTickGenerator(tumin, tumax);
  286. }
  287. this.min = ret[0];
  288. this.max = ret[1];
  289. this.numberTicks = ret[2];
  290. this.tickInterval = ret[4];
  291. this.daTickInterval = [this.tickInterval/1000, 'seconds'];
  292. for (var i=0; i<this.numberTicks; i++){
  293. var min = new $.jsDate(this.min);
  294. tt = min.add(i*this.daTickInterval[0], this.daTickInterval[1]).getTime();
  295. var t = new this.tickRenderer(this.tickOptions);
  296. // var t = new $.jqplot.AxisTickRenderer(this.tickOptions);
  297. if (!this.showTicks) {
  298. t.showLabel = false;
  299. t.showMark = false;
  300. }
  301. else if (!this.showTickMarks) {
  302. t.showMark = false;
  303. }
  304. t.setTick(tt, this.name);
  305. this._ticks.push(t);
  306. }
  307. }
  308. ////////
  309. // Some option(s) specified, work around that.
  310. ////////
  311. else {
  312. if (name == 'xaxis' || name == 'x2axis') {
  313. dim = this._plotDimensions.width;
  314. }
  315. else {
  316. dim = this._plotDimensions.height;
  317. }
  318. // if min, max and number of ticks specified, user can't specify interval.
  319. if (this.min != null && this.max != null && this.numberTicks != null) {
  320. this.tickInterval = null;
  321. }
  322. // if user specified a tick interval, convert to usable.
  323. if (this.tickInterval != null)
  324. {
  325. // if interval is a number or can be converted to one, use it.
  326. // Assume it is in SECONDS!!!
  327. if (Number(this.tickInterval)) {
  328. this.daTickInterval = [Number(this.tickInterval), 'seconds'];
  329. }
  330. // else, parse out something we can build from.
  331. else if (typeof this.tickInterval == "string") {
  332. var parts = this.tickInterval.split(' ');
  333. if (parts.length == 1) {
  334. this.daTickInterval = [1, parts[0]];
  335. }
  336. else if (parts.length == 2) {
  337. this.daTickInterval = [parts[0], parts[1]];
  338. }
  339. }
  340. }
  341. // if min and max are same, space them out a bit
  342. if (min == max) {
  343. var adj = 24*60*60*500; // 1/2 day
  344. min -= adj;
  345. max += adj;
  346. }
  347. range = max - min;
  348. var optNumTicks = 2 + parseInt(Math.max(0, dim-100)/100, 10);
  349. // Here try to set ticks based on data spacing.
  350. // if (this.min == null && this.max == null && this.numberTicks == null && this.tickInterval == null) {
  351. // //
  352. // }
  353. var rmin, rmax;
  354. rmin = (this.min != null) ? new $.jsDate(this.min).getTime() : min - range/2*(this.padMin - 1);
  355. rmax = (this.max != null) ? new $.jsDate(this.max).getTime() : max + range/2*(this.padMax - 1);
  356. this.min = rmin;
  357. this.max = rmax;
  358. range = this.max - this.min;
  359. if (this.numberTicks == null){
  360. // if tickInterval is specified by user, we will ignore computed maximum.
  361. // max will be equal or greater to fit even # of ticks.
  362. if (this.daTickInterval != null) {
  363. var nc = new $.jsDate(this.max).diff(this.min, this.daTickInterval[1], true);
  364. this.numberTicks = Math.ceil(nc/this.daTickInterval[0]) +1;
  365. // this.max = new $.jsDate(this.min).add(this.numberTicks-1, this.daTickInterval[1]).getTime();
  366. this.max = new $.jsDate(this.min).add((this.numberTicks-1) * this.daTickInterval[0], this.daTickInterval[1]).getTime();
  367. }
  368. else if (dim > 200) {
  369. this.numberTicks = parseInt(3+(dim-200)/100, 10);
  370. }
  371. else {
  372. this.numberTicks = 2;
  373. }
  374. }
  375. if (this.daTickInterval == null) {
  376. this.daTickInterval = [range / (this.numberTicks-1)/1000, 'seconds'];
  377. }
  378. for (var i=0; i<this.numberTicks; i++){
  379. var min = new $.jsDate(this.min);
  380. tt = min.add(i*this.daTickInterval[0], this.daTickInterval[1]).getTime();
  381. var t = new this.tickRenderer(this.tickOptions);
  382. // var t = new $.jqplot.AxisTickRenderer(this.tickOptions);
  383. if (!this.showTicks) {
  384. t.showLabel = false;
  385. t.showMark = false;
  386. }
  387. else if (!this.showTickMarks) {
  388. t.showMark = false;
  389. }
  390. t.setTick(tt, this.name);
  391. this._ticks.push(t);
  392. }
  393. }
  394. if (this._daTickInterval == null) {
  395. this._daTickInterval = this.daTickInterval;
  396. }
  397. };
  398. })(jQuery);