PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/files/jqplot/1.0.0/plugins/jqplot.pointLabels.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 362 lines | 210 code | 25 blank | 127 comment | 58 complexity | af2db87ae156376a654aaed5737a0a48 MD5 | raw file
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: 1.0.0b2_r1012
  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.PointLabels
  33. * Plugin for putting labels at the data points.
  34. *
  35. * To use this plugin, include the js
  36. * file in your source:
  37. *
  38. * > <script type="text/javascript" src="plugins/jqplot.pointLabels.js"></script>
  39. *
  40. * By default, the last value in the data ponit array in the data series is used
  41. * for the label. For most series renderers, extra data can be added to the
  42. * data point arrays and the last value will be used as the label.
  43. *
  44. * For instance,
  45. * this series:
  46. *
  47. * > [[1,4], [3,5], [7,2]]
  48. *
  49. * Would, by default, use the y values in the labels.
  50. * Extra data can be added to the series like so:
  51. *
  52. * > [[1,4,'mid'], [3 5,'hi'], [7,2,'low']]
  53. *
  54. * And now the point labels would be 'mid', 'low', and 'hi'.
  55. *
  56. * Options to the point labels and a custom labels array can be passed into the
  57. * "pointLabels" option on the series option like so:
  58. *
  59. * > series:[{pointLabels:{
  60. * > labels:['mid', 'hi', 'low'],
  61. * > location:'se',
  62. * > ypadding: 12
  63. * > }
  64. * > }]
  65. *
  66. * A custom labels array in the options takes precendence over any labels
  67. * in the series data. If you have a custom labels array in the options,
  68. * but still want to use values from the series array as labels, set the
  69. * "labelsFromSeries" option to true.
  70. *
  71. * By default, html entities (<, >, etc.) are escaped in point labels.
  72. * If you want to include actual html markup in the labels,
  73. * set the "escapeHTML" option to false.
  74. *
  75. */
  76. $.jqplot.PointLabels = function(options) {
  77. // Group: Properties
  78. //
  79. // prop: show
  80. // show the labels or not.
  81. this.show = $.jqplot.config.enablePlugins;
  82. // prop: location
  83. // compass location where to position the label around the point.
  84. // 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'
  85. this.location = 'n';
  86. // prop: labelsFromSeries
  87. // true to use labels within data point arrays.
  88. this.labelsFromSeries = false;
  89. // prop: seriesLabelIndex
  90. // array index for location of labels within data point arrays.
  91. // if null, will use the last element of the data point array.
  92. this.seriesLabelIndex = null;
  93. // prop: labels
  94. // array of arrays of labels, one array for each series.
  95. this.labels = [];
  96. // actual labels that will get displayed.
  97. // needed to preserve user specified labels in labels array.
  98. this._labels = [];
  99. // prop: stackedValue
  100. // true to display value as stacked in a stacked plot.
  101. // no effect if labels is specified.
  102. this.stackedValue = false;
  103. // prop: ypadding
  104. // vertical padding in pixels between point and label
  105. this.ypadding = 6;
  106. // prop: xpadding
  107. // horizontal padding in pixels between point and label
  108. this.xpadding = 6;
  109. // prop: escapeHTML
  110. // true to escape html entities in the labels.
  111. // If you want to include markup in the labels, set to false.
  112. this.escapeHTML = true;
  113. // prop: edgeTolerance
  114. // Number of pixels that the label must be away from an axis
  115. // boundary in order to be drawn. Negative values will allow overlap
  116. // with the grid boundaries.
  117. this.edgeTolerance = -5;
  118. // prop: formatter
  119. // A class of a formatter for the tick text. sprintf by default.
  120. this.formatter = $.jqplot.DefaultTickFormatter;
  121. // prop: formatString
  122. // string passed to the formatter.
  123. this.formatString = '';
  124. // prop: hideZeros
  125. // true to not show a label for a value which is 0.
  126. this.hideZeros = false;
  127. this._elems = [];
  128. $.extend(true, this, options);
  129. };
  130. var locations = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w'];
  131. var locationIndicies = {'nw':0, 'n':1, 'ne':2, 'e':3, 'se':4, 's':5, 'sw':6, 'w':7};
  132. var oppositeLocations = ['se', 's', 'sw', 'w', 'nw', 'n', 'ne', 'e'];
  133. // called with scope of a series
  134. $.jqplot.PointLabels.init = function (target, data, seriesDefaults, opts, plot){
  135. var options = $.extend(true, {}, seriesDefaults, opts);
  136. options.pointLabels = options.pointLabels || {};
  137. if (this.renderer.constructor === $.jqplot.BarRenderer && this.barDirection === 'horizontal' && !options.pointLabels.location) {
  138. options.pointLabels.location = 'e';
  139. }
  140. // add a pointLabels attribute to the series plugins
  141. this.plugins.pointLabels = new $.jqplot.PointLabels(options.pointLabels);
  142. this.plugins.pointLabels.setLabels.call(this);
  143. };
  144. // called with scope of series
  145. $.jqplot.PointLabels.prototype.setLabels = function() {
  146. var p = this.plugins.pointLabels;
  147. var labelIdx;
  148. if (p.seriesLabelIndex != null) {
  149. labelIdx = p.seriesLabelIndex;
  150. }
  151. else if (this.renderer.constructor === $.jqplot.BarRenderer && this.barDirection === 'horizontal') {
  152. labelIdx = 0;
  153. }
  154. else {
  155. labelIdx = (this._plotData.length === 0) ? 0 : this._plotData[0].length -1;
  156. }
  157. p._labels = [];
  158. if (p.labels.length === 0 || p.labelsFromSeries) {
  159. if (p.stackedValue) {
  160. if (this._plotData.length && this._plotData[0].length){
  161. // var idx = p.seriesLabelIndex || this._plotData[0].length -1;
  162. for (var i=0; i<this._plotData.length; i++) {
  163. p._labels.push(this._plotData[i][labelIdx]);
  164. }
  165. }
  166. }
  167. else {
  168. var d = this._plotData;
  169. if (this.renderer.constructor === $.jqplot.BarRenderer && this.waterfall) {
  170. d = this._data;
  171. }
  172. if (d.length && d[0].length) {
  173. // var idx = p.seriesLabelIndex || d[0].length -1;
  174. for (var i=0; i<d.length; i++) {
  175. p._labels.push(d[i][labelIdx]);
  176. }
  177. }
  178. d = null;
  179. }
  180. }
  181. else if (p.labels.length){
  182. p._labels = p.labels;
  183. }
  184. };
  185. $.jqplot.PointLabels.prototype.xOffset = function(elem, location, padding) {
  186. location = location || this.location;
  187. padding = padding || this.xpadding;
  188. var offset;
  189. switch (location) {
  190. case 'nw':
  191. offset = -elem.outerWidth(true) - this.xpadding;
  192. break;
  193. case 'n':
  194. offset = -elem.outerWidth(true)/2;
  195. break;
  196. case 'ne':
  197. offset = this.xpadding;
  198. break;
  199. case 'e':
  200. offset = this.xpadding;
  201. break;
  202. case 'se':
  203. offset = this.xpadding;
  204. break;
  205. case 's':
  206. offset = -elem.outerWidth(true)/2;
  207. break;
  208. case 'sw':
  209. offset = -elem.outerWidth(true) - this.xpadding;
  210. break;
  211. case 'w':
  212. offset = -elem.outerWidth(true) - this.xpadding;
  213. break;
  214. default: // same as 'nw'
  215. offset = -elem.outerWidth(true) - this.xpadding;
  216. break;
  217. }
  218. return offset;
  219. };
  220. $.jqplot.PointLabels.prototype.yOffset = function(elem, location, padding) {
  221. location = location || this.location;
  222. padding = padding || this.xpadding;
  223. var offset;
  224. switch (location) {
  225. case 'nw':
  226. offset = -elem.outerHeight(true) - this.ypadding;
  227. break;
  228. case 'n':
  229. offset = -elem.outerHeight(true) - this.ypadding;
  230. break;
  231. case 'ne':
  232. offset = -elem.outerHeight(true) - this.ypadding;
  233. break;
  234. case 'e':
  235. offset = -elem.outerHeight(true)/2;
  236. break;
  237. case 'se':
  238. offset = this.ypadding;
  239. break;
  240. case 's':
  241. offset = this.ypadding;
  242. break;
  243. case 'sw':
  244. offset = this.ypadding;
  245. break;
  246. case 'w':
  247. offset = -elem.outerHeight(true)/2;
  248. break;
  249. default: // same as 'nw'
  250. offset = -elem.outerHeight(true) - this.ypadding;
  251. break;
  252. }
  253. return offset;
  254. };
  255. // called with scope of series
  256. $.jqplot.PointLabels.draw = function (sctx, options, plot) {
  257. var p = this.plugins.pointLabels;
  258. // set labels again in case they have changed.
  259. p.setLabels.call(this);
  260. // remove any previous labels
  261. for (var i=0; i<p._elems.length; i++) {
  262. // Memory Leaks patch
  263. // p._elems[i].remove();
  264. p._elems[i].emptyForce();
  265. }
  266. p._elems.splice(0, p._elems.length);
  267. if (p.show) {
  268. var ax = '_'+this._stackAxis+'axis';
  269. if (!p.formatString) {
  270. p.formatString = this[ax]._ticks[0].formatString;
  271. p.formatter = this[ax]._ticks[0].formatter;
  272. }
  273. var pd = this._plotData;
  274. var xax = this._xaxis;
  275. var yax = this._yaxis;
  276. var elem, helem;
  277. for (var i=0, l=p._labels.length; i < l; i++) {
  278. var label = p._labels[i];
  279. if (p.hideZeros && parseInt(p._labels[i], 10) == 0) {
  280. label = '';
  281. }
  282. if (label != null) {
  283. label = p.formatter(p.formatString, label);
  284. }
  285. helem = document.createElement('div');
  286. p._elems[i] = $(helem);
  287. elem = p._elems[i];
  288. elem.addClass('jqplot-point-label jqplot-series-'+this.index+' jqplot-point-'+i);
  289. elem.css('position', 'absolute');
  290. elem.insertAfter(sctx.canvas);
  291. if (p.escapeHTML) {
  292. elem.text(label);
  293. }
  294. else {
  295. elem.html(label);
  296. }
  297. var location = p.location;
  298. if ((this.fillToZero && pd[i][1] < 0) || (this.fillToZero && this._type === 'bar' && this.barDirection === 'horizontal' && pd[i][0] < 0) || (this.waterfall && parseInt(label, 10)) < 0) {
  299. location = oppositeLocations[locationIndicies[location]];
  300. }
  301. var ell = xax.u2p(pd[i][0]) + p.xOffset(elem, location);
  302. var elt = yax.u2p(pd[i][1]) + p.yOffset(elem, location);
  303. if (this.renderer.constructor == $.jqplot.BarRenderer) {
  304. if (this.barDirection == "vertical") {
  305. ell += this._barNudge;
  306. }
  307. else {
  308. elt -= this._barNudge;
  309. }
  310. }
  311. elem.css('left', ell);
  312. elem.css('top', elt);
  313. var elr = ell + elem.width();
  314. var elb = elt + elem.height();
  315. var et = p.edgeTolerance;
  316. var scl = $(sctx.canvas).position().left;
  317. var sct = $(sctx.canvas).position().top;
  318. var scr = sctx.canvas.width + scl;
  319. var scb = sctx.canvas.height + sct;
  320. // if label is outside of allowed area, remove it
  321. if (ell - et < scl || elt - et < sct || elr + et > scr || elb + et > scb) {
  322. elem.remove();
  323. }
  324. elem = null;
  325. helem = null;
  326. }
  327. // finally, animate them if the series is animated
  328. // if (this.renderer.animation && this.renderer.animation._supported && this.renderer.animation.show && plot._drawCount < 2) {
  329. // var sel = '.jqplot-point-label.jqplot-series-'+this.index;
  330. // $(sel).hide();
  331. // $(sel).fadeIn(1000);
  332. // }
  333. }
  334. };
  335. $.jqplot.postSeriesInitHooks.push($.jqplot.PointLabels.init);
  336. $.jqplot.postDrawSeriesHooks.push($.jqplot.PointLabels.draw);
  337. })(jQuery);