LineChart.vue 7.2 KB

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