Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sparkline.mjs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. export default class Sparkline {
  2. constructor(element, options = {}) {
  3. this.element = element;
  4. this.options = {...Sparkline.options, ...options};
  5. {
  6. this.element.innerHTML = "<canvas></canvas>";
  7. this.canvas = this.element.firstChild;
  8. this.context = this.canvas.getContext("2d");
  9. this.ratio = window.devicePixelRatio || 1;
  10. if (this.options.tooltip) {
  11. this.canvas.style.position = "relative";
  12. this.canvas.addEventListener('mousemove', e => {
  13. const x = e.offsetX || e.layerX || 0;
  14. const delta = ((this.options.width - this.options.dotRadius * 2) / (this._points.length - 1));
  15. const index = minmax(0, Math.round((x - this.options.dotRadius) / delta), this._points.length - 1);
  16. this.canvas.title = this.options.tooltip(this._points[index], index, this._points);
  17. }, false);
  18. }
  19. }
  20. }
  21. get points() {
  22. return this._points;
  23. }
  24. set points(points) {
  25. this.draw(points);
  26. }
  27. static init(element, options) {
  28. return new Sparkline(element, options);
  29. }
  30. static draw(element, points, options) {
  31. const sparkline = new Sparkline(element, options);
  32. sparkline.draw(points);
  33. return sparkline;
  34. }
  35. draw(points = []) {
  36. this._points = points;
  37. this.canvas.width = this.options.width * this.ratio;
  38. this.canvas.style.width = `${this.options.width}px`;
  39. const pxHeight = this.options.height || this.element.offsetHeight;
  40. this.canvas.height = pxHeight * this.ratio;
  41. this.canvas.style.height = `${pxHeight}px`;
  42. const lineWidth = this.options.lineWidth * this.ratio;
  43. const offsetX = Math.max(this.options.dotRadius * this.ratio, lineWidth / 2);
  44. const offsetY = Math.max(this.options.dotRadius * this.ratio, lineWidth / 2);
  45. const width = this.canvas.width - offsetX * 2;
  46. const height = this.canvas.height - offsetY * 2;
  47. const minValue = Math.min.apply(Math, points);
  48. const maxValue = Math.max.apply(Math, points);
  49. const bottomValue = this.options.minValue != undefined ? this.options.minValue : Math.min(minValue, this.options.maxMinValue != undefined ? this.options.maxMinValue : minValue);
  50. const topValue = this.options.maxValue != undefined ? this.options.maxValue : Math.max(maxValue, this.options.minMaxValue != undefined ? this.options.minMaxValue : maxValue);
  51. let minX = offsetX;
  52. let maxX = offsetX;
  53. let x = offsetX;
  54. const y = index => (topValue === bottomValue)
  55. ? offsetY + height / 2
  56. : (offsetY + height) - ((points[index] - bottomValue) / (topValue - bottomValue)) * height;
  57. const delta = width / (points.length - 1);
  58. const line = (style, x, y) => {
  59. if (!style) return;
  60. this.context.save();
  61. this.context.strokeStyle = style.color || 'black';
  62. this.context.lineWidth = (style.width || 1) * this.ratio;
  63. this.context.globalAlpha = style.alpha || 1;
  64. this.context.beginPath();
  65. this.context.moveTo(style.direction != 'right' ? offsetX : x, y);
  66. this.context.lineTo(style.direction != 'left' ? width + offsetX : x, y);
  67. this.context.stroke();
  68. this.context.restore();
  69. }
  70. const dot = (color, lineStyle, x, y) => {
  71. this.context.beginPath();
  72. this.context.fillStyle = color;
  73. this.context.arc(x, y, this.options.dotRadius * this.ratio, 0, Math.PI * 2, false);
  74. this.context.fill();
  75. line(lineStyle, x, y);
  76. }
  77. this.context.save();
  78. this.context.strokeStyle = this.options.lineColor;
  79. this.context.fillStyle = this.options.lineColor;
  80. this.context.lineWidth = lineWidth;
  81. this.context.lineCap = 'round';
  82. this.context.lineJoin = 'round';
  83. if (this.options.fillBelow && points.length > 1) {
  84. this.context.save();
  85. this.context.beginPath();
  86. this.context.moveTo(x, y(0));
  87. for (let i = 1; i < points.length; i++) {
  88. x += delta;
  89. minX = points[i] == minValue ? x : minX;
  90. maxX = points[i] == maxValue ? x : maxX;
  91. this.context.lineTo(x, y(i));
  92. }
  93. this.context.lineTo(width + offsetX, height + offsetY + lineWidth / 2);
  94. this.context.lineTo(offsetX, height + offsetY + lineWidth / 2);
  95. this.context.fill();
  96. if (this.options.fillLighten > 0) {
  97. this.context.fillStyle = 'white';
  98. this.context.globalAlpha = this.options.fillLighten;
  99. this.context.fill();
  100. this.context.globalAlpha = 1;
  101. } else if (this.options.fillLighten < 0) {
  102. this.context.fillStyle = 'black';
  103. this.context.globalAlpha = -this.options.fillLighten;
  104. this.context.fill();
  105. }
  106. this.context.restore();
  107. }
  108. x = offsetX;
  109. this.context.beginPath();
  110. this.context.moveTo(x, y(0));
  111. for (let i = 1; i < points.length; i++) {
  112. x += delta;
  113. this.context.lineTo(x, y(i));
  114. }
  115. this.context.stroke();
  116. this.context.restore();
  117. line(this.options.bottomLine, 0, offsetY);
  118. line(this.options.topLine, 0, height + offsetY + lineWidth / 2);
  119. dot(this.options.startColor, this.options.startLine, offsetX + (points.length == 1 ? width / 2 : 0), y(0));
  120. dot(this.options.endColor, this.options.endLine, offsetX + (points.length == 1 ? width / 2 : width), y(points.length - 1));
  121. dot(this.options.minColor, this.options.minLine, minX + (points.length == 1 ? width / 2 : 0), y(points.indexOf(minValue)));
  122. dot(this.options.maxColor, this.options.maxLine, maxX + (points.length == 1 ? width / 2 : 0), y(points.indexOf(maxValue)));
  123. }
  124. }
  125. Sparkline.options = {
  126. width: 100,
  127. height: null,
  128. lineColor: "black",
  129. lineWidth: 1.5,
  130. startColor: "transparent",
  131. endColor: "black",
  132. maxColor: "transparent",
  133. minColor: "transparent",
  134. minValue: null,
  135. maxValue: null,
  136. minMaxValue: null,
  137. maxMinValue: null,
  138. dotRadius: 2.5,
  139. tooltip: null,
  140. fillBelow: true,
  141. fillLighten: 0.5,
  142. startLine: false,
  143. endLine: false,
  144. minLine: false,
  145. maxLine: false,
  146. bottomLine: false,
  147. topLine: false,
  148. averageLine: false
  149. };
  150. function minmax(a, b, c) {
  151. return Math.max(a, Math.min(b, c));
  152. }