dash-pie-chart.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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: "70%",
  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(14),
  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(16),
  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: util.vh(2), //指针粗细
  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: "#fff", //用颜色渐变函数不起作用
  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. hoverAnimation: false,
  219. data: [
  220. {
  221. value: this.value,
  222. itemStyle: {
  223. normal: {
  224. color: "transition",
  225. },
  226. },
  227. label: {
  228. normal: {
  229. formatter: function(params) {
  230. return params.value;
  231. },
  232. color: partten.getColor(this.color),
  233. fontSize: util.vh(16),
  234. fontWeight: "bold",
  235. position: "center",
  236. show: true,
  237. },
  238. },
  239. labelLine: {
  240. show: false,
  241. },
  242. },
  243. ],
  244. },
  245. // 光环
  246. {
  247. type: "pie",
  248. radius: "40%",
  249. center: ["50%", "50%"],
  250. animationType: "scale",
  251. animation: false,
  252. label: {
  253. show: false,
  254. },
  255. labelLine: {
  256. show: false,
  257. },
  258. data: [
  259. {
  260. value: 1,
  261. itemStyle: {
  262. normal: {
  263. color: "transparent",
  264. borderColor: partten.getColor(this.color),
  265. opacity: 0.3,
  266. shadowColor: partten.getColor(this.color),
  267. shadowBlur: util.vh(20),
  268. },
  269. },
  270. },
  271. ],
  272. },
  273. ],
  274. };
  275. chart.clear();
  276. chart.setOption(option);
  277. this.resize = function() {
  278. chart.resize();
  279. };
  280. window.removeEventListener("resize", this.resize);
  281. window.addEventListener("resize", this.resize);
  282. },
  283. },
  284. created() {
  285. this.id = "pie-chart-" + util.newGUID();
  286. },
  287. mounted() {
  288. this.$el.style.width = this.width;
  289. this.$el.style.height = this.height;
  290. this.initChart();
  291. },
  292. updated() {
  293. this.initChart();
  294. },
  295. unmounted() {
  296. window.removeEventListener("resize", this.resize);
  297. },
  298. };
  299. </script>
  300. <style lang="less" scoped>
  301. .chart {
  302. width: 100%;
  303. height: 100%;
  304. display: block;
  305. margin: auto;
  306. }
  307. </style>