SignalTimingChart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. scaleFactor: 1
  67. };
  68. },
  69. mounted() {
  70. this.updateScale();
  71. this.initChart();
  72. window.addEventListener('resize', this.handleResize);
  73. this._resizePending = false;
  74. this._resizeObserver = new ResizeObserver(() => {
  75. if (!this._resizePending) {
  76. this._resizePending = true;
  77. requestAnimationFrame(() => {
  78. this._resizePending = false;
  79. this.handleResize();
  80. });
  81. }
  82. });
  83. if (this.$refs.chartDom) {
  84. this._resizeObserver.observe(this.$refs.chartDom);
  85. }
  86. },
  87. beforeDestroy() {
  88. window.removeEventListener('resize', this.handleResize);
  89. if (this._resizeObserver) {
  90. this._resizeObserver.disconnect();
  91. }
  92. if (this.chartInstance) {
  93. this.chartInstance.dispose();
  94. this.chartInstance = null;
  95. }
  96. },
  97. watch: {
  98. // 监听 props 变化,一旦父组件传入新数据,立即触发 ECharts 重绘
  99. currentTime() {
  100. if (!this.loading && this.chartInstance) {
  101. this.updateChart();
  102. }
  103. },
  104. phaseData: {
  105. deep: true,
  106. handler(newVal) {
  107. if (!this.loading && this.chartInstance && newVal.length > 0) {
  108. this.updateChart();
  109. }
  110. }
  111. },
  112. loading(newVal) {
  113. // 当 loading 结束时,销毁旧实例并重建,确保使用正确的容器尺寸
  114. if (!newVal && this.phaseData.length > 0) {
  115. this.$nextTick(() => {
  116. setTimeout(() => {
  117. if (this.chartInstance) {
  118. this.chartInstance.dispose();
  119. this.chartInstance = null;
  120. }
  121. this.initChart();
  122. }, 50);
  123. });
  124. }
  125. }
  126. },
  127. methods: {
  128. handleResize() {
  129. this.updateScale();
  130. if (this.chartInstance) {
  131. this.chartInstance.resize();
  132. this.updateChart();
  133. }
  134. },
  135. updateScale() {
  136. const el = this.$el;
  137. if (!el) return;
  138. const baseWidth = 600; // 设计基准宽度
  139. this.scaleFactor = Math.max(0.5, el.clientWidth / baseWidth);
  140. el.style.setProperty('--s', this.scaleFactor);
  141. },
  142. initChart() {
  143. const chartDom = this.$refs.chartDom;
  144. if (!chartDom) return;
  145. this.chartInstance = echarts.init(chartDom);
  146. // 如果初始化时就已经有数据了,直接渲染
  147. if (this.phaseData.length > 0) {
  148. this.updateChart();
  149. }
  150. // 延迟一帧 resize,确保 v-show 切换后容器有正确宽度
  151. this.$nextTick(() => {
  152. requestAnimationFrame(() => {
  153. this.handleResize();
  154. });
  155. });
  156. },
  157. updateChart() {
  158. if (!this.chartInstance) return;
  159. this.chartInstance.setOption(this.getChartOption(), true);
  160. },
  161. getChartOption() {
  162. const s = this.scaleFactor;
  163. return {
  164. backgroundColor: 'transparent',
  165. grid: { left: Math.round(10 * s), right: Math.round(10 * s), top: Math.round(50 * s), bottom: Math.round(10 * s), containLabel: false },
  166. xAxis: { type: 'value', min: 0, max: this.cycleLength, show: false },
  167. yAxis: { type: 'category', data: ['Track 0', 'Track 1'], inverse: true, show: false },
  168. series: [
  169. {
  170. type: 'custom',
  171. renderItem: this.renderCustomItem,
  172. encode: { x: [1, 2], y: 0 },
  173. data: this.phaseData,
  174. markLine: {
  175. symbol: ['none', 'none'],
  176. silent: true,
  177. label: {
  178. show: true,
  179. position: 'start',
  180. formatter: `${this.currentTime}/${this.cycleLength}`,
  181. color: '#fff',
  182. backgroundColor: COLORS.MARK_BLUE,
  183. padding: [Math.round(4 * s), Math.round(8 * s)],
  184. borderRadius: 2,
  185. fontSize: Math.round(10 * s),
  186. fontWeight: 'bold',
  187. offset: [0, -2]
  188. },
  189. lineStyle: { color: COLORS.MARK_BLUE, type: 'solid', width: Math.round(2 * s) },
  190. data: [ { xAxis: this.currentTime } ]
  191. }
  192. }
  193. ]
  194. };
  195. },
  196. renderCustomItem(params, api) {
  197. const s = this.scaleFactor;
  198. const trackIndex = api.value(0);
  199. const start = api.coord([api.value(1), trackIndex]);
  200. const end = api.coord([api.value(2), trackIndex]);
  201. const blockHeight = api.size([0, 1])[1];
  202. const yPos = start[1] - blockHeight / 2;
  203. const blockWidth = end[0] - start[0];
  204. const dividerY = (api.coord([0, 1])[1] + api.coord([0, 0])[1]) / 2;
  205. const phaseName = api.value(3);
  206. const duration = api.value(4);
  207. const type = api.value(5);
  208. const iconKey = api.value(6);
  209. let fillStyle = COLORS.GREEN_LIGHT;
  210. if (type === 'stripe') fillStyle = stripePattern;
  211. else if (type === 'yellow') fillStyle = COLORS.YELLOW;
  212. else if (type === 'red') fillStyle = COLORS.RED;
  213. const rectShape = echarts.graphic.clipRectByRect(
  214. { x: start[0], y: yPos, width: blockWidth, height: blockHeight },
  215. { x: params.coordSys.x, y: params.coordSys.y, width: params.coordSys.width, height: params.coordSys.height }
  216. );
  217. if (!rectShape) return;
  218. const children = [];
  219. // A. 绘制刻度
  220. if (params.dataIndex === 0) {
  221. const axisBaseY = params.coordSys.y - Math.round(20 * s);
  222. [0, 35, 70, 105, 140].forEach(val => {
  223. const x = api.coord([val, 0])[0];
  224. children.push({ type: 'line', shape: { x1: x, y1: axisBaseY - Math.round(5 * s), x2: x, y2: axisBaseY + Math.round(5 * s) }, style: { stroke: COLORS.AXIS_LINE, lineWidth: Math.round(1.5 * s) } });
  225. });
  226. 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} ];
  227. stages.forEach(st => {
  228. const x1 = api.coord([st.s, 0])[0], x2 = api.coord([st.e, 0])[0], midX = (x1 + x2) / 2;
  229. const textHalf = Math.round(14 * s);
  230. children.push({ type: 'line', shape: { x1: x1, y1: axisBaseY, x2: midX - textHalf, y2: axisBaseY }, style: { stroke: COLORS.AXIS_LINE, lineWidth: Math.round(1.5 * s) } });
  231. children.push({ type: 'line', shape: { x1: midX + textHalf, y1: axisBaseY, x2: x2, y2: axisBaseY }, style: { stroke: COLORS.AXIS_LINE, lineWidth: Math.round(1.5 * s) } });
  232. children.push({ type: 'text', style: { text: st.n, x: midX, y: axisBaseY, fill: COLORS.TEXT_LIGHT, fontSize: Math.round(14 * s), align: 'center', verticalAlign: 'middle', fontWeight: 'bold' } });
  233. });
  234. }
  235. // B. 绘制色块底色
  236. children.push({ type: 'rect', shape: rectShape, style: { fill: fillStyle, stroke: 'none' } });
  237. // C. 绘制内部元素
  238. const fs = Math.max(0.8, s * 0.9); // 文字/图标用更小的缩放
  239. if (type === 'green' && blockWidth > 20) {
  240. const darkWidth = Math.round(25 * fs);
  241. const midY = yPos + blockHeight / 2;
  242. children.push({ type: 'rect', shape: { x: start[0], y: yPos, width: darkWidth, height: blockHeight }, style: { fill: COLORS.GREEN_DARK } });
  243. const arrowH = Math.round(4 * fs);
  244. children.push({ type: 'polygon', shape: { points: [ [start[0] + darkWidth, midY - arrowH], [start[0] + darkWidth, midY + arrowH], [start[0] + darkWidth + arrowH, midY] ] }, style: { fill: COLORS.GREEN_DARK } });
  245. if (iconKey && ICON_PATHS[iconKey]) {
  246. const iconSize = Math.round(14 * fs);
  247. const iconX = start[0] + (darkWidth - iconSize) / 2;
  248. const iconY = midY - iconSize / 2;
  249. children.push({
  250. type: 'path',
  251. shape: { pathData: ICON_PATHS[iconKey], x: iconX, y: iconY, width: iconSize, height: iconSize, layout: 'center' },
  252. style: { fill: COLORS.TEXT_DARK, stroke: 'none' }
  253. });
  254. }
  255. // 文字不受 clipRect 裁剪,独立绘制
  256. children.push({ type: 'text', style: { text: `${phaseName}\n${duration}`, x: start[0] + darkWidth + Math.round(4 * fs), y: midY, fill: COLORS.TEXT_DARK, fontSize: Math.round(12 * fs), fontFamily: 'Arial', fontWeight: 'bold', align: 'left', verticalAlign: 'middle' } });
  257. }
  258. // D. 分割线
  259. if (trackIndex === 1) {
  260. children.push({ type: 'line', shape: { x1: start[0], y1: dividerY, x2: end[0], y2: dividerY }, style: { stroke: COLORS.DIVIDER_LINE, lineWidth: Math.round(1.5 * s) } });
  261. }
  262. return { type: 'group', children: children };
  263. }
  264. }
  265. };
  266. </script>
  267. <style scoped>
  268. .signal-timing-widget {
  269. --s: 1;
  270. width: 100%;
  271. height: 100%;
  272. min-width: 0;
  273. background-color: transparent;
  274. box-sizing: border-box;
  275. position: relative;
  276. display: flex;
  277. flex-direction: column;
  278. overflow: hidden;
  279. }
  280. .header {
  281. display: flex;
  282. justify-content: space-between;
  283. align-items: center;
  284. margin-bottom: calc(var(--s) * 25px);
  285. color: #e0e6f1;
  286. }
  287. .title-area { font-size: calc(var(--s) * 16px); }
  288. .main-title { font-size: calc(var(--s) * 20px); font-weight: bold; margin-right: calc(var(--s) * 10px); }
  289. .sub-info { font-size: calc(var(--s) * 14px); opacity: 0.8; }
  290. .checkbox-area {
  291. font-size: calc(var(--s) * 14px); display: flex; align-items: center;
  292. cursor: pointer; opacity: 0.7; user-select: none;
  293. }
  294. .checkbox-area:hover { opacity: 1; }
  295. .checkbox-mock {
  296. width: calc(var(--s) * 14px); height: calc(var(--s) * 14px); border: 1px solid rgba(255, 255, 255, 0.5);
  297. margin-right: calc(var(--s) * 6px); border-radius: 2px;
  298. display: flex; align-items: center; justify-content: center;
  299. }
  300. .checkbox-mock.is-checked { background-color: #4da8ff; border-color: #4da8ff; }
  301. .chart-container { width: 100%; min-width: 0; flex: 1; min-height: 120px; overflow: hidden; }
  302. .loading-overlay {
  303. flex: 1; min-height: 120px; display: flex; align-items: center; justify-content: center;
  304. color: #758599; font-size: 14px;
  305. }
  306. </style>