dash-pie-chart.vue 8.6 KB

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