dash-pie-chart.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. initChart() {
  61. let option = {
  62. title: {
  63. show: true,
  64. text: this.title,
  65. x: "45%",
  66. y: "80%",
  67. z: 8,
  68. textAlign: "center",
  69. textStyle: {
  70. color: partten.getColor("gray"),
  71. fontSize: util.vh(14),
  72. fontWeight: "normal",
  73. },
  74. },
  75. series: [
  76. // 进度条
  77. {
  78. z: 1,
  79. name: "内部(环形)进度条",
  80. type: "gauge",
  81. radius: "100%",
  82. splitNumber: 5,
  83. axisLine: {
  84. lineStyle: {
  85. color: [
  86. [
  87. this.value / this.max,
  88. new echarts.graphic.LinearGradient(0, 0, 1, 0, [
  89. {
  90. offset: 0,
  91. color: partten.getColor(this.color) + "01",
  92. },
  93. {
  94. offset: 1,
  95. color: partten.getColor(this.color) + "ff",
  96. },
  97. ]),
  98. ],
  99. [1, "transparent"],
  100. ],
  101. width: util.vh(16),
  102. },
  103. },
  104. axisLabel: {
  105. show: false,
  106. },
  107. axisTick: {
  108. show: false,
  109. },
  110. splitLine: {
  111. show: false,
  112. },
  113. pointer: {
  114. show: false,
  115. },
  116. },
  117. // 指针
  118. {
  119. name: "指针",
  120. type: "gauge",
  121. z: 2,
  122. min: this.min,
  123. max: this.max,
  124. radius: "100%",
  125. axisLine: {
  126. show: false,
  127. },
  128. tooltip: {
  129. show: false,
  130. },
  131. axisLabel: {
  132. show: false,
  133. },
  134. axisTick: {
  135. show: false,
  136. },
  137. splitLine: {
  138. show: false,
  139. },
  140. detail: {
  141. show: false,
  142. },
  143. title: {
  144. //标题
  145. show: false,
  146. },
  147. data: [
  148. {
  149. value: this.value,
  150. },
  151. ],
  152. itemStyle: {
  153. normal: {
  154. color: "#fff",
  155. },
  156. },
  157. pointer: {
  158. show: true,
  159. length: "35%",
  160. radius: "20%",
  161. width: util.vh(2), //指针粗细
  162. offsetCenter: ["0%", "-40%"],
  163. },
  164. animationDuration: 1000,
  165. },
  166. // 刻度
  167. {
  168. name: "外部刻度",
  169. type: "gauge",
  170. // center: ['20%', '50%'],
  171. radius: "100%",
  172. min: this.min, //最小刻度
  173. max: this.max, //最大刻度
  174. splitNumber: 10, //刻度数量
  175. startAngle: 225,
  176. endAngle: -45,
  177. axisLine: {
  178. show: true,
  179. // 仪表盘刻度线
  180. lineStyle: {
  181. width: util.vh(1),
  182. color: [[1, partten.getColor(this.color)]],
  183. },
  184. },
  185. //仪表盘文字
  186. axisLabel: {
  187. show: false,
  188. },
  189. //刻度标签。
  190. axisTick: {
  191. show: true,
  192. distance: 6,
  193. splitNumber: 2,
  194. lineStyle: {
  195. color: "#fff", //用颜色渐变函数不起作用
  196. width: util.vh(1),
  197. },
  198. length: util.vh(4),
  199. }, //刻度样式
  200. splitLine: {
  201. show: false,
  202. }, //分隔线样式
  203. detail: {
  204. show: false,
  205. },
  206. pointer: {
  207. show: false,
  208. },
  209. },
  210. // 显示数字
  211. {
  212. type: "pie",
  213. radius: ["0", "40%"],
  214. center: ["50%", "50%"],
  215. z: 8,
  216. hoverAnimation: false,
  217. data: [
  218. {
  219. value: this.value,
  220. itemStyle: {
  221. normal: {
  222. color: "transition",
  223. },
  224. },
  225. label: {
  226. normal: {
  227. formatter: function(params) {
  228. return params.value;
  229. },
  230. color: partten.getColor(this.color),
  231. fontSize: util.vh(16),
  232. fontWeight: "bold",
  233. position: "center",
  234. show: true,
  235. },
  236. },
  237. labelLine: {
  238. show: false,
  239. },
  240. },
  241. ],
  242. },
  243. // 光环
  244. {
  245. type: "pie",
  246. radius: "40%",
  247. center: ["50%", "50%"],
  248. animationType: "scale",
  249. animation: false,
  250. label: {
  251. show: false,
  252. },
  253. labelLine: {
  254. show: false,
  255. },
  256. data: [
  257. {
  258. value: 1,
  259. itemStyle: {
  260. normal: {
  261. color: "transparent",
  262. borderColor: partten.getColor(this.color),
  263. opacity: 0.3,
  264. shadowColor: partten.getColor(this.color),
  265. shadowBlur: util.vh(20),
  266. },
  267. },
  268. },
  269. ],
  270. },
  271. ],
  272. };
  273. this.chart.setOption(option);
  274. },
  275. },
  276. created() {
  277. this.id = "pie-chart-" + util.newGUID();
  278. },
  279. mounted() {
  280. this.$el.style.width = this.width;
  281. this.$el.style.height = this.height;
  282. this.chart = echarts.init(this.$el);
  283. this.initChart();
  284. },
  285. updated() {
  286. this.initChart();
  287. },
  288. };
  289. </script>
  290. <style lang="less" scoped>
  291. .chart {
  292. width: 100%;
  293. height: 100%;
  294. display: block;
  295. margin: auto;
  296. }
  297. </style>