GaugeChart.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <!--
  2. * @description: ECharts仪表盘组件
  3. * @features:
  4. * 1. 可复用:支持在同一页面多次引用
  5. * 2. 响应式:页面缩放时自动调整大小
  6. * 3. 可配置:支持自定义各种属性和样式
  7. * 4. 数据驱动:支持通过props更新数据
  8. *
  9. * @example:
  10. * ```vue
  11. * <template>
  12. * <div class="gauge-container">
  13. * 基本用法
  14. * <GaugeChart :value="65" title="速度" unit="km/h" />
  15. *
  16. * 自定义配置
  17. * <GaugeChart
  18. * :value="80"
  19. * :min="0"
  20. * :max="100"
  21. * title="温度"
  22. * unit="°C"
  23. * :colors="['#36CFC9', '#FF7D00', '#F5222D']"
  24. * width="300px"
  25. * height="300px"
  26. * />
  27. * </div>
  28. * </template>
  29. *
  30. * <script>
  31. * import GaugeChart from '@/views/ledgerAssets/components/details/components/GaugeChart.vue';
  32. *
  33. * export default {
  34. * components: {
  35. * GaugeChart
  36. * },
  37. * data() {
  38. * return {
  39. * // 可以通过data属性动态更新图表数据
  40. * };
  41. * }
  42. * };
  43. * </script>
  44. * ```
  45. -->
  46. <template>
  47. <l-echart ref="chartRef" :style="{ width:width, height: height }"></l-echart >
  48. </template>
  49. <script>
  50. // import * as echarts from 'echarts';
  51. import * as echarts from '@/uni_modules/lime-echart/static/echarts.min'
  52. export default {
  53. name: 'GaugeChart',
  54. props: {
  55. /**
  56. * 图表宽度
  57. * @default '100%'
  58. * @type {string}
  59. */
  60. width: {
  61. type: String,
  62. default: '100%'
  63. },
  64. /**
  65. * 图表高度
  66. * @default '300px'
  67. * @type {string}
  68. */
  69. height: {
  70. type: String,
  71. default: '300px'
  72. },
  73. /**
  74. * 当前值 - 仪表盘显示的数值
  75. * @required true
  76. * @type {number}
  77. */
  78. value: {
  79. type: Number,
  80. required: true
  81. },
  82. /**
  83. * 最小值 - 仪表盘的最小值范围
  84. * @default 0
  85. * @type {number}
  86. */
  87. min: {
  88. type: Number,
  89. default: 0
  90. },
  91. /**
  92. * 最大值 - 仪表盘的最大值范围
  93. * @default 100
  94. * @type {number}
  95. */
  96. max: {
  97. type: Number,
  98. default: 100
  99. },
  100. /**
  101. * 标题 - 仪表盘的标题文本
  102. * @default ''
  103. * @type {string}
  104. */
  105. title: {
  106. type: String,
  107. default: ''
  108. },
  109. /**
  110. * 单位 - 数值的单位
  111. * @default ''
  112. * @type {string}
  113. */
  114. unit: {
  115. type: String,
  116. default: ''
  117. },
  118. /**
  119. * 颜色配置 - 仪表盘颜色渐变区间
  120. * 数组长度决定了颜色分段数量
  121. * @default ['#5470C6', '#91CC75', '#FAC858', '#EE6666']
  122. * @type {Array<string>}
  123. */
  124. colors: {
  125. type: Array,
  126. default: () => ['#5470C6', '#5470C6', '#5470C6', '#5470C6']
  127. },
  128. /**
  129. * 分割线数量 - 仪表盘刻度线数量
  130. * @default 10
  131. * @type {number}
  132. */
  133. splitNumber: {
  134. type: Number,
  135. default: 10
  136. },
  137. /**
  138. * 自定义配置 - 直接覆盖ECharts配置项
  139. * 可以用于高级自定义
  140. * @default {}
  141. * @type {Object}
  142. */
  143. options: {
  144. type: Object,
  145. default: () => ({})
  146. }
  147. },
  148. data() {
  149. return {
  150. chartInstance: null
  151. };
  152. },
  153. mounted() {
  154. this.initChart();
  155. this.handleResize();
  156. window.addEventListener('resize', this.handleResize);
  157. },
  158. beforeDestroy() {
  159. window.removeEventListener('resize', this.handleResize);
  160. if (this.chartInstance) {
  161. this.chartInstance.dispose();
  162. }
  163. },
  164. watch: {
  165. value: {
  166. handler() {
  167. this.updateChart();
  168. },
  169. immediate: false
  170. },
  171. min: 'updateChart',
  172. max: 'updateChart',
  173. title: 'updateChart',
  174. unit: 'updateChart',
  175. colors: {
  176. handler() {
  177. this.updateChart();
  178. },
  179. deep: true
  180. },
  181. splitNumber: 'updateChart',
  182. options: {
  183. handler() {
  184. this.updateChart();
  185. },
  186. deep: true
  187. }
  188. },
  189. methods: {
  190. /**
  191. * 初始化图表实例
  192. * 创建ECharts实例并设置初始配置
  193. * @private
  194. */
  195. initChart() {
  196. if (!this.$refs.chartRef) return;
  197. // 创建唯一的实例以支持在同一页面多次引用
  198. // this.chartInstance = echarts.init(this.$refs.chartRef);
  199. this.updateChart();
  200. },
  201. /**
  202. * 更新图表数据和配置
  203. * 根据props中的最新数据重新渲染图表
  204. * @private
  205. */
  206. updateChart() {
  207. // if (!this.chartInstance) return;
  208. const option = {
  209. // 移除顶部标题,改为在底部显示
  210. backgroundColor: 'transparent',
  211. series: [
  212. {
  213. name: this.title || '',
  214. type: 'gauge',
  215. // startAngle: 180,
  216. // endAngle: 0,
  217. min: this.min,
  218. max: this.max,
  219. splitNumber: this.splitNumber,
  220. radius: '90%',
  221. center: ['50%', '50%'],
  222. itemStyle: {
  223. color: '#7a71ff',
  224. shadowColor: 'rgba(0,138,255,0.45)',
  225. shadowBlur: 10,
  226. shadowOffsetX: 2,
  227. shadowOffsetY: 2,
  228. borderCap: 'round'
  229. },
  230. progress: {
  231. show: true,
  232. roundCap: true,
  233. width: 12,
  234. itemStyle: {
  235. color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
  236. {
  237. offset: 0,
  238. color: '#15b4ff'
  239. },
  240. {
  241. offset: 1,
  242. color: '#716dff'
  243. }
  244. ]),
  245. shadowColor: 'rgba(0,138,255,0.45)',
  246. shadowBlur: 10,
  247. shadowOffsetX: 2,
  248. shadowOffsetY: 2
  249. }
  250. },
  251. // progress: {
  252. // show: true,
  253. // width: 15,
  254. // roundCap: true,
  255. // },
  256. // 优化仪表盘轴线样式 - 采用单一蓝色调
  257. axisLine: {
  258. lineStyle: {
  259. width: 12,
  260. roundCap: true,
  261. // color: this.calculateColorRanges(),
  262. // shadowBlur: 10,
  263. // shadowColor: 'rgba(84, 112, 198, 0.3)'
  264. }
  265. },
  266. // 优化指针样式 - 更细,蓝色
  267. pointer: {
  268. show: true,
  269. width: 5,
  270. length: '65%',
  271. itemStyle: {
  272. color: '#716dff',
  273. // borderWidth: 1,
  274. // borderType: 'solid',
  275. // borderColor: '#333'
  276. }
  277. },
  278. // 优化刻度线 - 更细、更短,分布在弧上
  279. axisTick: {
  280. show: false,
  281. distance: 0,
  282. length: 6,
  283. lineStyle: {
  284. color: '#666',
  285. width: 1
  286. }
  287. },
  288. // 优化分割线 - 与示例图保持一致
  289. splitLine: {
  290. distance: 0,
  291. length: 8,
  292. lineStyle: {
  293. color: '#5470C6',
  294. width: 2
  295. }
  296. },
  297. // 优化标签位置和样式
  298. axisLabel: {
  299. color: '#333',
  300. distance: 20,
  301. fontSize: 12,
  302. fontWeight: 'normal'
  303. },
  304. // 优化中心显示内容 - 居中显示数值
  305. detail: {
  306. valueAnimation: true,
  307. formatter: `{value}${this.unit}`,
  308. color: '#333',
  309. fontSize: 18,
  310. fontWeight: 'bold',
  311. offsetCenter: [0, '40%'],
  312. borderColor: 'transparent'
  313. },
  314. // 添加单位显示
  315. title: {
  316. offsetCenter: [0, '65%'],
  317. fontSize: 14,
  318. color: '#333',
  319. fontWeight: 'normal'
  320. },
  321. data: [
  322. {
  323. value: this.value,
  324. name: this.title
  325. }
  326. ]
  327. }
  328. ],
  329. // 合并自定义配置,允许用户覆盖默认设置
  330. ...this.options
  331. };
  332. this.$refs.chartRef.init(echarts, chart => {
  333. chart.setOption(option,true)
  334. })
  335. // 使用true参数强制更新所有配置项
  336. // this.chartInstance.setOption(option, true);
  337. },
  338. /**
  339. * 计算仪表盘颜色渐变范围
  340. * 根据最小值、最大值和颜色数组计算分段颜色区间
  341. * @private
  342. * @returns {Array<Array<number|string>>} 颜色范围数组
  343. */
  344. calculateColorRanges() {
  345. // 对于压力计等场景,使用单一蓝色调更合适
  346. // 但保持配置灵活性,允许用户自定义颜色
  347. const ranges = [];
  348. const step = (this.max - this.min) / this.colors.length;
  349. for (let i = 0; i < this.colors.length; i++) {
  350. ranges.push([
  351. this.min + step * i,
  352. this.min + step * (i + 1),
  353. this.colors[i]
  354. ]);
  355. }
  356. return ranges;
  357. },
  358. /**
  359. * 处理窗口大小变化
  360. * 当页面缩放时自动调整图表大小以保持显示正常
  361. * @private
  362. */
  363. handleResize() {
  364. if (this.chartInstance) {
  365. this.chartInstance.resize();
  366. }
  367. },
  368. /**
  369. * 手动触发重绘
  370. * 提供给父组件调用的公共方法,用于强制刷新图表
  371. * @public
  372. */
  373. redraw() {
  374. if (!this.chartInstance) {
  375. this.initChart();
  376. } else {
  377. this.updateChart();
  378. }
  379. }
  380. }
  381. };
  382. </script>
  383. <style scoped>
  384. :deep(.echarts) {
  385. width: 100%;
  386. height: 100%;
  387. }
  388. </style>