dash-pie-chart.vue 10 KB

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