SignalTimingChart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <div class="signal-timing-widget">
  3. <div class="header">
  4. <div class="title-area">
  5. <span class="main-title">方案状态</span>
  6. <span class="sub-info" v-if="!loading">(周期: {{ cycleLength }} 相位差: 协调时间: 0)</span>
  7. </div>
  8. <div class="checkbox-area">
  9. <div class="checkbox-mock" :class="{ 'is-checked': followPhase }" @click="followPhase = !followPhase">
  10. <span v-if="followPhase" style="color: #fff; font-size: 12px; margin-left: 1px;">✓</span>
  11. </div>
  12. <span>跟随相位</span>
  13. </div>
  14. </div>
  15. <div v-if="loading" class="loading-overlay">
  16. <span>数据加载中...</span>
  17. </div>
  18. <div v-show="!loading" ref="chartDom" class="chart-container"></div>
  19. </div>
  20. </template>
  21. <script>
  22. import * as echarts from 'echarts';
  23. // 静态资源与常量
  24. const COLORS = {
  25. GREEN_LIGHT: '#8dc453', GREEN_DARK: '#73a542', YELLOW: '#fbd249', RED: '#ff7575', STRIPE_GREEN: '#a3d76e',
  26. TEXT_DARK: '#1e2638', AXIS_LINE: '#758599', DIVIDER_LINE: '#111827', MARK_BLUE: '#4da8ff', TEXT_LIGHT: '#d1d5db'
  27. };
  28. const stripeCanvas = document.createElement('canvas');
  29. stripeCanvas.width = 6; stripeCanvas.height = 20;
  30. const ctx = stripeCanvas.getContext('2d');
  31. ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, 6, 20);
  32. ctx.fillStyle = COLORS.STRIPE_GREEN; ctx.fillRect(0, 0, 3, 20);
  33. const stripePattern = { image: stripeCanvas, repeat: 'repeat' };
  34. const ICON_PATHS = {
  35. UP: 'M10 2 H14 V14 H20 L12 22 L4 14 H10 Z',
  36. DOWN: 'M10 22 H14 V10 H20 L12 2 L4 10 H10 Z',
  37. TURN_LEFT: 'M 21 22 H 15 V 14 C 15 11 13 9 10 9 H 8 V 14 L 0 7 L 8 0 V 5 H 10 C 15 5 21 9 21 14 V 22 Z',
  38. TURN_RIGHT: 'M 3 22 H 9 V 14 C 9 11 11 9 14 9 H 16 V 14 L 24 7 L 16 0 V 5 H 14 C 9 5 3 9 3 14 V 22 Z',
  39. UTURN: 'M 18 22 V 10 C 18 5 15 2 12 2 C 9 2 6 5 6 10 V 14 H 0 L 8 22 L 16 14 H 10 V 10 C 10 7 11 6 12 6 C 13 6 14 7 14 10 V 22 H 18 Z'
  40. };
  41. export default {
  42. name: 'SignalTimingChart',
  43. // 核心改变:从外部接收所有数据
  44. props: {
  45. loading: {
  46. type: Boolean,
  47. default: false
  48. },
  49. cycleLength: {
  50. type: Number,
  51. default: 0
  52. },
  53. currentTime: {
  54. type: Number,
  55. default: 0
  56. },
  57. phaseData: {
  58. type: Array,
  59. default: () => []
  60. }
  61. },
  62. data() {
  63. return {
  64. chartInstance: null,
  65. followPhase: false
  66. };
  67. },
  68. mounted() {
  69. this.initChart();
  70. window.addEventListener('resize', this.handleResize);
  71. // 监听容器尺寸变化,自适应 resize(用 rAF 防抖避免循环)
  72. this._resizePending = false;
  73. this._resizeObserver = new ResizeObserver(() => {
  74. if (!this._resizePending) {
  75. this._resizePending = true;
  76. requestAnimationFrame(() => {
  77. this._resizePending = false;
  78. this.handleResize();
  79. });
  80. }
  81. });
  82. if (this.$refs.chartDom) {
  83. this._resizeObserver.observe(this.$refs.chartDom);
  84. }
  85. },
  86. beforeDestroy() {
  87. window.removeEventListener('resize', this.handleResize);
  88. if (this._resizeObserver) {
  89. this._resizeObserver.disconnect();
  90. }
  91. if (this.chartInstance) {
  92. this.chartInstance.dispose();
  93. this.chartInstance = null;
  94. }
  95. },
  96. watch: {
  97. // 监听 props 变化,一旦父组件传入新数据,立即触发 ECharts 重绘
  98. currentTime() {
  99. if (!this.loading && this.chartInstance) {
  100. this.updateChart();
  101. }
  102. },
  103. phaseData: {
  104. deep: true,
  105. handler(newVal) {
  106. if (!this.loading && this.chartInstance && newVal.length > 0) {
  107. this.updateChart();
  108. }
  109. }
  110. },
  111. loading(newVal) {
  112. // 当 loading 结束时,确保图表进行一次渲染
  113. if (!newVal && this.phaseData.length > 0) {
  114. this.$nextTick(() => {
  115. if (!this.chartInstance) {
  116. this.initChart();
  117. } else {
  118. this.chartInstance.resize();
  119. }
  120. this.updateChart();
  121. });
  122. }
  123. }
  124. },
  125. methods: {
  126. handleResize() {
  127. if (this.chartInstance) {
  128. this.chartInstance.resize();
  129. }
  130. },
  131. initChart() {
  132. const chartDom = this.$refs.chartDom;
  133. if (!chartDom) return;
  134. this.chartInstance = echarts.init(chartDom);
  135. // 如果初始化时就已经有数据了,直接渲染
  136. if (this.phaseData.length > 0) {
  137. this.updateChart();
  138. }
  139. },
  140. updateChart() {
  141. if (!this.chartInstance) return;
  142. this.chartInstance.setOption(this.getChartOption(), true);
  143. },
  144. getChartOption() {
  145. return {
  146. backgroundColor: 'transparent',
  147. grid: { left: 10, right: 10, top: 50, bottom: 10, containLabel: false },
  148. xAxis: { type: 'value', min: 0, max: this.cycleLength, show: false },
  149. yAxis: { type: 'category', data: ['Track 0', 'Track 1'], inverse: true, show: false },
  150. series: [
  151. {
  152. type: 'custom',
  153. renderItem: this.renderCustomItem,
  154. encode: { x: [1, 2], y: 0 },
  155. data: this.phaseData,
  156. markLine: {
  157. symbol: ['none', 'none'],
  158. silent: true,
  159. label: {
  160. show: true,
  161. position: 'start',
  162. formatter: `${this.currentTime}/${this.cycleLength}`,
  163. color: '#fff',
  164. backgroundColor: COLORS.MARK_BLUE,
  165. padding: [4, 8],
  166. borderRadius: 2,
  167. fontSize: 12,
  168. fontWeight: 'bold',
  169. offset: [0, -2]
  170. },
  171. lineStyle: { color: COLORS.MARK_BLUE, type: 'solid', width: 2 },
  172. data: [ { xAxis: this.currentTime } ]
  173. }
  174. }
  175. ]
  176. };
  177. },
  178. renderCustomItem(params, api) {
  179. const trackIndex = api.value(0);
  180. const start = api.coord([api.value(1), trackIndex]);
  181. const end = api.coord([api.value(2), trackIndex]);
  182. const blockHeight = api.size([0, 1])[1];
  183. const yPos = start[1] - blockHeight / 2;
  184. const blockWidth = end[0] - start[0];
  185. const dividerY = (api.coord([0, 1])[1] + api.coord([0, 0])[1]) / 2;
  186. const phaseName = api.value(3);
  187. const duration = api.value(4);
  188. const type = api.value(5);
  189. const iconKey = api.value(6);
  190. let fillStyle = COLORS.GREEN_LIGHT;
  191. if (type === 'stripe') fillStyle = stripePattern;
  192. else if (type === 'yellow') fillStyle = COLORS.YELLOW;
  193. else if (type === 'red') fillStyle = COLORS.RED;
  194. const rectShape = echarts.graphic.clipRectByRect(
  195. { x: start[0], y: yPos, width: blockWidth, height: blockHeight },
  196. { x: params.coordSys.x, y: params.coordSys.y, width: params.coordSys.width, height: params.coordSys.height }
  197. );
  198. if (!rectShape) return;
  199. const children = [];
  200. // A. 绘制刻度
  201. if (params.dataIndex === 0) {
  202. const axisBaseY = params.coordSys.y - 20;
  203. [0, 35, 70, 105, 140].forEach(val => {
  204. const x = api.coord([val, 0])[0];
  205. children.push({ type: 'line', shape: { x1: x, y1: axisBaseY - 5, x2: x, y2: axisBaseY + 5 }, style: { stroke: COLORS.AXIS_LINE, lineWidth: 1.5 } });
  206. });
  207. const stages = [ {n:'S1', s:0, e:35}, {n:'S2', s:35, e:70}, {n:'S3', s:70, e:105}, {n:'S4', s:105, e:140} ];
  208. stages.forEach(st => {
  209. const x1 = api.coord([st.s, 0])[0], x2 = api.coord([st.e, 0])[0], midX = (x1 + x2) / 2;
  210. children.push({ type: 'line', shape: { x1: x1, y1: axisBaseY, x2: midX - 14, y2: axisBaseY }, style: { stroke: COLORS.AXIS_LINE, lineWidth: 1.5 } });
  211. children.push({ type: 'line', shape: { x1: midX + 14, y1: axisBaseY, x2: x2, y2: axisBaseY }, style: { stroke: COLORS.AXIS_LINE, lineWidth: 1.5 } });
  212. children.push({ type: 'text', style: { text: st.n, x: midX, y: axisBaseY, fill: COLORS.TEXT_LIGHT, fontSize: 13, align: 'center', verticalAlign: 'middle', fontWeight: 'bold' } });
  213. });
  214. }
  215. // B. 绘制色块底色
  216. children.push({ type: 'rect', shape: rectShape, style: { fill: fillStyle, stroke: 'none' } });
  217. // C. 绘制内部元素
  218. if (type === 'green' && blockWidth > 25) {
  219. const darkWidth = 32;
  220. const midY = yPos + blockHeight / 2;
  221. children.push({ type: 'rect', shape: { x: start[0], y: yPos, width: darkWidth, height: blockHeight }, style: { fill: COLORS.GREEN_DARK } });
  222. children.push({ type: 'polygon', shape: { points: [ [start[0] + darkWidth, midY - 5], [start[0] + darkWidth, midY + 5], [start[0] + darkWidth + 5, midY] ] }, style: { fill: COLORS.GREEN_DARK } });
  223. if (iconKey && ICON_PATHS[iconKey]) {
  224. const iconSize = 20;
  225. const iconX = start[0] + (darkWidth - iconSize) / 2;
  226. const iconY = midY - iconSize / 2;
  227. children.push({
  228. type: 'path',
  229. shape: { pathData: ICON_PATHS[iconKey], x: iconX, y: iconY, width: iconSize, height: iconSize, layout: 'center' },
  230. style: { fill: COLORS.TEXT_DARK, stroke: 'none' }
  231. });
  232. }
  233. children.push({ type: 'text', style: { text: `${phaseName}\n${duration}`, x: start[0] + darkWidth + 12, y: midY, fill: COLORS.TEXT_DARK, fontSize: 12, fontFamily: 'Arial', fontWeight: 'bold', align: 'left', verticalAlign: 'middle' } });
  234. }
  235. // D. 分割线
  236. if (trackIndex === 1) {
  237. children.push({ type: 'line', shape: { x1: start[0], y1: dividerY, x2: end[0], y2: dividerY }, style: { stroke: COLORS.DIVIDER_LINE, lineWidth: 1.5 } });
  238. }
  239. return { type: 'group', children: children };
  240. }
  241. }
  242. };
  243. </script>
  244. <style scoped>
  245. .signal-timing-widget {
  246. width: 100%;
  247. height: 100%;
  248. background-color: transparent;
  249. box-sizing: border-box;
  250. position: relative;
  251. display: flex;
  252. flex-direction: column;
  253. }
  254. .header {
  255. display: flex;
  256. justify-content: space-between;
  257. align-items: center;
  258. margin-bottom: 25px;
  259. color: #e0e6f1;
  260. }
  261. .title-area { font-size: 16px; }
  262. .main-title { font-size: 20px; font-weight: bold; margin-right: 10px; }
  263. .sub-info { font-size: 14px; opacity: 0.8; }
  264. .checkbox-area {
  265. font-size: 14px; display: flex; align-items: center;
  266. cursor: pointer; opacity: 0.7; user-select: none;
  267. }
  268. .checkbox-area:hover { opacity: 1; }
  269. .checkbox-mock {
  270. width: 14px; height: 14px; border: 1px solid rgba(255, 255, 255, 0.5);
  271. margin-right: 6px; border-radius: 2px;
  272. display: flex; align-items: center; justify-content: center;
  273. }
  274. .checkbox-mock.is-checked { background-color: #4da8ff; border-color: #4da8ff; }
  275. .chart-container { width: 100%; flex: 1; min-height: 120px; }
  276. .loading-overlay {
  277. flex: 1; min-height: 120px; display: flex; align-items: center; justify-content: center;
  278. color: #758599; font-size: 14px;
  279. }
  280. </style>