dual-pie-chart.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 { left } from "@antv/x6/lib/registry/port-layout/line";
  9. export default {
  10. name: "dsah-pie",
  11. componentName: "dsah-pie",
  12. props: {
  13. width: {
  14. type: String,
  15. default: "100%",
  16. },
  17. height: {
  18. type: String,
  19. default: "18.519vh",
  20. },
  21. paddingWidth: {
  22. type: String,
  23. default: "30%",
  24. },
  25. // 内部饼图数据
  26. innerData: {
  27. type: Array,
  28. default: () => [
  29. {
  30. value: 700,
  31. unit: "个",
  32. name: "行业大类1",
  33. },
  34. {
  35. value: 679,
  36. unit: "个",
  37. name: "行业大类2",
  38. },
  39. {
  40. value: 1548,
  41. unit: "个",
  42. name: "行业大类3",
  43. },
  44. ],
  45. },
  46. // 外部饼图数据
  47. outerData: {
  48. type: Array,
  49. default: () => [],
  50. },
  51. // 颜色
  52. color: {
  53. type: String,
  54. default: "green",
  55. },
  56. showLegend: {
  57. type: Boolean,
  58. default: true,
  59. },
  60. },
  61. data() {
  62. return {
  63. id: "",
  64. chart: null,
  65. };
  66. },
  67. computed: {},
  68. methods: {
  69. initChart() {
  70. let that = this;
  71. let legend1 = this.innerData.map((v) => v.name);
  72. let legend2 = this.outerData.map((v) => v.name);
  73. let legendData = [...legend1, ...legend2];
  74. let option = {
  75. color: [
  76. "#e17e23",
  77. "#ba3237",
  78. "#c531c7",
  79. "#4b55ae",
  80. "#ccf0d3",
  81. "#05bb4c",
  82. ],
  83. tooltip: {
  84. trigger: "item",
  85. backgroundColor:
  86. this.$store.state.themeName === "dark"
  87. ? "rgba(0,0,0,0.4)"
  88. : "rgba(255,255,255,0.5)",
  89. borderColor:
  90. this.$store.state.themeName === "dark"
  91. ? partten.getColor("gray")
  92. : "#000",
  93. textStyle: {
  94. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  95. fontSize: util.vh(16),
  96. },
  97. },
  98. grid: {
  99. top: 32,
  100. left: 60,
  101. right: 40,
  102. bottom: 24,
  103. },
  104. // legend: {
  105. // // show: this.showLegend,
  106. // orient: "vertical", //纵向图例
  107. // right: "0",
  108. // bottom: 32,
  109. // itemWidth: 15,
  110. // itemHeight: 15,
  111. // formatter: (name) => {
  112. // if (that.innerData.length) {
  113. // const item = that.innerData.filter((item) => item.name === name)[0];
  114. // return `{name|${name}:}{value| ${item.value}}`;
  115. // }
  116. // },
  117. // //icon: 'circle',
  118. // itemGap: 12, //图例item间距
  119. // textStyle: {
  120. // color: this.$store.state.themeName === "dark"
  121. // ? partten.getColor("gray")
  122. // : "#000",
  123. // fontSize: util.vh(14),
  124. // rich: {
  125. // name: {
  126. // color: this.$store.state.themeName === "dark"
  127. // ? partten.getColor("gray")
  128. // : "#000",
  129. // fontSize: 12,
  130. // },
  131. // value: {
  132. // color: this.$store.state.themeName === "dark"
  133. // ? partten.getColor("grayl")
  134. // : "#000",
  135. // fontSize: 12,
  136. // },
  137. // },
  138. // },
  139. // data: legend1,
  140. // },
  141. series: [
  142. {
  143. name: "",
  144. type: "pie",
  145. // center: [this.paddingWidth, "50%"],
  146. center: ["50%", "50%"],
  147. radius: [0, "35%"],
  148. itemStyle: {
  149. normal: {},
  150. },
  151. label: {
  152. normal: {
  153. show: false,
  154. },
  155. },
  156. labelLine: {
  157. normal: {
  158. show: false,
  159. },
  160. },
  161. data: this.innerData,
  162. },
  163. {
  164. name: "",
  165. type: "pie",
  166. center: ["50%", "50%"],
  167. radius: ["55%", "95%"],
  168. data: this.outerData,
  169. labelLine: {
  170. normal: {
  171. // length: 40,
  172. // length2: 120,
  173. lineStyle: {
  174. color:
  175. this.$store.state.themeName === "dark" ? "#fff" : "#000",
  176. fontSize: util.vh(14),
  177. },
  178. },
  179. },
  180. itemStyle: {
  181. normal: {
  182. borderWidth:
  183. this.$store.state.themeName === "dark"
  184. ? util.vh(14)
  185. : util.vh(8),
  186. borderColor: "#071812",
  187. },
  188. },
  189. label: {
  190. normal: {
  191. formatter: (params) => {
  192. return "{percent|" + params.percent.toFixed(2) + "%}";
  193. },
  194. padding: [0, 0, 0, 0],
  195. rich: {
  196. color:
  197. this.$store.state.themeName === "dark" ? "#fff" : "#000",
  198. percent: {
  199. fontSize: util.vh(14),
  200. color:
  201. this.$store.state.themeName === "dark"
  202. ? "#FFFFFF"
  203. : "#000000",
  204. },
  205. },
  206. },
  207. },
  208. },
  209. ],
  210. };
  211. this.chart.setOption(option);
  212. },
  213. },
  214. created() {
  215. this.id = "pie-chart-" + util.newGUID();
  216. },
  217. mounted() {
  218. this.$el.style.width = this.width;
  219. this.$el.style.height = this.height;
  220. this.chart = echarts.init(this.$el);
  221. this.initChart();
  222. },
  223. updated() {
  224. this.initChart();
  225. },
  226. watch: {
  227. "$store.state.themeName"() {
  228. this.initChart();
  229. },
  230. },
  231. };
  232. </script>
  233. <style lang="less" scoped>
  234. .chart {
  235. width: 100%;
  236. height: 100%;
  237. display: block;
  238. margin: auto;
  239. }
  240. </style>