jqplot.categoryAxisRenderer.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: 1.0.0a_r701
  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) {
  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. }
  292. this._elem = this._elem || $('<div class="jqplot-axis jqplot-'+this.name+'" style="position:absolute;"></div>');
  293. if (this.name == 'xaxis' || this.name == 'x2axis') {
  294. this._elem.width(this._plotDimensions.width);
  295. }
  296. else {
  297. this._elem.height(this._plotDimensions.height);
  298. }
  299. // create a _label object.
  300. this.labelOptions.axis = this.name;
  301. this._label = new this.labelRenderer(this.labelOptions);
  302. if (this._label.show) {
  303. var elem = this._label.draw(ctx);
  304. elem.appendTo(this._elem);
  305. }
  306. var t = this._ticks;
  307. for (var i=0; i<t.length; i++) {
  308. var tick = t[i];
  309. if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
  310. var elem = tick.draw(ctx);
  311. elem.appendTo(this._elem);
  312. }
  313. }
  314. this._groupLabels = [];
  315. // now make group labels
  316. for (var i=0; i<this.groupLabels.length; i++)
  317. {
  318. var elem = $('<div style="position:absolute;" class="jqplot-'+this.name+'-groupLabel"></div>');
  319. elem.html(this.groupLabels[i]);
  320. this._groupLabels.push(elem);
  321. elem.appendTo(this._elem);
  322. }
  323. }
  324. return this._elem;
  325. };
  326. // called with scope of axis
  327. $.jqplot.CategoryAxisRenderer.prototype.set = function() {
  328. var dim = 0;
  329. var temp;
  330. var w = 0;
  331. var h = 0;
  332. var lshow = (this._label == null) ? false : this._label.show;
  333. if (this.show) {
  334. var t = this._ticks;
  335. for (var i=0; i<t.length; i++) {
  336. var tick = t[i];
  337. if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
  338. if (this.name == 'xaxis' || this.name == 'x2axis') {
  339. temp = tick._elem.outerHeight(true);
  340. }
  341. else {
  342. temp = tick._elem.outerWidth(true);
  343. }
  344. if (temp > dim) {
  345. dim = temp;
  346. }
  347. }
  348. }
  349. var dim2 = 0;
  350. for (var i=0; i<this._groupLabels.length; i++) {
  351. var l = this._groupLabels[i];
  352. if (this.name == 'xaxis' || this.name == 'x2axis') {
  353. temp = l.outerHeight(true);
  354. }
  355. else {
  356. temp = l.outerWidth(true);
  357. }
  358. if (temp > dim2) {
  359. dim2 = temp;
  360. }
  361. }
  362. if (lshow) {
  363. w = this._label._elem.outerWidth(true);
  364. h = this._label._elem.outerHeight(true);
  365. }
  366. if (this.name == 'xaxis') {
  367. dim += dim2 + h;
  368. this._elem.css({'height':dim+'px', left:'0px', bottom:'0px'});
  369. }
  370. else if (this.name == 'x2axis') {
  371. dim += dim2 + h;
  372. this._elem.css({'height':dim+'px', left:'0px', top:'0px'});
  373. }
  374. else if (this.name == 'yaxis') {
  375. dim += dim2 + w;
  376. this._elem.css({'width':dim+'px', left:'0px', top:'0px'});
  377. if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
  378. this._label._elem.css('width', w+'px');
  379. }
  380. }
  381. else {
  382. dim += dim2 + w;
  383. this._elem.css({'width':dim+'px', right:'0px', top:'0px'});
  384. if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
  385. this._label._elem.css('width', w+'px');
  386. }
  387. }
  388. }
  389. };
  390. // called with scope of axis
  391. $.jqplot.CategoryAxisRenderer.prototype.pack = function(pos, offsets) {
  392. var ticks = this._ticks;
  393. var max = this.max;
  394. var min = this.min;
  395. var offmax = offsets.max;
  396. var offmin = offsets.min;
  397. var lshow = (this._label == null) ? false : this._label.show;
  398. for (var p in pos) {
  399. this._elem.css(p, pos[p]);
  400. }
  401. this._offsets = offsets;
  402. // pixellength will be + for x axes and - for y axes becasue pixels always measured from top left.
  403. var pixellength = offmax - offmin;
  404. var unitlength = max - min;
  405. // point to unit and unit to point conversions references to Plot DOM element top left corner.
  406. this.p2u = function(p){
  407. return (p - offmin) * unitlength / pixellength + min;
  408. };
  409. this.u2p = function(u){
  410. return (u - min) * pixellength / unitlength + offmin;
  411. };
  412. if (this.name == 'xaxis' || this.name == 'x2axis'){
  413. this.series_u2p = function(u){
  414. return (u - min) * pixellength / unitlength;
  415. };
  416. this.series_p2u = function(p){
  417. return p * unitlength / pixellength + min;
  418. };
  419. }
  420. else {
  421. this.series_u2p = function(u){
  422. return (u - max) * pixellength / unitlength;
  423. };
  424. this.series_p2u = function(p){
  425. return p * unitlength / pixellength + max;
  426. };
  427. }
  428. if (this.show) {
  429. if (this.name == 'xaxis' || this.name == 'x2axis') {
  430. for (i=0; i<ticks.length; i++) {
  431. var t = ticks[i];
  432. if (t.show && t.showLabel) {
  433. var shim;
  434. if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
  435. // will need to adjust auto positioning based on which axis this is.
  436. var temp = (this.name == 'xaxis') ? 1 : -1;
  437. switch (t.labelPosition) {
  438. case 'auto':
  439. // position at end
  440. if (temp * t.angle < 0) {
  441. shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  442. }
  443. // position at start
  444. else {
  445. shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
  446. }
  447. break;
  448. case 'end':
  449. shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  450. break;
  451. case 'start':
  452. shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
  453. break;
  454. case 'middle':
  455. shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  456. break;
  457. default:
  458. shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  459. break;
  460. }
  461. }
  462. else {
  463. shim = -t.getWidth()/2;
  464. }
  465. var val = this.u2p(t.value) + shim + 'px';
  466. t._elem.css('left', val);
  467. t.pack();
  468. }
  469. }
  470. var labeledge=['bottom', 0];
  471. if (lshow) {
  472. var w = this._label._elem.outerWidth(true);
  473. this._label._elem.css('left', offmin + pixellength/2 - w/2 + 'px');
  474. if (this.name == 'xaxis') {
  475. this._label._elem.css('bottom', '0px');
  476. labeledge = ['bottom', this._label._elem.outerHeight(true)];
  477. }
  478. else {
  479. this._label._elem.css('top', '0px');
  480. labeledge = ['top', this._label._elem.outerHeight(true)];
  481. }
  482. this._label.pack();
  483. }
  484. // draw the group labels
  485. var step = parseInt(this._ticks.length/this.groups, 10);
  486. for (i=0; i<this._groupLabels.length; i++) {
  487. var mid = 0;
  488. var count = 0;
  489. for (var j=i*step; j<=(i+1)*step; j++) {
  490. if (this._ticks[j]._elem && this._ticks[j].label != " ") {
  491. var t = this._ticks[j]._elem;
  492. var p = t.position();
  493. mid += p.left + t.outerWidth(true)/2;
  494. count++;
  495. }
  496. }
  497. mid = mid/count;
  498. this._groupLabels[i].css({'left':(mid - this._groupLabels[i].outerWidth(true)/2)});
  499. this._groupLabels[i].css(labeledge[0], labeledge[1]);
  500. }
  501. }
  502. else {
  503. for (i=0; i<ticks.length; i++) {
  504. var t = ticks[i];
  505. if (t.show && t.showLabel) {
  506. var shim;
  507. if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
  508. // will need to adjust auto positioning based on which axis this is.
  509. var temp = (this.name == 'yaxis') ? 1 : -1;
  510. switch (t.labelPosition) {
  511. case 'auto':
  512. // position at end
  513. case 'end':
  514. if (temp * t.angle < 0) {
  515. shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
  516. }
  517. else {
  518. shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
  519. }
  520. break;
  521. case 'start':
  522. if (t.angle > 0) {
  523. shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
  524. }
  525. else {
  526. shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
  527. }
  528. break;
  529. case 'middle':
  530. // if (t.angle > 0) {
  531. // shim = -t.getHeight()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  532. // }
  533. // else {
  534. // shim = -t.getHeight()/2 - t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
  535. // }
  536. shim = -t.getHeight()/2;
  537. break;
  538. default:
  539. shim = -t.getHeight()/2;
  540. break;
  541. }
  542. }
  543. else {
  544. shim = -t.getHeight()/2;
  545. }
  546. var val = this.u2p(t.value) + shim + 'px';
  547. t._elem.css('top', val);
  548. t.pack();
  549. }
  550. }
  551. var labeledge=['left', 0];
  552. if (lshow) {
  553. var h = this._label._elem.outerHeight(true);
  554. this._label._elem.css('top', offmax - pixellength/2 - h/2 + 'px');
  555. if (this.name == 'yaxis') {
  556. this._label._elem.css('left', '0px');
  557. labeledge = ['left', this._label._elem.outerWidth(true)];
  558. }
  559. else {
  560. this._label._elem.css('right', '0px');
  561. labeledge = ['right', this._label._elem.outerWidth(true)];
  562. }
  563. this._label.pack();
  564. }
  565. // draw the group labels, position top here, do left after label position.
  566. var step = parseInt(this._ticks.length/this.groups, 10);
  567. for (i=0; i<this._groupLabels.length; i++) {
  568. var mid = 0;
  569. var count = 0;
  570. for (var j=i*step; j<=(i+1)*step; j++) {
  571. if (this._ticks[j]._elem && this._ticks[j].label != " ") {
  572. var t = this._ticks[j]._elem;
  573. var p = t.position();
  574. mid += p.top + t.outerHeight()/2;
  575. count++;
  576. }
  577. }
  578. mid = mid/count;
  579. this._groupLabels[i].css({'top':mid - this._groupLabels[i].outerHeight()/2});
  580. this._groupLabels[i].css(labeledge[0], labeledge[1]);
  581. }
  582. }
  583. }
  584. };
  585. })(jQuery);