Theme Inspinia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.flot.symbol.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Flot plugin that adds some extra symbols for plotting points.
  2. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3. Licensed under the MIT license.
  4. The symbols are accessed as strings through the standard symbol options:
  5. series: {
  6. points: {
  7. symbol: "square" // or "diamond", "triangle", "cross"
  8. }
  9. }
  10. */
  11. (function ($) {
  12. function processRawData(plot, series, datapoints) {
  13. // we normalize the area of each symbol so it is approximately the
  14. // same as a circle of the given radius
  15. var handlers = {
  16. square: function (ctx, x, y, radius, shadow) {
  17. // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
  18. var size = radius * Math.sqrt(Math.PI) / 2;
  19. ctx.rect(x - size, y - size, size + size, size + size);
  20. },
  21. diamond: function (ctx, x, y, radius, shadow) {
  22. // pi * r^2 = 2s^2 => s = r * sqrt(pi/2)
  23. var size = radius * Math.sqrt(Math.PI / 2);
  24. ctx.moveTo(x - size, y);
  25. ctx.lineTo(x, y - size);
  26. ctx.lineTo(x + size, y);
  27. ctx.lineTo(x, y + size);
  28. ctx.lineTo(x - size, y);
  29. },
  30. triangle: function (ctx, x, y, radius, shadow) {
  31. // pi * r^2 = 1/2 * s^2 * sin (pi / 3) => s = r * sqrt(2 * pi / sin(pi / 3))
  32. var size = radius * Math.sqrt(2 * Math.PI / Math.sin(Math.PI / 3));
  33. var height = size * Math.sin(Math.PI / 3);
  34. ctx.moveTo(x - size/2, y + height/2);
  35. ctx.lineTo(x + size/2, y + height/2);
  36. if (!shadow) {
  37. ctx.lineTo(x, y - height/2);
  38. ctx.lineTo(x - size/2, y + height/2);
  39. }
  40. },
  41. cross: function (ctx, x, y, radius, shadow) {
  42. // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
  43. var size = radius * Math.sqrt(Math.PI) / 2;
  44. ctx.moveTo(x - size, y - size);
  45. ctx.lineTo(x + size, y + size);
  46. ctx.moveTo(x - size, y + size);
  47. ctx.lineTo(x + size, y - size);
  48. }
  49. };
  50. var s = series.points.symbol;
  51. if (handlers[s])
  52. series.points.symbol = handlers[s];
  53. }
  54. function init(plot) {
  55. plot.hooks.processDatapoints.push(processRawData);
  56. }
  57. $.plot.plugins.push({
  58. init: init,
  59. name: 'symbols',
  60. version: '1.0'
  61. });
  62. })(jQuery);