dual-pie-chart.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "200px",
  19. },
  20. // 内部饼图数据
  21. innerData: {
  22. type: Array,
  23. default: () => [
  24. {
  25. value: 700,
  26. unit: "个",
  27. name: "行业大类1",
  28. },
  29. {
  30. value: 679,
  31. unit: "个",
  32. name: "行业大类2",
  33. },
  34. {
  35. value: 1548,
  36. unit: "个",
  37. name: "行业大类3",
  38. },
  39. ],
  40. },
  41. // 外部饼图数据
  42. outerData: {
  43. type: Array,
  44. default: () => [
  45. {
  46. value: 310,
  47. unit: "个",
  48. name: "邮件营销",
  49. },
  50. {
  51. value: 234,
  52. unit: "个",
  53. name: "联盟广告",
  54. },
  55. {
  56. value: 335,
  57. unit: "个",
  58. name: "视频广告",
  59. },
  60. {
  61. value: 548,
  62. unit: "个",
  63. name: "百度",
  64. },
  65. {
  66. value: 351,
  67. unit: "个",
  68. name: "谷歌",
  69. },
  70. {
  71. value: 247,
  72. unit: "个",
  73. name: "必应",
  74. },
  75. ],
  76. },
  77. // 颜色
  78. color: {
  79. type: String,
  80. default: "green",
  81. },
  82. },
  83. data() {
  84. return {
  85. id: "",
  86. chart: null,
  87. };
  88. },
  89. computed: {},
  90. methods: {
  91. initChart() {
  92. let legend1 = this.innerData.map((v) => v.name);
  93. let legend2 = this.outerData.map((v) => v.name);
  94. let legendData = [...legend1, ...legend2];
  95. let option = {
  96. tooltip: {
  97. trigger: "item",
  98. backgroundColor: "rgba(0,0,0,0.3)",
  99. textStyle: {
  100. color: "#fff",
  101. fontSize: util.vh(16),
  102. },
  103. },
  104. grid: {
  105. top: 32,
  106. left: 60,
  107. right: 40,
  108. bottom: 24,
  109. },
  110. legend: {
  111. orient: "vertical", //纵向图例
  112. right: "16",
  113. bottom: 32,
  114. itemWidth: 15,
  115. itemHeight: 15,
  116. //icon: 'circle',
  117. itemGap: 12, //图例item间距
  118. textStyle: {
  119. color: "#fff",
  120. fontSize: util.vh(12),
  121. },
  122. data: legend1,
  123. },
  124. series: [
  125. {
  126. name: "访问来源",
  127. type: "pie",
  128. center: ["40%", "50%"],
  129. radius: [0, "35%"],
  130. itemStyle: {
  131. normal: {},
  132. },
  133. label: {
  134. normal: {
  135. show: false,
  136. },
  137. },
  138. labelLine: {
  139. normal: {
  140. show: false,
  141. },
  142. },
  143. data: this.innerData,
  144. },
  145. {
  146. name: "访问来源",
  147. type: "pie",
  148. center: ["40%", "50%"],
  149. radius: ["55%", "95%"],
  150. data: this.outerData,
  151. labelLine: {
  152. normal: {
  153. // length: 40,
  154. // length2: 120,
  155. lineStyle: {
  156. color: "#fff",
  157. fontSize: util.vh(14),
  158. },
  159. },
  160. },
  161. itemStyle: {
  162. normal: {
  163. borderWidth: util.vh(12),
  164. borderColor: "#071812",
  165. },
  166. },
  167. label: {
  168. normal: {
  169. formatter: (params) => {
  170. return "{percent|" + params.percent.toFixed(2) + "%}";
  171. },
  172. padding: [0, 0, 0, 0],
  173. rich: {
  174. color: "#fff",
  175. percent: {
  176. fontSize: util.vh(12),
  177. color: "#fff",
  178. },
  179. },
  180. },
  181. },
  182. },
  183. ],
  184. };
  185. this.chart.setOption(option);
  186. }
  187. },
  188. created() {
  189. this.id = "pie-chart-" + util.newGUID();
  190. },
  191. mounted() {
  192. this.$el.style.width = this.width;
  193. this.$el.style.height = this.height;
  194. this.chart = echarts.init(this.$el);
  195. this.initChart();
  196. },
  197. updated() {
  198. this.initChart();
  199. },
  200. };
  201. </script>
  202. <style lang="less" scoped>
  203. .chart {
  204. width: 100%;
  205. height: 100%;
  206. display: block;
  207. margin: auto;
  208. }
  209. </style>