jquery.flot.image.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Flot plugin for plotting images.
  2. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3. Licensed under the MIT license.
  4. The data syntax is [ [ image, x1, y1, x2, y2 ], ... ] where (x1, y1) and
  5. (x2, y2) are where you intend the two opposite corners of the image to end up
  6. in the plot. Image must be a fully loaded Javascript image (you can make one
  7. with new Image()). If the image is not complete, it's skipped when plotting.
  8. There are two helpers included for retrieving images. The easiest work the way
  9. that you put in URLs instead of images in the data, like this:
  10. [ "myimage.png", 0, 0, 10, 10 ]
  11. Then call $.plot.image.loadData( data, options, callback ) where data and
  12. options are the same as you pass in to $.plot. This loads the images, replaces
  13. the URLs in the data with the corresponding images and calls "callback" when
  14. all images are loaded (or failed loading). In the callback, you can then call
  15. $.plot with the data set. See the included example.
  16. A more low-level helper, $.plot.image.load(urls, callback) is also included.
  17. Given a list of URLs, it calls callback with an object mapping from URL to
  18. Image object when all images are loaded or have failed loading.
  19. The plugin supports these options:
  20. series: {
  21. images: {
  22. show: boolean
  23. anchor: "corner" or "center"
  24. alpha: [ 0, 1 ]
  25. }
  26. }
  27. They can be specified for a specific series:
  28. $.plot( $("#placeholder"), [{
  29. data: [ ... ],
  30. images: { ... }
  31. ])
  32. Note that because the data format is different from usual data points, you
  33. can't use images with anything else in a specific data series.
  34. Setting "anchor" to "center" causes the pixels in the image to be anchored at
  35. the corner pixel centers inside of at the pixel corners, effectively letting
  36. half a pixel stick out to each side in the plot.
  37. A possible future direction could be support for tiling for large images (like
  38. Google Maps).
  39. */
  40. (function ($) {
  41. var options = {
  42. series: {
  43. images: {
  44. show: false,
  45. alpha: 1,
  46. anchor: "corner" // or "center"
  47. }
  48. }
  49. };
  50. $.plot.image = {};
  51. $.plot.image.loadDataImages = function (series, options, callback) {
  52. var urls = [], points = [];
  53. var defaultShow = options.series.images.show;
  54. $.each(series, function (i, s) {
  55. if (!(defaultShow || s.images.show))
  56. return;
  57. if (s.data)
  58. s = s.data;
  59. $.each(s, function (i, p) {
  60. if (typeof p[0] == "string") {
  61. urls.push(p[0]);
  62. points.push(p);
  63. }
  64. });
  65. });
  66. $.plot.image.load(urls, function (loadedImages) {
  67. $.each(points, function (i, p) {
  68. var url = p[0];
  69. if (loadedImages[url])
  70. p[0] = loadedImages[url];
  71. });
  72. callback();
  73. });
  74. }
  75. $.plot.image.load = function (urls, callback) {
  76. var missing = urls.length, loaded = {};
  77. if (missing == 0)
  78. callback({});
  79. $.each(urls, function (i, url) {
  80. var handler = function () {
  81. --missing;
  82. loaded[url] = this;
  83. if (missing == 0)
  84. callback(loaded);
  85. };
  86. $('<img />').load(handler).error(handler).attr('src', url);
  87. });
  88. };
  89. function drawSeries(plot, ctx, series) {
  90. var plotOffset = plot.getPlotOffset();
  91. if (!series.images || !series.images.show)
  92. return;
  93. var points = series.datapoints.points,
  94. ps = series.datapoints.pointsize;
  95. for (var i = 0; i < points.length; i += ps) {
  96. var img = points[i],
  97. x1 = points[i + 1], y1 = points[i + 2],
  98. x2 = points[i + 3], y2 = points[i + 4],
  99. xaxis = series.xaxis, yaxis = series.yaxis,
  100. tmp;
  101. // actually we should check img.complete, but it
  102. // appears to be a somewhat unreliable indicator in
  103. // IE6 (false even after load event)
  104. if (!img || img.width <= 0 || img.height <= 0)
  105. continue;
  106. if (x1 > x2) {
  107. tmp = x2;
  108. x2 = x1;
  109. x1 = tmp;
  110. }
  111. if (y1 > y2) {
  112. tmp = y2;
  113. y2 = y1;
  114. y1 = tmp;
  115. }
  116. // if the anchor is at the center of the pixel, expand the
  117. // image by 1/2 pixel in each direction
  118. if (series.images.anchor == "center") {
  119. tmp = 0.5 * (x2-x1) / (img.width - 1);
  120. x1 -= tmp;
  121. x2 += tmp;
  122. tmp = 0.5 * (y2-y1) / (img.height - 1);
  123. y1 -= tmp;
  124. y2 += tmp;
  125. }
  126. // clip
  127. if (x1 == x2 || y1 == y2 ||
  128. x1 >= xaxis.max || x2 <= xaxis.min ||
  129. y1 >= yaxis.max || y2 <= yaxis.min)
  130. continue;
  131. var sx1 = 0, sy1 = 0, sx2 = img.width, sy2 = img.height;
  132. if (x1 < xaxis.min) {
  133. sx1 += (sx2 - sx1) * (xaxis.min - x1) / (x2 - x1);
  134. x1 = xaxis.min;
  135. }
  136. if (x2 > xaxis.max) {
  137. sx2 += (sx2 - sx1) * (xaxis.max - x2) / (x2 - x1);
  138. x2 = xaxis.max;
  139. }
  140. if (y1 < yaxis.min) {
  141. sy2 += (sy1 - sy2) * (yaxis.min - y1) / (y2 - y1);
  142. y1 = yaxis.min;
  143. }
  144. if (y2 > yaxis.max) {
  145. sy1 += (sy1 - sy2) * (yaxis.max - y2) / (y2 - y1);
  146. y2 = yaxis.max;
  147. }
  148. x1 = xaxis.p2c(x1);
  149. x2 = xaxis.p2c(x2);
  150. y1 = yaxis.p2c(y1);
  151. y2 = yaxis.p2c(y2);
  152. // the transformation may have swapped us
  153. if (x1 > x2) {
  154. tmp = x2;
  155. x2 = x1;
  156. x1 = tmp;
  157. }
  158. if (y1 > y2) {
  159. tmp = y2;
  160. y2 = y1;
  161. y1 = tmp;
  162. }
  163. tmp = ctx.globalAlpha;
  164. ctx.globalAlpha *= series.images.alpha;
  165. ctx.drawImage(img,
  166. sx1, sy1, sx2 - sx1, sy2 - sy1,
  167. x1 + plotOffset.left, y1 + plotOffset.top,
  168. x2 - x1, y2 - y1);
  169. ctx.globalAlpha = tmp;
  170. }
  171. }
  172. function processRawData(plot, series, data, datapoints) {
  173. if (!series.images.show)
  174. return;
  175. // format is Image, x1, y1, x2, y2 (opposite corners)
  176. datapoints.format = [
  177. { required: true },
  178. { x: true, number: true, required: true },
  179. { y: true, number: true, required: true },
  180. { x: true, number: true, required: true },
  181. { y: true, number: true, required: true }
  182. ];
  183. }
  184. function init(plot) {
  185. plot.hooks.processRawData.push(processRawData);
  186. plot.hooks.drawSeries.push(drawSeries);
  187. }
  188. $.plot.plugins.push({
  189. init: init,
  190. options: options,
  191. name: 'image',
  192. version: '1.1'
  193. });
  194. })(jQuery);