jqplot.categoryAxisRenderer.js 26 KB

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