Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

312 lignes
14 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.BezierCurveRenderer.js
  32. // Renderer which draws lines as stacked bezier curves.
  33. // Data for the line will not be specified as an array of
  34. // [x, y] data point values, but as a an array of [start piont, bezier curve]
  35. // So, the line is specified as: [[xstart, ystart], [cp1x, cp1y, cp2x, cp2y, xend, yend]].
  36. $.jqplot.BezierCurveRenderer = function(){
  37. $.jqplot.LineRenderer.call(this);
  38. };
  39. $.jqplot.BezierCurveRenderer.prototype = new $.jqplot.LineRenderer();
  40. $.jqplot.BezierCurveRenderer.prototype.constructor = $.jqplot.BezierCurveRenderer;
  41. // Method: setGridData
  42. // converts the user data values to grid coordinates and stores them
  43. // in the gridData array.
  44. // Called with scope of a series.
  45. $.jqplot.BezierCurveRenderer.prototype.setGridData = function(plot) {
  46. // recalculate the grid data
  47. var xp = this._xaxis.series_u2p;
  48. var yp = this._yaxis.series_u2p;
  49. // this._plotData should be same as this.data
  50. var data = this.data;
  51. this.gridData = [];
  52. this._prevGridData = [];
  53. // if seriesIndex = 0, fill to x axis.
  54. // if seriesIndex > 0, fill to previous series data.
  55. var idx = this.index;
  56. if (data.length == 2) {
  57. if (idx == 0) {
  58. this.gridData = [
  59. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])],
  60. [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]),
  61. xp.call(this._xaxis, data[1][2]), yp.call(this._yaxis, data[1][3]),
  62. xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, data[1][5])],
  63. [xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, this._yaxis.min)],
  64. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, this._yaxis.min)]
  65. ];
  66. }
  67. else {
  68. var psd = plot.series[idx-1].data;
  69. this.gridData = [
  70. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])],
  71. [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]),
  72. xp.call(this._xaxis, data[1][2]), yp.call(this._yaxis, data[1][3]),
  73. xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, data[1][5])],
  74. [xp.call(this._xaxis, psd[1][4]), yp.call(this._yaxis, psd[1][5])],
  75. [xp.call(this._xaxis, psd[1][2]), yp.call(this._yaxis, psd[1][3]),
  76. xp.call(this._xaxis, psd[1][0]), yp.call(this._yaxis, psd[1][1]),
  77. xp.call(this._xaxis, psd[0][0]), yp.call(this._yaxis, psd[0][1])]
  78. ];
  79. }
  80. }
  81. else {
  82. if (idx == 0) {
  83. this.gridData = [
  84. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])],
  85. [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]),
  86. xp.call(this._xaxis, data[2][0]), yp.call(this._yaxis, data[2][1]),
  87. xp.call(this._xaxis, data[3][0]), yp.call(this._yaxis, data[3][1])],
  88. [xp.call(this._xaxis, data[3][1]), yp.call(this._yaxis, this._yaxis.min)],
  89. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, this._yaxis.min)]
  90. ];
  91. }
  92. else {
  93. var psd = plot.series[idx-1].data;
  94. this.gridData = [
  95. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])],
  96. [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]),
  97. xp.call(this._xaxis, data[2][0]), yp.call(this._yaxis, data[2][1]),
  98. xp.call(this._xaxis, data[3][0]), yp.call(this._yaxis, data[3][1])],
  99. [xp.call(this._xaxis, psd[3][0]), yp.call(this._yaxis, psd[3][1])],
  100. [xp.call(this._xaxis, psd[2][0]), yp.call(this._yaxis, psd[2][1]),
  101. xp.call(this._xaxis, psd[1][0]), yp.call(this._yaxis, psd[1][1]),
  102. xp.call(this._xaxis, psd[0][0]), yp.call(this._yaxis, psd[0][1])]
  103. ];
  104. }
  105. }
  106. };
  107. // Method: makeGridData
  108. // converts any arbitrary data values to grid coordinates and
  109. // returns them. This method exists so that plugins can use a series'
  110. // linerenderer to generate grid data points without overwriting the
  111. // grid data associated with that series.
  112. // Called with scope of a series.
  113. $.jqplot.BezierCurveRenderer.prototype.makeGridData = function(data, plot) {
  114. // recalculate the grid data
  115. var xp = this._xaxis.series_u2p;
  116. var yp = this._yaxis.series_u2p;
  117. var gd = [];
  118. var pgd = [];
  119. // if seriesIndex = 0, fill to x axis.
  120. // if seriesIndex > 0, fill to previous series data.
  121. var idx = this.index;
  122. if (data.length == 2) {
  123. if (idx == 0) {
  124. gd = [
  125. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])],
  126. [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]),
  127. xp.call(this._xaxis, data[1][2]), yp.call(this._yaxis, data[1][3]),
  128. xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, data[1][5])],
  129. [xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, this._yaxis.min)],
  130. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, this._yaxis.min)]
  131. ];
  132. }
  133. else {
  134. var psd = plot.series[idx-1].data;
  135. gd = [
  136. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])],
  137. [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]),
  138. xp.call(this._xaxis, data[1][2]), yp.call(this._yaxis, data[1][3]),
  139. xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, data[1][5])],
  140. [xp.call(this._xaxis, psd[1][4]), yp.call(this._yaxis, psd[1][5])],
  141. [xp.call(this._xaxis, psd[1][2]), yp.call(this._yaxis, psd[1][3]),
  142. xp.call(this._xaxis, psd[1][0]), yp.call(this._yaxis, psd[1][1]),
  143. xp.call(this._xaxis, psd[0][0]), yp.call(this._yaxis, psd[0][1])]
  144. ];
  145. }
  146. }
  147. else {
  148. if (idx == 0) {
  149. gd = [
  150. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])],
  151. [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]),
  152. xp.call(this._xaxis, data[2][0]), yp.call(this._yaxis, data[2][1]),
  153. xp.call(this._xaxis, data[3][0]), yp.call(this._yaxis, data[3][1])],
  154. [xp.call(this._xaxis, data[3][1]), yp.call(this._yaxis, this._yaxis.min)],
  155. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, this._yaxis.min)]
  156. ];
  157. }
  158. else {
  159. var psd = plot.series[idx-1].data;
  160. gd = [
  161. [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])],
  162. [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]),
  163. xp.call(this._xaxis, data[2][0]), yp.call(this._yaxis, data[2][1]),
  164. xp.call(this._xaxis, data[3][0]), yp.call(this._yaxis, data[3][1])],
  165. [xp.call(this._xaxis, psd[3][0]), yp.call(this._yaxis, psd[3][1])],
  166. [xp.call(this._xaxis, psd[2][0]), yp.call(this._yaxis, psd[2][1]),
  167. xp.call(this._xaxis, psd[1][0]), yp.call(this._yaxis, psd[1][1]),
  168. xp.call(this._xaxis, psd[0][0]), yp.call(this._yaxis, psd[0][1])]
  169. ];
  170. }
  171. }
  172. return gd;
  173. };
  174. // called within scope of series.
  175. $.jqplot.BezierCurveRenderer.prototype.draw = function(ctx, gd, options) {
  176. var i;
  177. ctx.save();
  178. if (gd.length) {
  179. if (this.showLine) {
  180. ctx.save();
  181. var opts = (options != null) ? options : {};
  182. ctx.fillStyle = opts.fillStyle || this.color;
  183. ctx.beginPath();
  184. ctx.moveTo(gd[0][0], gd[0][1]);
  185. ctx.bezierCurveTo(gd[1][0], gd[1][1], gd[1][2], gd[1][3], gd[1][4], gd[1][5]);
  186. ctx.lineTo(gd[2][0], gd[2][1]);
  187. if (gd[3].length == 2) {
  188. ctx.lineTo(gd[3][0], gd[3][1]);
  189. }
  190. else {
  191. ctx.bezierCurveTo(gd[3][0], gd[3][1], gd[3][2], gd[3][3], gd[3][4], gd[3][5]);
  192. }
  193. ctx.closePath();
  194. ctx.fill();
  195. ctx.restore();
  196. }
  197. }
  198. ctx.restore();
  199. };
  200. $.jqplot.BezierCurveRenderer.prototype.drawShadow = function(ctx, gd, options) {
  201. // This is a no-op, shadows drawn with lines.
  202. };
  203. $.jqplot.BezierAxisRenderer = function() {
  204. $.jqplot.LinearAxisRenderer.call(this);
  205. };
  206. $.jqplot.BezierAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
  207. $.jqplot.BezierAxisRenderer.prototype.constructor = $.jqplot.BezierAxisRenderer;
  208. // Axes on a plot with Bezier Curves
  209. $.jqplot.BezierAxisRenderer.prototype.init = function(options){
  210. $.extend(true, this, options);
  211. var db = this._dataBounds;
  212. // Go through all the series attached to this axis and find
  213. // the min/max bounds for this axis.
  214. for (var i=0; i<this._series.length; i++) {
  215. var s = this._series[i];
  216. var d = s.data;
  217. if (d.length == 4) {
  218. for (var j=0; j<d.length; j++) {
  219. if (this.name == 'xaxis' || this.name == 'x2axis') {
  220. if (d[j][0] < db.min || db.min == null) {
  221. db.min = d[j][0];
  222. }
  223. if (d[j][0] > db.max || db.max == null) {
  224. db.max = d[j][0];
  225. }
  226. }
  227. else {
  228. if (d[j][1] < db.min || db.min == null) {
  229. db.min = d[j][1];
  230. }
  231. if (d[j][1] > db.max || db.max == null) {
  232. db.max = d[j][1];
  233. }
  234. }
  235. }
  236. }
  237. else {
  238. if (this.name == 'xaxis' || this.name == 'x2axis') {
  239. if (d[0][0] < db.min || db.min == null) {
  240. db.min = d[0][0];
  241. }
  242. if (d[0][0] > db.max || db.max == null) {
  243. db.max = d[0][0];
  244. }
  245. for (var j=0; j<5; j+=2) {
  246. if (d[1][j] < db.min || db.min == null) {
  247. db.min = d[1][j];
  248. }
  249. if (d[1][j] > db.max || db.max == null) {
  250. db.max = d[1][j];
  251. }
  252. }
  253. }
  254. else {
  255. if (d[0][1] < db.min || db.min == null) {
  256. db.min = d[0][1];
  257. }
  258. if (d[0][1] > db.max || db.max == null) {
  259. db.max = d[0][1];
  260. }
  261. for (var j=1; j<6; j+=2) {
  262. if (d[1][j] < db.min || db.min == null) {
  263. db.min = d[1][j];
  264. }
  265. if (d[1][j] > db.max || db.max == null) {
  266. db.max = d[1][j];
  267. }
  268. }
  269. }
  270. }
  271. }
  272. };
  273. // setup default renderers for axes and legend so user doesn't have to
  274. // called with scope of plot
  275. function preInit(target, data, options) {
  276. options = options || {};
  277. options.axesDefaults = $.extend(true, {pad:0}, options.axesDefaults);
  278. options.legend = $.extend(true, {placement:'outside'}, options.legend);
  279. // only set these if there is a pie series
  280. var setopts = false;
  281. if (options.seriesDefaults.renderer == $.jqplot.BezierCurveRenderer) {
  282. setopts = true;
  283. }
  284. else if (options.series) {
  285. for (var i=0; i < options.series.length; i++) {
  286. if (options.series[i].renderer == $.jqplot.BezierCurveRenderer) {
  287. setopts = true;
  288. }
  289. }
  290. }
  291. if (setopts) {
  292. options.axesDefaults.renderer = $.jqplot.BezierAxisRenderer;
  293. }
  294. }
  295. $.jqplot.preInitHooks.push(preInit);
  296. })(jQuery);