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.
 
 
 
 
 
 

611 líneas
25 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. // class: $.jqplot.MekkoAxisRenderer
  32. // An axis renderer for a Mekko chart.
  33. // Should be used with a Mekko chart where the mekkoRenderer is used on the series.
  34. // Displays the Y axis as a range from 0 to 1 (0 to 100%) and the x axis with a tick
  35. // for each series scaled to the sum of all the y values.
  36. $.jqplot.MekkoAxisRenderer = function() {
  37. };
  38. // called with scope of axis object.
  39. $.jqplot.MekkoAxisRenderer.prototype.init = function(options){
  40. // prop: tickMode
  41. // How to space the ticks on the axis.
  42. // 'bar' will place a tick at the width of each bar.
  43. // This is the default for the x axis.
  44. // 'even' will place ticks at even intervals. This is
  45. // the default for x2 axis and y axis. y axis cannot be changed.
  46. this.tickMode;
  47. // prop: barLabelRenderer
  48. // renderer to use to draw labels under each bar.
  49. this.barLabelRenderer = $.jqplot.AxisLabelRenderer;
  50. // prop: barLabels
  51. // array of labels to put under each bar.
  52. this.barLabels = this.barLabels || [];
  53. // prop: barLabelOptions
  54. // options object to pass to the bar label renderer.
  55. this.barLabelOptions = {};
  56. this.tickOptions = $.extend(true, {showGridline:false}, this.tickOptions);
  57. this._barLabels = [];
  58. $.extend(true, this, options);
  59. if (this.name == 'yaxis') {
  60. this.tickOptions.formatString = this.tickOptions.formatString || "%d\%";
  61. }
  62. var db = this._dataBounds;
  63. db.min = 0;
  64. // for y axes, scale always go from 0 to 1 (0 to 100%)
  65. if (this.name == 'yaxis' || this.name == 'y2axis') {
  66. db.max = 100;
  67. this.tickMode = 'even';
  68. }
  69. // For x axes, scale goes from 0 to sum of all y values.
  70. else if (this.name == 'xaxis'){
  71. this.tickMode = (this.tickMode == null) ? 'bar' : this.tickMode;
  72. for (var i=0; i<this._series.length; i++) {
  73. db.max += this._series[i]._sumy;
  74. }
  75. }
  76. else if (this.name == 'x2axis'){
  77. this.tickMode = (this.tickMode == null) ? 'even' : this.tickMode;
  78. for (var i=0; i<this._series.length; i++) {
  79. db.max += this._series[i]._sumy;
  80. }
  81. }
  82. };
  83. // called with scope of axis
  84. $.jqplot.MekkoAxisRenderer.prototype.draw = function(ctx) {
  85. if (this.show) {
  86. // populate the axis label and value properties.
  87. // createTicks is a method on the renderer, but
  88. // call it within the scope of the axis.
  89. this.renderer.createTicks.call(this);
  90. // fill a div with axes labels in the right direction.
  91. // Need to pregenerate each axis to get it's bounds and
  92. // position it and the labels correctly on the plot.
  93. var dim=0;
  94. var temp;
  95. var elem = document.createElement('div');
  96. this._elem = $(elem);
  97. this._elem.addClass('jqplot-axis jqplot-'+this.name);
  98. this._elem.css('position', 'absolute');
  99. elem = null;
  100. if (this.name == 'xaxis' || this.name == 'x2axis') {
  101. this._elem.width(this._plotDimensions.width);
  102. }
  103. else {
  104. this._elem.height(this._plotDimensions.height);
  105. }
  106. // draw the axis label
  107. // create a _label object.
  108. this.labelOptions.axis = this.name;
  109. this._label = new this.labelRenderer(this.labelOptions);
  110. if (this._label.show) {
  111. this._elem.append(this._label.draw(ctx));
  112. }
  113. var t, tick, elem;
  114. if (this.showTicks) {
  115. t = this._ticks;
  116. for (var i=0; i<t.length; i++) {
  117. tick = t[i];
  118. if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
  119. this._elem.append(tick.draw(ctx));
  120. }
  121. }
  122. }
  123. // draw the series labels
  124. for (i=0; i<this.barLabels.length; i++) {
  125. this.barLabelOptions.axis = this.name;
  126. this.barLabelOptions.label = this.barLabels[i];
  127. this._barLabels.push(new this.barLabelRenderer(this.barLabelOptions));
  128. if (this.tickMode != 'bar') {
  129. this._barLabels[i].show = false;
  130. }
  131. if (this._barLabels[i].show) {
  132. var elem = this._barLabels[i].draw(ctx);
  133. elem.removeClass('jqplot-'+this.name+'-label');
  134. elem.addClass('jqplot-'+this.name+'-tick');
  135. elem.addClass('jqplot-mekko-barLabel');
  136. elem.appendTo(this._elem);
  137. elem = null;
  138. }
  139. }
  140. }
  141. return this._elem;
  142. };
  143. // called with scope of an axis
  144. $.jqplot.MekkoAxisRenderer.prototype.reset = function() {
  145. this.min = this._min;
  146. this.max = this._max;
  147. this.tickInterval = this._tickInterval;
  148. this.numberTicks = this._numberTicks;
  149. // this._ticks = this.__ticks;
  150. };
  151. // called with scope of axis
  152. $.jqplot.MekkoAxisRenderer.prototype.set = function() {
  153. var dim = 0;
  154. var temp;
  155. var w = 0;
  156. var h = 0;
  157. var lshow = (this._label == null) ? false : this._label.show;
  158. if (this.show && this.showTicks) {
  159. var t = this._ticks;
  160. for (var i=0; i<t.length; i++) {
  161. var tick = t[i];
  162. if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
  163. if (this.name == 'xaxis' || this.name == 'x2axis') {
  164. temp = tick._elem.outerHeight(true);
  165. }
  166. else {
  167. temp = tick._elem.outerWidth(true);
  168. }
  169. if (temp > dim) {
  170. dim = temp;
  171. }
  172. }
  173. }
  174. if (lshow) {
  175. w = this._label._elem.outerWidth(true);
  176. h = this._label._elem.outerHeight(true);
  177. }
  178. if (this.name == 'xaxis') {
  179. dim = dim + h;
  180. this._elem.css({'height':dim+'px', left:'0px', bottom:'0px'});
  181. }
  182. else if (this.name == 'x2axis') {
  183. dim = dim + h;
  184. this._elem.css({'height':dim+'px', left:'0px', top:'0px'});
  185. }
  186. else if (this.name == 'yaxis') {
  187. dim = dim + w;
  188. this._elem.css({'width':dim+'px', left:'0px', top:'0px'});
  189. if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
  190. this._label._elem.css('width', w+'px');
  191. }
  192. }
  193. else {
  194. dim = dim + w;
  195. this._elem.css({'width':dim+'px', right:'0px', top:'0px'});
  196. if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
  197. this._label._elem.css('width', w+'px');
  198. }
  199. }
  200. }
  201. };
  202. // called with scope of axis
  203. $.jqplot.MekkoAxisRenderer.prototype.createTicks = function() {
  204. // we're are operating on an axis here
  205. var ticks = this._ticks;
  206. var userTicks = this.ticks;
  207. var name = this.name;
  208. // databounds were set on axis initialization.
  209. var db = this._dataBounds;
  210. var dim, interval;
  211. var min, max;
  212. var pos1, pos2;
  213. var t, tt, i, j;
  214. // if we already have ticks, use them.
  215. // ticks must be in order of increasing value.
  216. if (userTicks.length) {
  217. // ticks could be 1D or 2D array of [val, val, ,,,] or [[val, label], [val, label], ...] or mixed
  218. for (i=0; i<userTicks.length; i++){
  219. var ut = userTicks[i];
  220. var t = new this.tickRenderer(this.tickOptions);
  221. if (ut.constructor == Array) {
  222. t.value = ut[0];
  223. t.label = ut[1];
  224. if (!this.showTicks) {
  225. t.showLabel = false;
  226. t.showMark = false;
  227. }
  228. else if (!this.showTickMarks) {
  229. t.showMark = false;
  230. }
  231. t.setTick(ut[0], this.name);
  232. this._ticks.push(t);
  233. }
  234. else {
  235. t.value = ut;
  236. if (!this.showTicks) {
  237. t.showLabel = false;
  238. t.showMark = false;
  239. }
  240. else if (!this.showTickMarks) {
  241. t.showMark = false;
  242. }
  243. t.setTick(ut, this.name);
  244. this._ticks.push(t);
  245. }
  246. }
  247. this.numberTicks = userTicks.length;
  248. this.min = this._ticks[0].value;
  249. this.max = this._ticks[this.numberTicks-1].value;
  250. this.tickInterval = (this.max - this.min) / (this.numberTicks - 1);
  251. }
  252. // we don't have any ticks yet, let's make some!
  253. else {
  254. if (name == 'xaxis' || name == 'x2axis') {
  255. dim = this._plotDimensions.width;
  256. }
  257. else {
  258. dim = this._plotDimensions.height;
  259. }
  260. // if min, max and number of ticks specified, user can't specify interval.
  261. if (this.min != null && this.max != null && this.numberTicks != null) {
  262. this.tickInterval = null;
  263. }
  264. min = (this.min != null) ? this.min : db.min;
  265. max = (this.max != null) ? this.max : db.max;
  266. // if min and max are same, space them out a bit.+
  267. if (min == max) {
  268. var adj = 0.05;
  269. if (min > 0) {
  270. adj = Math.max(Math.log(min)/Math.LN10, 0.05);
  271. }
  272. min -= adj;
  273. max += adj;
  274. }
  275. var range = max - min;
  276. var rmin, rmax;
  277. var temp, prev, curr;
  278. var ynumticks = [3,5,6,11,21];
  279. // yaxis divide ticks in nice intervals from 0 to 1.
  280. if (this.name == 'yaxis' || this.name == 'y2axis') {
  281. this.min = 0;
  282. this.max = 100;
  283. // user didn't specify number of ticks.
  284. if (!this.numberTicks){
  285. if (this.tickInterval) {
  286. this.numberTicks = 3 + Math.ceil(range / this.tickInterval);
  287. }
  288. else {
  289. temp = 2 + Math.ceil((dim-(this.tickSpacing-1))/this.tickSpacing);
  290. for (i=0; i<ynumticks.length; i++) {
  291. curr = temp/ynumticks[i];
  292. if (curr == 1) {
  293. this.numberTicks = ynumticks[i];
  294. break;
  295. }
  296. else if (curr > 1) {
  297. prev = curr;
  298. continue;
  299. }
  300. else if (curr < 1) {
  301. // was prev or is curr closer to one?
  302. if (Math.abs(prev - 1) < Math.abs(curr - 1)) {
  303. this.numberTicks = ynumticks[i-1];
  304. break;
  305. }
  306. else {
  307. this.numberTicks = ynumticks[i];
  308. break;
  309. }
  310. }
  311. else if (i == ynumticks.length -1) {
  312. this.numberTicks = ynumticks[i];
  313. }
  314. }
  315. this.tickInterval = range / (this.numberTicks - 1);
  316. }
  317. }
  318. // user did specify number of ticks.
  319. else {
  320. this.tickInterval = range / (this.numberTicks - 1);
  321. }
  322. for (var i=0; i<this.numberTicks; i++){
  323. tt = this.min + i * this.tickInterval;
  324. t = new this.tickRenderer(this.tickOptions);
  325. // var t = new $.jqplot.AxisTickRenderer(this.tickOptions);
  326. if (!this.showTicks) {
  327. t.showLabel = false;
  328. t.showMark = false;
  329. }
  330. else if (!this.showTickMarks) {
  331. t.showMark = false;
  332. }
  333. t.setTick(tt, this.name);
  334. this._ticks.push(t);
  335. }
  336. }
  337. // for x axes, have number ot ticks equal to number of series and ticks placed
  338. // at sum of y values for each series.
  339. else if (this.tickMode == 'bar') {
  340. this.min = 0;
  341. this.numberTicks = this._series.length + 1;
  342. t = new this.tickRenderer(this.tickOptions);
  343. if (!this.showTicks) {
  344. t.showLabel = false;
  345. t.showMark = false;
  346. }
  347. else if (!this.showTickMarks) {
  348. t.showMark = false;
  349. }
  350. t.setTick(0, this.name);
  351. this._ticks.push(t);
  352. temp = 0;
  353. for (i=1; i<this.numberTicks; i++){
  354. temp += this._series[i-1]._sumy;
  355. t = new this.tickRenderer(this.tickOptions);
  356. if (!this.showTicks) {
  357. t.showLabel = false;
  358. t.showMark = false;
  359. }
  360. else if (!this.showTickMarks) {
  361. t.showMark = false;
  362. }
  363. t.setTick(temp, this.name);
  364. this._ticks.push(t);
  365. }
  366. this.max = this.max || temp;
  367. // if user specified a max and it is greater than sum, add a tick
  368. if (this.max > temp) {
  369. t = new this.tickRenderer(this.tickOptions);
  370. if (!this.showTicks) {
  371. t.showLabel = false;
  372. t.showMark = false;
  373. }
  374. else if (!this.showTickMarks) {
  375. t.showMark = false;
  376. }
  377. t.setTick(this.max, this.name);
  378. this._ticks.push(t);
  379. }
  380. }
  381. else if (this.tickMode == 'even') {
  382. this.min = 0;
  383. this.max = this.max || db.max;
  384. // get a desired number of ticks
  385. var nt = 2 + Math.ceil((dim-(this.tickSpacing-1))/this.tickSpacing);
  386. range = this.max - this.min;
  387. this.numberTicks = nt;
  388. this.tickInterval = range / (this.numberTicks - 1);
  389. for (i=0; i<this.numberTicks; i++){
  390. tt = this.min + i * this.tickInterval;
  391. t = new this.tickRenderer(this.tickOptions);
  392. // var t = new $.jqplot.AxisTickRenderer(this.tickOptions);
  393. if (!this.showTicks) {
  394. t.showLabel = false;
  395. t.showMark = false;
  396. }
  397. else if (!this.showTickMarks) {
  398. t.showMark = false;
  399. }
  400. t.setTick(tt, this.name);
  401. this._ticks.push(t);
  402. }
  403. }
  404. }
  405. };
  406. // called with scope of axis
  407. $.jqplot.MekkoAxisRenderer.prototype.pack = function(pos, offsets) {
  408. var ticks = this._ticks;
  409. var max = this.max;
  410. var min = this.min;
  411. var offmax = offsets.max;
  412. var offmin = offsets.min;
  413. var lshow = (this._label == null) ? false : this._label.show;
  414. for (var p in pos) {
  415. this._elem.css(p, pos[p]);
  416. }
  417. this._offsets = offsets;
  418. // pixellength will be + for x axes and - for y axes becasue pixels always measured from top left.
  419. var pixellength = offmax - offmin;
  420. var unitlength = max - min;
  421. // point to unit and unit to point conversions references to Plot DOM element top left corner.
  422. this.p2u = function(p){
  423. return (p - offmin) * unitlength / pixellength + min;
  424. };
  425. this.u2p = function(u){
  426. return (u - min) * pixellength / unitlength + offmin;
  427. };
  428. if (this.name == 'xaxis' || this.name == 'x2axis'){
  429. this.series_u2p = function(u){
  430. return (u - min) * pixellength / unitlength;
  431. };
  432. this.series_p2u = function(p){
  433. return p * unitlength / pixellength + min;
  434. };
  435. }
  436. else {
  437. this.series_u2p = function(u){
  438. return (u - max) * pixellength / unitlength;
  439. };
  440. this.series_p2u = function(p){
  441. return p * unitlength / pixellength + max;
  442. };
  443. }
  444. if (this.show) {
  445. if (this.name == 'xaxis' || this.name == 'x2axis') {
  446. for (var i=0; i<ticks.length; i++) {
  447. var t = ticks[i];
  448. if (t.show && t.showLabel) {
  449. var shim;
  450. if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
  451. // will need to adjust auto positioning based on which axis this is.
  452. var temp = (this.name == 'xaxis') ? 1 : -1;
  453. switch (t.labelPosition) {
  454. case 'auto':
  455. // position at end
  456. if (temp * t.angle < 0) {
  457. shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  458. }
  459. // position at start
  460. else {
  461. shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
  462. }
  463. break;
  464. case 'end':
  465. shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  466. break;
  467. case 'start':
  468. shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
  469. break;
  470. case 'middle':
  471. shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  472. break;
  473. default:
  474. shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
  475. break;
  476. }
  477. }
  478. else {
  479. shim = -t.getWidth()/2;
  480. }
  481. var val = this.u2p(t.value) + shim + 'px';
  482. t._elem.css('left', val);
  483. t.pack();
  484. }
  485. }
  486. var w;
  487. if (lshow) {
  488. w = this._label._elem.outerWidth(true);
  489. this._label._elem.css('left', offmin + pixellength/2 - w/2 + 'px');
  490. if (this.name == 'xaxis') {
  491. this._label._elem.css('bottom', '0px');
  492. }
  493. else {
  494. this._label._elem.css('top', '0px');
  495. }
  496. this._label.pack();
  497. }
  498. // now show the labels under the bars.
  499. var b, l, r;
  500. for (var i=0; i<this.barLabels.length; i++) {
  501. b = this._barLabels[i];
  502. if (b.show) {
  503. w = b.getWidth();
  504. l = this._ticks[i].getLeft() + this._ticks[i].getWidth();
  505. r = this._ticks[i+1].getLeft();
  506. b._elem.css('left', (r+l-w)/2+'px');
  507. b._elem.css('top', this._ticks[i]._elem.css('top'));
  508. b.pack();
  509. }
  510. }
  511. }
  512. else {
  513. for (var i=0; i<ticks.length; i++) {
  514. var t = ticks[i];
  515. if (t.show && t.showLabel) {
  516. var shim;
  517. if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
  518. // will need to adjust auto positioning based on which axis this is.
  519. var temp = (this.name == 'yaxis') ? 1 : -1;
  520. switch (t.labelPosition) {
  521. case 'auto':
  522. // position at end
  523. case 'end':
  524. if (temp * t.angle < 0) {
  525. shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
  526. }
  527. else {
  528. shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
  529. }
  530. break;
  531. case 'start':
  532. if (t.angle > 0) {
  533. shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
  534. }
  535. else {
  536. shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
  537. }
  538. break;
  539. case 'middle':
  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. 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. }
  561. else {
  562. this._label._elem.css('right', '0px');
  563. }
  564. this._label.pack();
  565. }
  566. }
  567. }
  568. };
  569. })(jQuery);