1. let draw,
    2. timer,
    3. polylineStart,
    4. polylinePhase1,
    5. polylinePhase2,
    6. polygonHightlight; //高亮区域
    7. const curve = {
    8. width: 273,
    9. height: 180,
    10. d1: 120, //overdrawnPrice虚线->liq.Price虚线距离
    11. d2: 60, //liq.Price虚线->y轴初始位置
    12. scaleY: 1,
    13. scaleX: 273 / 128 / 3,
    14. color: "#FCE6DC",
    15. activeColor: "#EF814F",
    16. lineWidth: 2,
    17. startPointsNum: 6, //起始20s的坐标点个数
    18. gap: 3 //x轴坐标间距
    19. };
    20. onMounted(async () => {
    21. draw = SVG()
    22. .addTo("#price-curve")
    23. .size("100%", "180px");
    24. await fetchPrice(route.params.id);
    25. // 生成多边形构造器
    26. polylineStart = draw.polyline();
    27. polylinePhase1 = draw.polyline();
    28. polylinePhase2 = draw.polyline();
    29. polygonHightlight = draw.polygon();
    30. //每3s获取一次历史价格,每40s渲染一次图像
    31. timer = setInterval(
    32. (() => {
    33. let counter = 0;
    34. return () => {
    35. counter += 40;
    36. if (counter >= 3000) {
    37. fetchPrice(route.params.id);
    38. counter = 0;
    39. }
    40. renderCurve();
    41. };
    42. })(),
    43. 40
    44. );
    45. if (el.value) {
    46. el.value.addEventListener("mouseover", mouseoverHanlder);
    47. el.value.addEventListener("mouseout", mouseoutHandler);
    48. }
    49. });
    50. //鼠标移出事件
    51. function mouseoutHandler() {
    52. state.phase1.isActive = false;
    53. state.phase2.isActive = false;
    54. }
    55. //渲染折线
    56. function renderCurve() {
    57. //start
    58. const startData = state.coordinates.slice(0, curve.startPointsNum);
    59. polylineStart
    60. .plot(startData)
    61. .fill("none")
    62. .stroke({
    63. color: curve.color,
    64. width: curve.lineWidth,
    65. linecap: "round"
    66. });
    67. //phase 1
    68. const phase1Data = state.coordinates.slice(
    69. curve.startPointsNum - 1,
    70. state.phase2Index ? state.phase2Index : state.coordinates.length
    71. );
    72. polylinePhase1
    73. .plot(phase1Data)
    74. .fill("none")
    75. .stroke({
    76. color: state.phase1.color,
    77. width: curve.lineWidth,
    78. linecap: "round"
    79. });
    80. //phase 2
    81. let phase2Data = [];
    82. if (state.phase2Index) {
    83. phase2Data = state.coordinates.slice(state.phase2Index - 1);
    84. polylinePhase2
    85. .plot(phase2Data)
    86. .fill("none")
    87. .stroke({
    88. color: state.phase2.color,
    89. width: curve.lineWidth,
    90. linecap: "round"
    91. });
    92. }
    93. //highlightArea
    94. let hightlightData = [];
    95. if (state.phase1.isActive && phase1Data.length > 0) {
    96. hightlightData = phase1Data;
    97. hightlightData.unshift(phase1Data[0][0], curve.height);
    98. hightlightData.push(phase1Data[phase1Data.length - 1][0], curve.height);
    99. } else if (
    100. state.phase2Index &&
    101. state.phase2.isActive &&
    102. phase2Data.length > 0
    103. ) {
    104. hightlightData = phase2Data;
    105. hightlightData.unshift(phase2Data[0][0], curve.height);
    106. hightlightData.push(phase2Data[phase2Data.length - 1][0], curve.height);
    107. }
    108. polygonHightlight.plot(hightlightData).fill(
    109. draw
    110. .gradient("linear", add => {
    111. add.stop({ offset: 0, color: curve.activeColor, opacity: 0.5 });
    112. add.stop({ offset: 1, color: "#fff" });
    113. })
    114. .from(0, 0)
    115. .to(0, 1)
    116. );
    117. }
    118. onBeforeUnmount(() => {
    119. if (timer) clearInterval(timer);
    120. });