Graph.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. export class Graph {
  2. constructor( maxPoints = 512 ) {
  3. this.maxPoints = maxPoints;
  4. this.lines = {};
  5. this.limit = 0;
  6. this.limitIndex = 0;
  7. this.domElement = document.createElement( 'canvas' );
  8. this.domElement.setAttribute( 'class', 'graph-canvas' );
  9. this.ctx = this.domElement.getContext( '2d' );
  10. this.width = 0;
  11. this.height = 0;
  12. this.devicePixelRatio = window.devicePixelRatio || 1;
  13. }
  14. resize( width, height ) {
  15. this.width = width;
  16. this.height = height;
  17. this.devicePixelRatio = window.devicePixelRatio || 1;
  18. this.domElement.width = width * this.devicePixelRatio;
  19. this.domElement.height = height * this.devicePixelRatio;
  20. this.draw();
  21. }
  22. addLine( id, color ) {
  23. this.lines[ id ] = {
  24. color: color,
  25. resolved: null,
  26. points: []
  27. };
  28. }
  29. addPoint( lineId, value ) {
  30. const line = this.lines[ lineId ];
  31. if ( ! line ) return;
  32. line.points.push( value );
  33. if ( line.points.length > this.maxPoints ) {
  34. line.points.shift();
  35. }
  36. if ( value > this.limit ) {
  37. this.limit = value;
  38. this.limitIndex = 0;
  39. }
  40. }
  41. resetLimit() {
  42. this.limit = 0;
  43. this.limitIndex = 0;
  44. }
  45. update() {
  46. const width = this.domElement.clientWidth;
  47. const height = this.domElement.clientHeight;
  48. if ( width === 0 || height === 0 ) return;
  49. if ( width !== this.width || height !== this.height ) {
  50. this.resize( width, height );
  51. } else {
  52. this.draw();
  53. }
  54. if ( this.limitIndex ++ > this.maxPoints ) {
  55. this.resetLimit();
  56. }
  57. }
  58. draw() {
  59. const ctx = this.ctx;
  60. const dpr = this.devicePixelRatio;
  61. const width = this.width;
  62. const height = this.height;
  63. ctx.clearRect( 0, 0, width * dpr, height * dpr );
  64. if ( width === 0 || height === 0 ) return;
  65. ctx.save();
  66. ctx.scale( dpr, dpr );
  67. const pointStep = width / ( this.maxPoints - 1 );
  68. for ( const id in this.lines ) {
  69. const line = this.lines[ id ];
  70. if ( line.points.length === 0 ) continue;
  71. if ( ! line.resolved ) {
  72. line.resolved = this._resolveColor( line.color );
  73. }
  74. const resolved = line.resolved;
  75. const drawColor = resolved ? resolved.color : '#ffffff';
  76. const offset = width - ( ( line.points.length - 1 ) * pointStep );
  77. // 1. Draw fill (with opacity)
  78. let fillStyle = drawColor;
  79. if ( height > 0 ) {
  80. const gradient = ctx.createLinearGradient( 0, 0, 0, height );
  81. gradient.addColorStop( 0, drawColor );
  82. gradient.addColorStop( 1, ( resolved && resolved.transparent ) || 'rgba(0,0,0,0)' );
  83. fillStyle = gradient;
  84. }
  85. ctx.fillStyle = fillStyle;
  86. ctx.globalAlpha = 0.4;
  87. ctx.beginPath();
  88. ctx.moveTo( offset, height );
  89. for ( let i = 0; i < line.points.length; i ++ ) {
  90. const x = offset + i * pointStep;
  91. const y = this.limit === 0 ? height : height - ( line.points[ i ] / this.limit ) * height;
  92. ctx.lineTo( x, y );
  93. }
  94. ctx.lineTo( offset + ( line.points.length - 1 ) * pointStep, height );
  95. ctx.closePath();
  96. ctx.fill();
  97. // 2. Draw stroke (full opacity)
  98. ctx.strokeStyle = drawColor;
  99. ctx.lineWidth = 2;
  100. ctx.globalAlpha = 1.0;
  101. ctx.beginPath();
  102. for ( let i = 0; i < line.points.length; i ++ ) {
  103. const x = offset + i * pointStep;
  104. const y = this.limit === 0 ? height : height - ( line.points[ i ] / this.limit ) * height;
  105. if ( i === 0 ) {
  106. ctx.moveTo( x, y );
  107. } else {
  108. ctx.lineTo( x, y );
  109. }
  110. }
  111. ctx.stroke();
  112. }
  113. ctx.restore();
  114. }
  115. _resolveColor( color ) {
  116. let resolved = color;
  117. if ( color.startsWith( 'var(' ) ) {
  118. const varName = color.slice( 4, - 1 ).trim();
  119. resolved = getComputedStyle( this.domElement ).getPropertyValue( varName ).trim();
  120. if ( ! resolved ) {
  121. return null;
  122. }
  123. }
  124. let transparentColor = 'rgba(0,0,0,0)';
  125. if ( resolved.startsWith( '#' ) ) {
  126. const hex = resolved.substring( 0, 7 );
  127. transparentColor = hex + '00';
  128. } else if ( resolved.startsWith( 'rgb' ) ) {
  129. const match = resolved.match( /^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*[\d.]+)?\)$/ );
  130. if ( match ) {
  131. transparentColor = `rgba(${match[ 1 ]}, ${match[ 2 ]}, ${match[ 3 ]}, 0)`;
  132. }
  133. }
  134. return {
  135. color: resolved,
  136. transparent: transparentColor
  137. };
  138. }
  139. dispose() {
  140. }
  141. }
粤ICP备19079148号