TrafficTimeSpace.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <div ref="chartContainer" class="traffic-timespace-chart"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts';
  6. export default {
  7. name: 'TrafficTimeSpace',
  8. props: {
  9. // 路口名称数组(从起点到终点)
  10. intersections: {
  11. type: Array,
  12. required: true
  13. },
  14. // 各路口到起点的距离(米),与 intersections 一一对应
  15. distances: {
  16. type: Array,
  17. required: true
  18. },
  19. // 绿波带数据: [{ yBottom, yTop, xBL, xBR, xTL, xTR, label, direction }]
  20. // direction: 'up' | 'down'
  21. waveData: {
  22. type: Array,
  23. default: () => []
  24. },
  25. // 绿灯数据: [{ y, start, end }]
  26. greenData: {
  27. type: Array,
  28. default: () => []
  29. },
  30. // 可视窗口显示的时间跨度(秒)
  31. viewWindow: {
  32. type: Number,
  33. default: 350
  34. },
  35. // 是否启用自动滚动
  36. autoScroll: {
  37. type: Boolean,
  38. default: false
  39. },
  40. // 自动滚动速度(秒/帧)
  41. scrollSpeed: {
  42. type: Number,
  43. default: 0.5
  44. },
  45. // 上行波带颜色
  46. upWaveColor: {
  47. type: String,
  48. default: 'rgba(46, 196, 182, 0.45)'
  49. },
  50. // 下行波带颜色
  51. downWaveColor: {
  52. type: String,
  53. default: 'rgba(104, 231, 95, 0.4)'
  54. },
  55. // 波带文字颜色
  56. waveLabelColor: {
  57. type: String,
  58. default: '#e0f7fa'
  59. }
  60. },
  61. data() {
  62. return {
  63. chart: null,
  64. scrollTimer: null,
  65. currentViewTime: 0
  66. };
  67. },
  68. computed: {
  69. maxDistance() {
  70. return Math.max(...this.distances);
  71. },
  72. maxDataTime() {
  73. let max = 0;
  74. this.waveData.forEach(w => {
  75. max = Math.max(max, w.xBR, w.xTR);
  76. });
  77. this.greenData.forEach(g => {
  78. max = Math.max(max, g.end);
  79. });
  80. return max;
  81. },
  82. // 转为 ECharts custom series 所需的数组格式
  83. echartsWaveData() {
  84. return this.waveData.map(w => [
  85. w.yBottom, w.yTop, w.xBL, w.xBR, w.xTL, w.xTR, w.label || '', w.direction || 'up'
  86. ]);
  87. },
  88. echartsGreenData() {
  89. return this.greenData.map(g => [g.y, g.start, g.end]);
  90. },
  91. echartsRedData() {
  92. return this.distances.map(y => [y]);
  93. },
  94. // 路口名称需要反转以匹配 Y 轴方向
  95. reversedIntersections() {
  96. return [...this.intersections].reverse();
  97. }
  98. },
  99. mounted() {
  100. this.$nextTick(() => {
  101. this.initChart();
  102. if (this.autoScroll) this.startScroll();
  103. });
  104. },
  105. beforeDestroy() {
  106. this.stopScroll();
  107. if (this._resizeObserver) {
  108. this._resizeObserver.disconnect();
  109. this._resizeObserver = null;
  110. }
  111. if (this.chart) {
  112. this.chart.dispose();
  113. this.chart = null;
  114. }
  115. window.removeEventListener('resize', this._resizeHandler);
  116. },
  117. watch: {
  118. waveData() { this.updateChart(); },
  119. greenData() { this.updateChart(); },
  120. autoScroll(val) {
  121. val ? this.startScroll() : this.stopScroll();
  122. }
  123. },
  124. methods: {
  125. initChart() {
  126. this.chart = echarts.init(this.$refs.chartContainer);
  127. this._resizeHandler = () => this.chart && this.chart.resize();
  128. window.addEventListener('resize', this._resizeHandler);
  129. // 监听容器尺寸变化(弹窗拉伸、延迟渲染等场景)
  130. if (typeof ResizeObserver !== 'undefined') {
  131. this._roaPending = false;
  132. this._resizeObserver = new ResizeObserver(() => {
  133. if (!this._roaPending) {
  134. this._roaPending = true;
  135. requestAnimationFrame(() => {
  136. this._roaPending = false;
  137. this.chart && this.chart.resize();
  138. });
  139. }
  140. });
  141. this._resizeObserver.observe(this.$refs.chartContainer);
  142. }
  143. this.updateChart();
  144. },
  145. updateChart() {
  146. if (!this.chart) return;
  147. const self = this;
  148. const distances = this.distances;
  149. const intersections = this.reversedIntersections;
  150. const maxDist = this.maxDistance;
  151. this.chart.setOption({
  152. backgroundColor: 'transparent',
  153. animation: false,
  154. tooltip: { show: false },
  155. grid: { left: 110, right: 30, top: 40, bottom: 40 },
  156. xAxis: {
  157. type: 'value',
  158. min: this.currentViewTime,
  159. max: this.currentViewTime + this.viewWindow,
  160. axisLabel: { color: '#7b95b9', formatter: '{value}s', fontSize: 13 },
  161. splitLine: { show: true, lineStyle: { color: '#1a305d', type: 'solid' } },
  162. axisLine: { lineStyle: { color: '#31548e' } }
  163. },
  164. yAxis: {
  165. type: 'value',
  166. min: 0,
  167. max: maxDist,
  168. interval: distances.length > 1 ? distances[1] - distances[0] : 300,
  169. axisLabel: {
  170. interval: 0,
  171. color: '#9cb1d4',
  172. fontWeight: 'bold',
  173. fontSize: 13,
  174. formatter: value => distances.includes(value) ? intersections[distances.indexOf(value)] : ''
  175. },
  176. splitLine: { show: true, lineStyle: { color: '#1a305d' } }
  177. },
  178. series: [
  179. {
  180. type: 'custom',
  181. renderItem: function (params, api) {
  182. return self.renderWave(params, api);
  183. },
  184. data: this.echartsWaveData,
  185. clip: true,
  186. z: 1
  187. },
  188. {
  189. type: 'custom',
  190. renderItem: function (params, api) {
  191. return self.renderRedBackground(params, api);
  192. },
  193. data: this.echartsRedData,
  194. clip: true,
  195. z: 2
  196. },
  197. {
  198. type: 'custom',
  199. renderItem: function (params, api) {
  200. return self.renderGreenLight(params, api);
  201. },
  202. data: this.echartsGreenData,
  203. clip: true,
  204. z: 3
  205. }
  206. ]
  207. });
  208. },
  209. renderRedBackground(params, api) {
  210. const y = api.value(0);
  211. const startX = api.coord([0, y])[0];
  212. const endX = api.coord([this.maxDataTime, y])[0];
  213. return {
  214. type: 'rect',
  215. shape: { x: startX, y: api.coord([0, y])[1] - 3, width: endX - startX, height: 6 },
  216. style: { fill: '#f02828' }
  217. };
  218. },
  219. renderGreenLight(params, api) {
  220. const y = api.value(0);
  221. const p1 = api.coord([api.value(1), y]);
  222. const p2 = api.coord([api.value(2), y]);
  223. return {
  224. type: 'rect',
  225. shape: { x: p1[0], y: p1[1] - 4, width: p2[0] - p1[0], height: 8 },
  226. style: api.style({ fill: '#68e75f' })
  227. };
  228. },
  229. renderWave(params, api) {
  230. const yBottom = api.value(0), yTop = api.value(1);
  231. const xBL = api.value(2), xBR = api.value(3), xTL = api.value(4), xTR = api.value(5);
  232. const text = api.value(6), dir = api.value(7);
  233. const ptBL = api.coord([xBL, yBottom]), ptBR = api.coord([xBR, yBottom]);
  234. const ptTL = api.coord([xTL, yTop]), ptTR = api.coord([xTR, yTop]);
  235. const angle = -Math.atan2(ptTL[1] - ptBL[1], ptTL[0] - ptBL[0]);
  236. const fillColor = dir === 'up' ? this.upWaveColor : this.downWaveColor;
  237. return {
  238. type: 'group',
  239. children: [
  240. {
  241. type: 'polygon',
  242. shape: { points: [ptBL, ptBR, ptTR, ptTL] },
  243. z2: 1,
  244. style: api.style({ fill: fillColor, stroke: 'none' })
  245. },
  246. {
  247. type: 'text',
  248. x: (ptBL[0] + ptTR[0]) / 2,
  249. y: (ptBL[1] + ptTR[1]) / 2,
  250. rotation: angle,
  251. z2: 5,
  252. style: {
  253. text: text,
  254. fill: this.waveLabelColor,
  255. font: 'bold 15px sans-serif',
  256. textAlign: 'center',
  257. textVerticalAlign: 'middle'
  258. }
  259. }
  260. ]
  261. };
  262. },
  263. startScroll() {
  264. this.stopScroll();
  265. this.scrollTimer = setInterval(() => {
  266. this.currentViewTime += this.scrollSpeed;
  267. if (this.currentViewTime > this.maxDataTime - this.viewWindow) {
  268. this.currentViewTime = 0;
  269. }
  270. if (this.chart) {
  271. this.chart.setOption({
  272. xAxis: { min: this.currentViewTime, max: this.currentViewTime + this.viewWindow }
  273. });
  274. }
  275. }, 16);
  276. },
  277. stopScroll() {
  278. if (this.scrollTimer) {
  279. clearInterval(this.scrollTimer);
  280. this.scrollTimer = null;
  281. }
  282. },
  283. resize() {
  284. if (this.chart) this.chart.resize();
  285. }
  286. }
  287. };
  288. </script>
  289. <style scoped>
  290. .traffic-timespace-chart {
  291. width: 100%;
  292. height: 100%;
  293. }
  294. </style>