jqplot.linePattern.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: @VERSION
  6. * Revision: @REVISION
  7. *
  8. * Copyright (c) 2009-2013 Chris Leonello
  9. * jqPlot is currently available for use in all personal or commercial projects
  10. * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
  11. * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
  12. * choose the license that best suits your project and use it accordingly.
  13. *
  14. * Although not required, the author would appreciate an email letting him
  15. * know of any substantial use of jqPlot. You can reach the author at:
  16. * chris at jqplot dot com or see http://www.jqplot.com/info.php .
  17. *
  18. * If you are feeling kind and generous, consider supporting the project by
  19. * making a donation at: http://www.jqplot.com/donate.php .
  20. *
  21. * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
  22. *
  23. * version 2007.04.27
  24. * author Ash Searle
  25. * http://hexmen.com/blog/2007/03/printf-sprintf/
  26. * http://hexmen.com/js/sprintf.js
  27. * The author (Ash Searle) has placed this code in the public domain:
  28. * "This code is unrestricted: you are free to use it however you like."
  29. *
  30. */
  31. /**
  32. * The following dashed line support contributed by Cory Sharp.
  33. * After I implemented an inferior method, Cory responded with a generous
  34. * contribution of code and input which proved a more powerful and
  35. * elegant solution.
  36. */
  37. (function($) {
  38. var dotlen = 0.1;
  39. $.jqplot.LinePattern = function (ctx, pattern) {
  40. var defaultLinePatterns = {
  41. dotted: [ dotlen, $.jqplot.config.dotGapLength ],
  42. dashed: [ $.jqplot.config.dashLength, $.jqplot.config.gapLength ],
  43. solid: null
  44. };
  45. if (typeof pattern === 'string') {
  46. if (pattern[0] === '.' || pattern[0] === '-') {
  47. var s = pattern;
  48. pattern = [];
  49. for (var i=0, imax=s.length; i<imax; i++) {
  50. if (s[i] === '.') {
  51. pattern.push( dotlen );
  52. }
  53. else if (s[i] === '-') {
  54. pattern.push( $.jqplot.config.dashLength );
  55. }
  56. else {
  57. continue;
  58. }
  59. pattern.push( $.jqplot.config.gapLength );
  60. }
  61. }
  62. else {
  63. pattern = defaultLinePatterns[pattern];
  64. }
  65. }
  66. if (!(pattern && pattern.length)) {
  67. return ctx;
  68. }
  69. var patternIndex = 0;
  70. var patternDistance = pattern[0];
  71. var px = 0;
  72. var py = 0;
  73. var pathx0 = 0;
  74. var pathy0 = 0;
  75. var moveTo = function (x, y) {
  76. ctx.moveTo( x, y );
  77. px = x;
  78. py = y;
  79. pathx0 = x;
  80. pathy0 = y;
  81. };
  82. var lineTo = function (x, y) {
  83. var scale = ctx.lineWidth;
  84. var dx = x - px;
  85. var dy = y - py;
  86. var dist = Math.sqrt(dx*dx+dy*dy);
  87. if ((dist > 0) && (scale > 0)) {
  88. dx /= dist;
  89. dy /= dist;
  90. while (true) {
  91. var dp = scale * patternDistance;
  92. if (dp < dist) {
  93. px += dp * dx;
  94. py += dp * dy;
  95. if ((patternIndex & 1) == 0) {
  96. ctx.lineTo( px, py );
  97. }
  98. else {
  99. ctx.moveTo( px, py );
  100. }
  101. dist -= dp;
  102. patternIndex++;
  103. if (patternIndex >= pattern.length) {
  104. patternIndex = 0;
  105. }
  106. patternDistance = pattern[patternIndex];
  107. }
  108. else {
  109. px = x;
  110. py = y;
  111. if ((patternIndex & 1) == 0) {
  112. ctx.lineTo( px, py );
  113. }
  114. else {
  115. ctx.moveTo( px, py );
  116. }
  117. patternDistance -= dist / scale;
  118. break;
  119. }
  120. }
  121. }
  122. };
  123. var beginPath = function () {
  124. ctx.beginPath();
  125. };
  126. var closePath = function () {
  127. lineTo( pathx0, pathy0 );
  128. };
  129. return {
  130. moveTo: moveTo,
  131. lineTo: lineTo,
  132. beginPath: beginPath,
  133. closePath: closePath
  134. };
  135. };
  136. })(jQuery);