jquery.flot.symbol.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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", "plus", "ellipse", "rectangle"
  8. }
  9. }
  10. */
  11. (function ($) {
  12. // we normalize the area of each symbol so it is approximately the
  13. // same as a circle of the given radius
  14. var square = function (ctx, x, y, radius, shadow) {
  15. // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
  16. var size = radius * Math.sqrt(Math.PI) / 2;
  17. ctx.rect(x - size, y - size, size + size, size + size);
  18. },
  19. rectangle = function (ctx, x, y, radius, shadow) {
  20. // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
  21. var size = radius * Math.sqrt(Math.PI) / 2;
  22. ctx.rect(x - size, y - size, size + size, size + size);
  23. },
  24. diamond = function (ctx, x, y, radius, shadow) {
  25. // pi * r^2 = 2s^2 => s = r * sqrt(pi/2)
  26. var size = radius * Math.sqrt(Math.PI / 2);
  27. ctx.moveTo(x - size, y);
  28. ctx.lineTo(x, y - size);
  29. ctx.lineTo(x + size, y);
  30. ctx.lineTo(x, y + size);
  31. ctx.lineTo(x - size, y);
  32. ctx.lineTo(x, y - size);
  33. },
  34. triangle = function (ctx, x, y, radius, shadow) {
  35. // pi * r^2 = 1/2 * s^2 * sin (pi / 3) => s = r * sqrt(2 * pi / sin(pi / 3))
  36. var size = radius * Math.sqrt(2 * Math.PI / Math.sin(Math.PI / 3));
  37. var height = size * Math.sin(Math.PI / 3);
  38. ctx.moveTo(x - size / 2, y + height / 2);
  39. ctx.lineTo(x + size / 2, y + height / 2);
  40. if (!shadow) {
  41. ctx.lineTo(x, y - height / 2);
  42. ctx.lineTo(x - size / 2, y + height / 2);
  43. ctx.lineTo(x + size / 2, y + height / 2);
  44. }
  45. },
  46. cross = function (ctx, x, y, radius, shadow) {
  47. // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
  48. var size = radius * Math.sqrt(Math.PI) / 2;
  49. ctx.moveTo(x - size, y - size);
  50. ctx.lineTo(x + size, y + size);
  51. ctx.moveTo(x - size, y + size);
  52. ctx.lineTo(x + size, y - size);
  53. },
  54. ellipse = function(ctx, x, y, radius, shadow, fill) {
  55. if (!shadow) {
  56. ctx.moveTo(x + radius, y);
  57. ctx.arc(x, y, radius, 0, Math.PI * 2, false);
  58. }
  59. },
  60. plus = function (ctx, x, y, radius, shadow) {
  61. var size = radius * Math.sqrt(Math.PI / 2);
  62. ctx.moveTo(x - size, y);
  63. ctx.lineTo(x + size, y);
  64. ctx.moveTo(x, y + size);
  65. ctx.lineTo(x, y - size);
  66. },
  67. handlers = {
  68. square: square,
  69. rectangle: rectangle,
  70. diamond: diamond,
  71. triangle: triangle,
  72. cross: cross,
  73. ellipse: ellipse,
  74. plus: plus
  75. };
  76. square.fill = true;
  77. rectangle.fill = true;
  78. diamond.fill = true;
  79. triangle.fill = true;
  80. ellipse.fill = true;
  81. function init(plot) {
  82. plot.drawSymbol = handlers;
  83. }
  84. $.plot.plugins.push({
  85. init: init,
  86. name: 'symbols',
  87. version: '1.0'
  88. });
  89. })(jQuery);