dash-pie-chart.vue 9.6 KB

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