dash-pie-chart.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@/helper/util.js";
  6. import partten from "@/helper/partten.js";
  7. import * as echarts from "echarts";
  8. import $ from "jquery";
  9. export default {
  10. name: "dsah-pie",
  11. componentName: "dsah-pie",
  12. props: {
  13. // 宽度 默认9.722vh
  14. width: {
  15. type: String,
  16. default: "85%",
  17. },
  18. // 高度 默认9.722vh
  19. height: {
  20. type: String,
  21. default: "7.4074vh",
  22. },
  23. // 标题
  24. title: {
  25. type: String,
  26. default: "标题",
  27. },
  28. unit: {
  29. type: String,
  30. default: "",
  31. },
  32. // 值
  33. value: {
  34. type: Number,
  35. default: 0,
  36. },
  37. // 最小值
  38. min: {
  39. type: Number,
  40. default: 0,
  41. },
  42. // 最大值
  43. max: {
  44. type: Number,
  45. default: 100,
  46. },
  47. // 颜色 传入 green yellow等 (partten中支持的颜色)
  48. color: {
  49. type: String,
  50. default: "green",
  51. },
  52. },
  53. data() {
  54. return {
  55. id: "",
  56. chart: null,
  57. };
  58. },
  59. computed: {
  60. colorValue() {
  61. return partten.getColor(this.color);
  62. },
  63. },
  64. methods: {
  65. resize() {},
  66. initChart() {
  67. let chart = echarts.init(this.$el);
  68. let option = {
  69. title: {
  70. show: true,
  71. text: this.unit,
  72. subtext: this.title,
  73. subtextStyle: {
  74. fontSize: 12,
  75. color: "#fff",
  76. },
  77. x: "45%",
  78. y: "62%",
  79. z: 8,
  80. textAlign: "center",
  81. textStyle: {
  82. color:'#999',
  83. fontSize: util.vh(12),
  84. fontWeight: "normal",
  85. },
  86. },
  87. series: [
  88. // 进度条
  89. {
  90. z: 1,
  91. name: "内部(环形)进度条",
  92. type: "gauge",
  93. radius: "100%",
  94. splitNumber: 5,
  95. axisLine: {
  96. lineStyle: {
  97. color: [
  98. [
  99. (this.value < 0 ? 0 : this.value) / this.max,
  100. new echarts.graphic.LinearGradient(0, 0, 1, 0, [
  101. {
  102. offset: 0,
  103. color: $("#appBody").is(".dark")
  104. ? partten.getColor(this.color) + "01"
  105. : "rgb(57, 54, 143)",
  106. },
  107. {
  108. offset: 1,
  109. color: $("#appBody").is(".dark")
  110. ? partten.getColor(this.color) + "ff"
  111. : "rgb(57, 54, 143)",
  112. },
  113. ]),
  114. ],
  115. [1, "transparent"],
  116. ],
  117. width: util.vh(12),
  118. },
  119. },
  120. axisLabel: {
  121. show: false,
  122. },
  123. axisTick: {
  124. show: false,
  125. },
  126. splitLine: {
  127. show: false,
  128. },
  129. pointer: {
  130. show: false,
  131. },
  132. },
  133. // 指针
  134. {
  135. name: "指针",
  136. type: "gauge",
  137. z: 2,
  138. min: this.min,
  139. max: this.max,
  140. radius: "100%",
  141. axisLine: {
  142. show: false,
  143. },
  144. tooltip: {
  145. show: false,
  146. },
  147. axisLabel: {
  148. show: false,
  149. },
  150. axisTick: {
  151. show: false,
  152. },
  153. splitLine: {
  154. show: false,
  155. },
  156. detail: {
  157. show: false,
  158. },
  159. title: {
  160. //标题
  161. show: false,
  162. },
  163. data: [
  164. {
  165. value: this.value < 0 ? 0 : this.value,
  166. },
  167. ],
  168. itemStyle: {
  169. normal: {
  170. color: $("#appBody").is(".dark") ? "#fff" : "#000",
  171. },
  172. },
  173. pointer: {
  174. show: true,
  175. length: "35%",
  176. radius: "20%",
  177. width: 1, //指针粗细
  178. offsetCenter: ["0%", "-40%"],
  179. },
  180. animationDuration: 0,
  181. },
  182. // 刻度
  183. {
  184. name: "外部刻度",
  185. type: "gauge",
  186. // center: ['20%', '50%'],
  187. radius: "100%",
  188. min: this.min, //最小刻度
  189. max: this.max, //最大刻度
  190. splitNumber: 10, //刻度数量
  191. startAngle: 225,
  192. endAngle: -45,
  193. axisLine: {
  194. show: true,
  195. // 仪表盘刻度线
  196. lineStyle: {
  197. width: util.vh(1),
  198. color: [
  199. [
  200. 1,
  201. $("#appBody").is(".dark")
  202. ? partten.getColor(this.color)
  203. : "rgb(57, 54, 143)",
  204. ],
  205. ],
  206. },
  207. },
  208. //仪表盘文字
  209. axisLabel: {
  210. show: false,
  211. },
  212. //刻度标签。
  213. axisTick: {
  214. show: true,
  215. distance: 6,
  216. splitNumber: 2,
  217. lineStyle: {
  218. color: $("#appBody").is(".dark")
  219. ? partten.getColor("green")
  220. : "rgb(57, 54, 143)", //用颜色渐变函数不起作用
  221. width: util.vh(1),
  222. },
  223. length: util.vh(4),
  224. }, //刻度样式
  225. splitLine: {
  226. show: false,
  227. }, //分隔线样式
  228. detail: {
  229. show: false,
  230. },
  231. pointer: {
  232. show: false,
  233. },
  234. },
  235. // 显示数字
  236. {
  237. type: "pie",
  238. radius: ["0", "40%"],
  239. center: ["50%", "50%"],
  240. z: 8,
  241. animation: false,
  242. hoverAnimation: false,
  243. data: [
  244. {
  245. value: this.value < 0 ? 0 : this.value,
  246. itemStyle: {
  247. normal: {
  248. color: "transition",
  249. },
  250. },
  251. label: {
  252. normal: {
  253. formatter: function (params) {
  254. return params.value;
  255. },
  256. color: $("#appBody").is(".dark")
  257. ? partten.getColor(this.color)
  258. : "rgb(57, 54, 143)",
  259. fontSize: util.vh(12),
  260. fontWeight: "bold",
  261. position: "center",
  262. show: true,
  263. },
  264. },
  265. labelLine: {
  266. show: false,
  267. },
  268. },
  269. ],
  270. },
  271. {
  272. name: "内部阴影",
  273. type: "gauge",
  274. radius: "90%",
  275. splitNumber: 10,
  276. axisLine: {
  277. lineStyle: {
  278. color: [
  279. // [
  280. // 1,
  281. // new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  282. // {
  283. // offset: 0,
  284. // color: "rgba(45,230,150,0)",
  285. // },
  286. // {
  287. // offset: 0.5,
  288. // color: "rgba(45,230,150,0.2)",
  289. // },
  290. // {
  291. // offset: 1,
  292. // color: "rgba(45,230,150,1)",
  293. // },
  294. // ]),
  295. // ],
  296. [
  297. 1,
  298. $("#appBody").is(".dark")
  299. ? "rgba(45,230,150,0.1)"
  300. : "rgba(57, 54, 143, 0.1)",
  301. ],
  302. // [1, "rgba(45,230,150,0)"],
  303. ],
  304. width: 3,
  305. },
  306. },
  307. axisLabel: {
  308. show: false,
  309. },
  310. axisTick: {
  311. show: false,
  312. },
  313. splitLine: {
  314. show: false,
  315. },
  316. itemStyle: {
  317. show: false,
  318. },
  319. },
  320. // 光环
  321. {
  322. type: "pie",
  323. radius: "40%",
  324. center: ["50%", "50%"],
  325. animationType: "scale",
  326. animation: false,
  327. label: {
  328. show: false,
  329. },
  330. labelLine: {
  331. show: false,
  332. },
  333. emphasis: {
  334. scale: false,
  335. },
  336. data: [
  337. {
  338. value: 1,
  339. itemStyle: {
  340. color: "transparent",
  341. borderColor: $("#appBody").is(".dark")
  342. ? partten.getColor(this.color)
  343. : "rgb(57, 54, 143)",
  344. opacity: 0.3,
  345. shadowColor: $("#appBody").is(".dark")
  346. ? partten.getColor(this.color)
  347. : "rgb(57, 54, 143)",
  348. shadowBlur: util.vh(20),
  349. },
  350. },
  351. ],
  352. },
  353. ],
  354. };
  355. chart.clear();
  356. chart.setOption(option);
  357. this.resize = function () {
  358. chart.resize();
  359. };
  360. window.removeEventListener("resize", this.resize);
  361. window.addEventListener("resize", this.resize);
  362. },
  363. },
  364. created() {
  365. this.id = "pie-chart-" + util.newGUID();
  366. },
  367. mounted() {
  368. this.$el.style.width = this.width;
  369. this.$el.style.height = this.height;
  370. this.initChart();
  371. },
  372. updated() {
  373. this.initChart();
  374. },
  375. unmounted() {
  376. window.removeEventListener("resize", this.resize);
  377. },
  378. watch: {
  379. "$store.state.themeName"() {
  380. this.initChart();
  381. },
  382. },
  383. };
  384. </script>
  385. <style lang="less" scoped>
  386. .chart {
  387. width: 100%;
  388. height: 100%;
  389. display: block;
  390. margin: auto;
  391. }
  392. </style>