LineChart.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <div class="line-chart-container">
  3. <div ref="chartContainer" class="chart-wrapper" :style="{ width: width, height: height }"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import * as echarts from 'echarts';
  8. export default {
  9. name: 'LineChart',
  10. props: {
  11. /**
  12. * 图表宽度
  13. * @default '100%'
  14. */
  15. width: {
  16. type: String,
  17. default: '100%'
  18. },
  19. /**
  20. * 图表高度
  21. * @default '400px'
  22. */
  23. height: {
  24. type: String,
  25. default: '300px'
  26. },
  27. /**
  28. * 图表数据
  29. * @example
  30. * {
  31. * xAxis: ['周一', '周二', '周三', '周四', '周五'],
  32. * series: [
  33. * {
  34. * name: '数据1',
  35. * data: [120, 200, 150, 80, 70],
  36. * color: '#5470C6'
  37. * },
  38. * {
  39. * name: '数据2',
  40. * data: [90, 150, 220, 160, 130],
  41. * color: '#91CC75'
  42. * }
  43. * ]
  44. * }
  45. */
  46. data: {
  47. type: Object,
  48. required: true,
  49. validator: (value) => {
  50. return value && Array.isArray(value.xAxis) && Array.isArray(value.series);
  51. }
  52. },
  53. /**
  54. * 图表标题
  55. */
  56. title: {
  57. type: String,
  58. default: ''
  59. },
  60. /**
  61. * X轴名称
  62. */
  63. xAxisName: {
  64. type: String,
  65. default: ''
  66. },
  67. /**
  68. * Y轴名称
  69. */
  70. yAxisName: {
  71. type: String,
  72. default: ''
  73. },
  74. /**
  75. * 是否显示图例
  76. * @default true
  77. */
  78. showLegend: {
  79. type: Boolean,
  80. default: true
  81. },
  82. /**
  83. * 高级配置项,会与默认配置合并
  84. */
  85. options: {
  86. type: Object,
  87. default: () => ({})
  88. }
  89. },
  90. data() {
  91. return {
  92. chart: null,
  93. resizeObserver: null
  94. };
  95. },
  96. watch: {
  97. /**
  98. * 监听数据变化,更新图表
  99. */
  100. data: {
  101. deep: true,
  102. handler(newData) {
  103. this.updateChart(newData);
  104. }
  105. }
  106. },
  107. mounted() {
  108. this.initChart();
  109. this.addResizeListener();
  110. },
  111. beforeDestroy() {
  112. this.removeResizeListener();
  113. if (this.chart) {
  114. this.chart.dispose();
  115. this.chart = null;
  116. }
  117. },
  118. methods: {
  119. /**
  120. * 初始化图表
  121. * @private
  122. */
  123. initChart() {
  124. if (!this.$refs.chartContainer) return;
  125. this.chart = echarts.init(this.$refs.chartContainer);
  126. this.updateChart(this.data);
  127. },
  128. /**
  129. * 更新图表数据
  130. * @param {Object} chartData - 图表数据
  131. * @public
  132. */
  133. updateChart(chartData) {
  134. if (!this.chart || !chartData) return;
  135. // 获取当前图表的yAxisName作为单位
  136. const unit = this.yAxisName || '';
  137. const series = chartData.series.map(item => ({
  138. name: item.name,
  139. type: 'line',
  140. // 使用简单的数据结构
  141. data: item.data,
  142. smooth: true,
  143. lineStyle: {
  144. width: 3,
  145. color: item.color
  146. },
  147. itemStyle: {
  148. color: item.color
  149. },
  150. areaStyle: {
  151. opacity: 0.1,
  152. color: item.color
  153. },
  154. emphasis: {
  155. focus: 'series'
  156. },
  157. // 将单位信息作为额外的属性传递
  158. seriesUnit: unit
  159. }));
  160. const legend = this.showLegend ? {
  161. data: chartData.series.map(item => item.name),
  162. top: '5%'
  163. } : {
  164. show: false
  165. };
  166. const option = {
  167. title: {
  168. text: this.title,
  169. left: 'center',
  170. top: '5%',
  171. textStyle: {
  172. fontSize: 16,
  173. fontWeight: 'normal'
  174. }
  175. },
  176. tooltip: {
  177. trigger: 'axis',
  178. backgroundColor: 'rgba(255, 255, 255, 0.95)',
  179. borderColor: '#ddd',
  180. borderWidth: 1,
  181. textStyle: {
  182. color: '#333'
  183. },
  184. formatter: function(params) {
  185. let result = params[0].name + '<br/>';
  186. params.forEach(item => {
  187. // 获取series的unit信息
  188. const unit = item.seriesUnit || '';
  189. result += `<div style="display: flex; align-items: center; margin: 5px 0;">
  190. <span style="display: inline-block; width: 10px; height: 10px; background: ${item.color}; border-radius: 50%; margin-right: 8px;"></span>
  191. <span style="color: ${item.color};">${item.seriesName}: ${item.value}${unit}</span>
  192. </div>`;
  193. });
  194. return result;
  195. }
  196. },
  197. legend,
  198. dataZoom: [
  199. {
  200. type: 'slider',
  201. filterMode: 'none',
  202. bottom: '0',
  203. height: 20
  204. },
  205. {
  206. type: 'inside',
  207. filterMode: 'none'
  208. },
  209. ],
  210. grid: {
  211. left: '3%',
  212. right: '4%',
  213. bottom: '7%',
  214. top: this.title ? '20%' : '15%',
  215. containLabel: true
  216. },
  217. xAxis: {
  218. type: 'category',
  219. boundaryGap: false,
  220. data: chartData.xAxis,
  221. name: this.xAxisName,
  222. nameTextStyle: {
  223. padding: [0, 0, 0, 40]
  224. },
  225. axisLine: {
  226. lineStyle: {
  227. color: '#ddd'
  228. }
  229. },
  230. axisLabel: {
  231. color: '#666',
  232. fontSize: 12
  233. }
  234. },
  235. yAxis: {
  236. type: 'value',
  237. name: this.yAxisName,
  238. nameTextStyle: {
  239. padding: [0, 40, 0, 0]
  240. },
  241. axisLine: {
  242. show: false
  243. },
  244. axisTick: {
  245. show: false
  246. },
  247. axisLabel: {
  248. color: '#666',
  249. fontSize: 12
  250. },
  251. splitLine: {
  252. lineStyle: {
  253. color: '#f0f0f0',
  254. type: 'dashed'
  255. }
  256. }
  257. },
  258. series,
  259. ...this.options
  260. };
  261. this.chart.setOption(option, true);
  262. },
  263. /**
  264. * 添加响应式监听
  265. * @private
  266. */
  267. addResizeListener() {
  268. if (window.ResizeObserver) {
  269. this.resizeObserver = new ResizeObserver(() => {
  270. if (this.chart) {
  271. this.chart.resize();
  272. }
  273. });
  274. this.resizeObserver.observe(this.$refs.chartContainer);
  275. } else {
  276. // 兼容旧浏览器
  277. window.addEventListener('resize', this.handleResize);
  278. }
  279. },
  280. /**
  281. * 移除响应式监听
  282. * @private
  283. */
  284. removeResizeListener() {
  285. if (this.resizeObserver) {
  286. this.resizeObserver.disconnect();
  287. this.resizeObserver = null;
  288. } else {
  289. window.removeEventListener('resize', this.handleResize);
  290. }
  291. },
  292. /**
  293. * 处理窗口大小变化
  294. * @private
  295. */
  296. handleResize() {
  297. if (this.chart) {
  298. this.chart.resize();
  299. }
  300. },
  301. /**
  302. * 手动调整图表大小
  303. * @public
  304. */
  305. resize() {
  306. if (this.chart) {
  307. this.chart.resize();
  308. }
  309. }
  310. }
  311. };
  312. </script>
  313. <style scoped>
  314. .line-chart-container {
  315. width: 100%;
  316. height: 100%;
  317. }
  318. .chart-wrapper {
  319. width: 100%;
  320. height: 100%;
  321. }
  322. </style>