You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

636 line
26 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.CategoryAxisRenderer
  33. * A plugin for jqPlot to render a category style axis, with equal pixel spacing between y data values of a series.
  34. *
  35. * To use this renderer, include the plugin in your source
  36. * > <script type="text/javascript" language="javascript" src="plugins/jqplot.categoryAxisRenderer.js"></script>
  37. *
  38. * and supply the appropriate options to your plot
  39. *
  40. * > {axes:{xaxis:{renderer:$.jqplot.CategoryAxisRenderer}}}
  41. **/
  42. $.jqplot.CategoryAxisRenderer = function(options) {
  43. $.jqplot.LinearAxisRenderer.call(this);
  44. // prop: sortMergedLabels
  45. // True to sort tick labels when labels are created by merging
  46. // x axis values from multiple series. That is, say you have
  47. // two series like:
  48. // > line1 = [[2006, 4], [2008, 9], [2009, 16]];
  49. // > line2 = [[2006, 3], [2007, 7], [2008, 6]];
  50. // If no label array is specified, tick labels will be collected
  51. // from the x values of the series. With sortMergedLabels
  52. // set to true, tick labels will be:
  53. // > [2006, 2007, 2008, 2009]
  54. // With sortMergedLabels set to false, tick labels will be:
  55. // > [2006, 2008, 2009, 2007]
  56. //
  57. // Note, this property is specified on the renderOptions for the
  58. // axes when creating a plot:
  59. // > axes:{xaxis:{renderer:$.jqplot.CategoryAxisRenderer, rendererOptions:{sortMergedLabels:true}}}
  60. this.sortMergedLabels = false;
  61. };
  62. $.jqplot.CategoryAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
  63. $.jqplot.CategoryAxisRenderer.prototype.constructor = $.jqplot.CategoryAxisRenderer;
  64. $.jqplot.CategoryAxisRenderer.prototype.init = function(options){
  65. this.groups = 1;
  66. this.groupLabels = [];
  67. this._groupLabels = [];
  68. this._grouped = false;
  69. this._barsPerGroup = null;
  70. // prop: tickRenderer
  71. // A class of a rendering engine for creating the ticks labels displayed on the plot,
  72. // See <$.jqplot.AxisTickRenderer>.
  73. // this.tickRenderer = $.jqplot.AxisTickRenderer;
  74. // this.labelRenderer = $.jqplot.AxisLabelRenderer;
  75. $.extend(true, this, {tickOptions:{formatString:'%d'}}, options);
  76. var db = this._dataBounds;
  77. // Go through all the series attached to this axis and find
  78. // the min/max bounds for this axis.
  79. for (var i=0; i<this._series.length; i++) {
  80. var s = this._series[i];
  81. if (s.groups) {
  82. this.groups = s.groups;
  83. }
  84. var d = s.data;
  85. for (var j=0; j<d.length; j++) {
  86. if (this.name == 'xaxis' || this.name == 'x2axis') {
  87. if (d[j][0] < db.min || db.min == null) {
  88. db.min = d[j][0];
  89. }
  90. if (d[j][0] > db.max || db.max == null) {
  91. db.max = d[j][0];
  92. }
  93. }
  94. else {
  95. if (d[j][1] < db.min || db.min == null) {
  96. db.min = d[j][1];
  97. }
  98. if (d[j][1] > db.max || db.max == null) {
  99. db.max = d[j][1];
  100. }
  101. }
  102. }
  103. }
  104. if (this.groupLabels.length) {
  105. this.groups = this.groupLabels.length;
  106. }
  107. };
  108. $.jqplot.CategoryAxisRenderer.prototype.createTicks = function() {
  109. // we're are operating on an axis here
  110. var ticks = this._ticks;
  111. var userTicks = this.ticks;
  112. var name = this.name;
  113. // databounds were set on axis initialization.
  114. var db = this._dataBounds;
  115. var dim, interval;
  116. var min, max;
  117. var pos1, pos2;
  118. var tt, i;
  119. // if we already have ticks, use them.
  120. if (userTicks.length) {
  121. // adjust with blanks if we have groups
  122. if (this.groups > 1 && !this._grouped) {
  123. var l = userTicks.length;
  124. var skip = parseInt(l/this.groups, 10);
  125. var count = 0;
  126. for (var i=skip; i<l; i+=skip) {
  127. userTicks.splice(i+count, 0, ' ');
  128. count++;
  129. }
  130. this._grouped = true;
  131. }
  132. this.min = 0.5;
  133. this.max = userTicks.length + 0.5;
  134. var range = this.max - this.min;
  135. this.numberTicks = 2*userTicks.length + 1;
  136. for (i=0; i<userTicks.length; i++){
  137. tt = this.min + 2 * i * range / (this.numberTicks-1);
  138. // need a marker before and after the tick
  139. var t = new this.tickRenderer(this.tickOptions);
  140. t.showLabel = false;
  141. // t.showMark = true;
  142. t.setTick(tt, this.name);
  143. this._ticks.push(t);
  144. var t = new this.tickRenderer(this.tickOptions);
  145. t.label = userTicks[i];
  146. // t.showLabel = true;
  147. t.showMark = false;
  148. t.showGridline = false;
  149. t.setTick(tt+0.5, this.name);
  150. this._ticks.push(t);
  151. }
  152. // now add the last tick at the end
  153. var t = new this.tickRenderer(this.tickOptions);
  154. t.showLabel = false;
  155. // t.showMark = true;
  156. t.setTick(tt+1, this.name);
  157. this._ticks.push(t);
  158. }
  159. // we don't have any ticks yet, let's make some!
  160. else {
  161. if (name == 'xaxis' || name == 'x2axis') {
  162. dim = this._plotDimensions.width;
  163. }
  164. else {
  165. dim = this._plotDimensions.height;
  166. }
  167. // if min, max and number of ticks specified, user can't specify interval.
  168. if (this.min != null && this.max != null && this.numberTicks != null) {
  169. this.tickInterval = null;
  170. }
  171. // if max, min, and interval specified and interval won't fit, ignore interval.
  172. if (this.min != null && this.max != null && this.tickInterval != null) {
  173. if (parseInt((this.max-this.min)/this.tickInterval, 10) != (this.max-this.min)/this.tickInterval) {
  174. this.tickInterval = null;
  175. }
  176. }
  177. // find out how many categories are in the lines and collect labels
  178. var labels = [];
  179. var numcats = 0;
  180. var min = 0.5;
  181. var max, val;
  182. var isMerged = false;
  183. for (var i=0; i<this._series.length; i++) {
  184. var s = this._series[i];
  185. for (var j=0; j<s.data.length; j++) {
  186. if (this.name == 'xaxis' || this.name == 'x2axis') {
  187. val = s.data[j][0];
  188. }
  189. else {
  190. val = s.data[j][1];
  191. }
  192. if ($.inArray(val, labels) == -1) {
  193. isMerged = true;
  194. numcats += 1;
  195. labels.push(val);
  196. }
  197. }
  198. }
  199. if (isMerged && this.sortMergedLabels) {
  200. labels.sort(function(a,b) { return a - b; });
  201. }
  202. // keep a reference to these tick labels to use for redrawing plot (see bug #57)
  203. this.ticks = labels;
  204. // now bin the data values to the right lables.
  205. for (var i=0; i<this._series.length; i++) {
  206. var s = this._series[i];
  207. for (var j=0; j<s.data.length; j++) {
  208. if (this.name == 'xaxis' || this.name == 'x2axis') {
  209. val = s.data[j][0];
  210. }
  211. else {
  212. val = s.data[j][1];
  213. }
  214. // for category axis, force the values into category bins.
  215. // we should have the value in the label array now.
  216. var idx = $.inArray(val, labels)+1;
  217. if (this.name == 'xaxis' || this.name == 'x2axis') {
  218. s.data[j][0] = idx;
  219. }
  220. else {
  221. s.data[j][1] = idx;
  222. }
  223. }
  224. }
  225. // adjust with blanks if we have groups
  226. if (this.groups > 1 && !this._grouped) {
  227. var l = labels.length;
  228. var skip = parseInt(l/this.groups, 10);
  229. var count = 0;
  230. for (var i=skip; i<l; i+=skip+1) {
  231. labels[i] = ' ';
  232. }
  233. this._grouped = true;
  234. }
  235. max = numcats + 0.5;
  236. if (this.numberTicks == null) {
  237. this.numberTicks = 2*numcats + 1;
  238. }
  239. var range = max - min;
  240. this.min = min;
  241. this.max = max;
  242. var track = 0;
  243. // todo: adjust this so more ticks displayed.
  244. var maxVisibleTicks = parseInt(3+dim/20, 10);
  245. var skip = parseInt(numcats/maxVisibleTicks, 10);
  246. if (this.tickInterval == null) {
  247. this.tickInterval = range / (this.numberTicks-1);
  248. }
  249. // if tickInterval is specified, we will ignore any computed maximum.
  250. for (var i=0; i<this.numberTicks; i++){
  251. tt = this.min + i * this.tickInterval;
  252. var t = new this.tickRenderer(this.tickOptions);
  253. // if even tick, it isn't a category, it's a divider
  254. if (i/2 == parseInt(i/2, 10)) {
  255. t.showLabel = false;
  256. t.showMark = true;
  257. }
  258. else {
  259. if (skip>0 && track<skip) {
  260. t.showLabel = false;
  261. track += 1;
  262. }
  263. else {
  264. t.showLabel = true;
  265. track = 0;
  266. }
  267. t.label = t.formatter(t.formatString, labels[(i-1)/2]);
  268. t.showMark = false;
  269. t.showGridline = false;
  270. }
  271. t.setTick(tt, this.name);
  272. this._ticks.push(t);
  273. }
  274. }
  275. };
  276. // called with scope of axis
  277. $.jqplot.CategoryAxisRenderer.prototype.draw = function(ctx, plot) {
  278. if (this.show) {
  279. // populate the axis label and value properties.
  280. // createTicks is a method on the renderer, but
  281. // call it within the scope of the axis.
  282. this.renderer.createTicks.call(this);
  283. // fill a div with axes labels in the right direction.
  284. // Need to pregenerate each axis to get it's bounds and
  285. // position it and the labels correctly on the plot.
  286. var dim=0;
  287. var temp;
  288. // Added for theming.
  289. if (this._elem) {
  290. // this._elem.empty();
  291. // Memory Leaks patch
  292. this._elem.emptyForce();
  293. }
  294. this._elem = this._elem || $('<div class="jqplot-axis jqplot-'+this.name+'" style="position:absolute;"></div>');
  295. if (this.name == 'xaxis' || this.name == 'x2axis') {
  296. this._elem.width(this._plotDimensions.width);
  297. }
  298. else {
  299. this._elem.height(this._plotDimensions.height);
  300. }
  301. // create a _label object.
  302. this.labelOptions.axis = this.name;
  303. this._label = new this.labelRenderer(this.labelOptions);
  304. if (this._label.show) {
  305. var elem = this._label.draw(ctx, plot);
  306. elem.appendTo(this._elem);
  307. }
  308. var t = this._ticks;
  309. for (var i=0; i<t.length; i++) {
  310. var tick = t[i];
  311. if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
  312. var elem = tick.draw(ctx, plot);
  313. elem.appendTo(this._elem);
  314. }
  315. }
  316. this._groupLabels = [];
  317. // now make group labels
  318. for (var i=0; i<this.groupLabels.length; i++)
  319. {
  320. var elem = $('<div style="position:absolute;" class="jqplot-'+this.name+'-groupLabel"></div>');
  321. elem.html(this.groupLabels[i]);
  322. this._groupLabels.push(elem);
  323. elem.appendTo(this._elem);
  324. }
  325. }
  326. return this._elem;
  327. };
  328. // called with scope of axis
  329. $.jqplot.CategoryAxisRenderer.prototype.set = function() {
  330. var dim = 0;
  331. var temp;
  332. var w = 0;
  333. var h = 0;
  334. var lshow = (this._label == null) ? false : this._label.show;
  335. if (this.show) {
  336. var t = this._ticks;
  337. for (var i=0; i<t.length; i++) {
  338. var tick = t[i];
  339. if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
  340. if (this.name == 'xaxis' || this.name == 'x2axis') {
  341. temp = tick._elem.outerHeight(true);
  342. }
  343. else {
  344. temp = tick._elem.outerWidth(true);
  345. }
  346. if (temp > dim) {
  347. dim = temp;
  348. }
  349. }
  350. }
  351. var dim2 = 0;
  352. for (var i=0; i<this._groupLabels.length; i++) {
  353. var l = this._groupLabels[i];
  354. if (this.name == 'xaxis' || this.name == 'x2axis') {
  355. temp = l.outerHeight(true);
  356. }
  357. else {
  358. temp = l.outerWidth(true);
  359. }
  360. if (temp > dim2) {
  361. dim2 = temp;
  362. }
  363. }
  364. if (lshow) {
  365. w = this._label._elem.outerWidth(true);
  366. h = this._label._elem.outerHeight(true);
  367. }
  368. if (this.name == 'xaxis') {
  369. dim += dim2 + h;
  370. this._elem.css({'height':dim+'px', left:'0px', bottom:'0px'});
  371. }
  372. else if (this.name == 'x2axis') {
  373. dim += dim2 + h;
  374. this._elem.css({'height':dim+'px', left:'0px', top:'0px'});
  375. }
  376. else if (this.name == 'yaxis') {
  377. dim += dim2 + w;
  378. this._elem.css({'width':dim+'px', left:'0px', top:'0px'});
  379. if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
  380. this._label._elem.css('width', w+'px');
  381. }
  382. }
  383. else {
  384. dim += dim2 + w;
  385. this._elem.css({'width':dim+'px', right:'0px', top:'0px'});
  386. if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
  387. this._label._elem.css('width', w+'px');
  388. }
  389. }
  390. }
  391. };
  392. // called with scope of axis
  393. $.jqplot.CategoryAxisRenderer.prototype.pack = function(pos, offsets) {
  394. var ticks = this._ticks;
  395. var max = this.max;
  396. var min = this.min;
  397. var offmax = offsets.max;
  398. var offmin = offsets.min;
  399. var lshow = (this._label == null) ? false : this._label.show;
  400. var i;
  401. for (var p in pos) {
  402. this._elem.css(p, pos[p]);
  403. }
  404. this._offsets = offsets;
  405. // pixellength will be + for x axes and - for y axes becasue pixels always measured from top left.
  406. var pixellength = offmax - offmin;
  407. var unitlength = max - min;
  408. // point to unit and unit to point conversions references to Plot DOM element top left corner.
  409. this.p2u = function(p){
  410. return (p - offmin) * unitlength / pixellength + min;
  411. };
  412. this.u2p = function(u){
  413. return (u - min) * pixellength / unitlength + offmin;
  414. };
  415. if (this.name == 'xaxis' || this.name == 'x2axis'){
  416. this.series_u2p = function(u){
  417. return (u - min) * pixellength / unitlength;
  418. };
  419. this.series_p2u = function(p){
  420. return p * unitlength / pixellength + min;
  421. };
  422. }
  423. else {
  424. this.series_u2p = function(u){
  425. return (u - max) * pixellength / unitlength;
  426. };
  427. this.series_p2u = function(p){
  428. return p * unitlength / pixellength + max;
  429. };
  430. }
  431. if (this.show) {
  432. if (this.name == 'xaxis' || this.name == 'x2axis') {
  433. for (i=0; i<ticks.length; i++) {
  434. var t = ticks[i];
  435. if (t.show && t.showLabel) {
  436. var shim;
  437. if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
  438. // will need to adjust auto positioning based on which axis this is.
  439. var temp = (this.name == 'xaxis') ? 1 : -1;
  440. switch (t.labelPosition) {
  441. case 'auto':
  442. // position at end
  443. if (temp * t.angle < 0) {
  444. shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  445. }
  446. // position at start
  447. else {
  448. shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
  449. }
  450. break;
  451. case 'end':
  452. shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  453. break;
  454. case 'start':
  455. shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
  456. break;
  457. case 'middle':
  458. shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  459. break;
  460. default:
  461. shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  462. break;
  463. }
  464. }
  465. else {
  466. shim = -t.getWidth()/2;
  467. }
  468. var val = this.u2p(t.value) + shim + 'px';
  469. t._elem.css('left', val);
  470. t.pack();
  471. }
  472. }
  473. var labeledge=['bottom', 0];
  474. if (lshow) {
  475. var w = this._label._elem.outerWidth(true);
  476. this._label._elem.css('left', offmin + pixellength/2 - w/2 + 'px');
  477. if (this.name == 'xaxis') {
  478. this._label._elem.css('bottom', '0px');
  479. labeledge = ['bottom', this._label._elem.outerHeight(true)];
  480. }
  481. else {
  482. this._label._elem.css('top', '0px');
  483. labeledge = ['top', this._label._elem.outerHeight(true)];
  484. }
  485. this._label.pack();
  486. }
  487. // draw the group labels
  488. var step = parseInt(this._ticks.length/this.groups, 10);
  489. for (i=0; i<this._groupLabels.length; i++) {
  490. var mid = 0;
  491. var count = 0;
  492. for (var j=i*step; j<=(i+1)*step; j++) {
  493. if (this._ticks[j]._elem && this._ticks[j].label != " ") {
  494. var t = this._ticks[j]._elem;
  495. var p = t.position();
  496. mid += p.left + t.outerWidth(true)/2;
  497. count++;
  498. }
  499. }
  500. mid = mid/count;
  501. this._groupLabels[i].css({'left':(mid - this._groupLabels[i].outerWidth(true)/2)});
  502. this._groupLabels[i].css(labeledge[0], labeledge[1]);
  503. }
  504. }
  505. else {
  506. for (i=0; i<ticks.length; i++) {
  507. var t = ticks[i];
  508. if (t.show && t.showLabel) {
  509. var shim;
  510. if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
  511. // will need to adjust auto positioning based on which axis this is.
  512. var temp = (this.name == 'yaxis') ? 1 : -1;
  513. switch (t.labelPosition) {
  514. case 'auto':
  515. // position at end
  516. case 'end':
  517. if (temp * t.angle < 0) {
  518. shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
  519. }
  520. else {
  521. shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
  522. }
  523. break;
  524. case 'start':
  525. if (t.angle > 0) {
  526. shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
  527. }
  528. else {
  529. shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
  530. }
  531. break;
  532. case 'middle':
  533. // if (t.angle > 0) {
  534. // shim = -t.getHeight()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  535. // }
  536. // else {
  537. // shim = -t.getHeight()/2 - t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
  538. // }
  539. shim = -t.getHeight()/2;
  540. break;
  541. default:
  542. shim = -t.getHeight()/2;
  543. break;
  544. }
  545. }
  546. else {
  547. shim = -t.getHeight()/2;
  548. }
  549. var val = this.u2p(t.value) + shim + 'px';
  550. t._elem.css('top', val);
  551. t.pack();
  552. }
  553. }
  554. var labeledge=['left', 0];
  555. if (lshow) {
  556. var h = this._label._elem.outerHeight(true);
  557. this._label._elem.css('top', offmax - pixellength/2 - h/2 + 'px');
  558. if (this.name == 'yaxis') {
  559. this._label._elem.css('left', '0px');
  560. labeledge = ['left', this._label._elem.outerWidth(true)];
  561. }
  562. else {
  563. this._label._elem.css('right', '0px');
  564. labeledge = ['right', this._label._elem.outerWidth(true)];
  565. }
  566. this._label.pack();
  567. }
  568. // draw the group labels, position top here, do left after label position.
  569. var step = parseInt(this._ticks.length/this.groups, 10);
  570. for (i=0; i<this._groupLabels.length; i++) {
  571. var mid = 0;
  572. var count = 0;
  573. for (var j=i*step; j<=(i+1)*step; j++) {
  574. if (this._ticks[j]._elem && this._ticks[j].label != " ") {
  575. var t = this._ticks[j]._elem;
  576. var p = t.position();
  577. mid += p.top + t.outerHeight()/2;
  578. count++;
  579. }
  580. }
  581. mid = mid/count;
  582. this._groupLabels[i].css({'top':mid - this._groupLabels[i].outerHeight()/2});
  583. this._groupLabels[i].css(labeledge[0], labeledge[1]);
  584. }
  585. }
  586. }
  587. };
  588. })(jQuery);