pieNew.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <div id="pie_echart" ref="echart"></div>
  3. </template>
  4. <script>
  5. // let colors = [
  6. // ["#FC8053", "#F2CAA4"],
  7. // ["#5583e7", "#36dddb"],
  8. // ["#f888b1", "#fbe6ee"],
  9. // ];
  10. let colors = [
  11. "#62ADED",
  12. "#DFE15A",
  13. "#6EDC8D",
  14. "#00A42E",
  15. "#F9B447",
  16. "#7F4FE5",
  17. "#FF6969",
  18. "#27CED9",
  19. "#DF56F5",
  20. "#DCFFAF",
  21. ];
  22. let option = {
  23. backgroundColor: "rgba(0,0,0,0)",
  24. label: {
  25. //图例文字的样式
  26. color: "#fff",
  27. fontSize: 16,
  28. },
  29. title: {
  30. show: false,
  31. text: "暂无数据",
  32. x: "39%",
  33. y: "28%",
  34. textStyle: {
  35. // rich: {
  36. fontSize: 14,
  37. color: "#fff",
  38. fontWeight: "bold",
  39. },
  40. },
  41. legend: {
  42. // type: "scroll",
  43. orient: "vertical",
  44. right: "2%",
  45. top: "center",
  46. data: [],
  47. icon: "rect", // 这个字段控制形状 类型包括 circle,rect ,roundRect,triangle,diamond,pin,arrow,none
  48. itemWidth: 12, // 设置宽度
  49. itemHeight: 12, // 设置高度
  50. itemGap: 10, // 设置间距
  51. textStyle: {
  52. //图例文字的样式
  53. color: "#fff",
  54. fontSize: 14,
  55. },
  56. textStyle: {
  57. rich: {
  58. name: {
  59. color: "#fff", //#BCD3E5
  60. fontSize: 14,
  61. width: 60,
  62. },
  63. value: {
  64. color: "#64DAFF",
  65. fontSize: 14,
  66. padding: [5, 4],
  67. align: "center",
  68. width: 100,
  69. },
  70. unit: {
  71. color: "#fff", //#BCD3E5
  72. fontSize: 14,
  73. width: 5,
  74. },
  75. },
  76. },
  77. },
  78. graphic: [
  79. {
  80. type: "group",
  81. top: "middle",
  82. left: "center",
  83. id: "data",
  84. children: [
  85. // {
  86. // type: "text",
  87. // id: "current",
  88. // top: 40,
  89. // style: {
  90. // text: dataAll,
  91. // font: 'bolder 36px "Microsoft YaHei", sans-serif',
  92. // fill: "#fff",
  93. // textAlign: "center"
  94. // }
  95. // },
  96. // {
  97. // type: "text",
  98. // id: "all",
  99. // top: 80,
  100. // left:'10%',
  101. // style: {
  102. // text: "报建数量",
  103. // font: 'bolder 14px "Microsoft YaHei", sans-serif',
  104. // fill: "#fff",
  105. // // textAlign: "center"
  106. // }
  107. // }
  108. ],
  109. },
  110. ],
  111. series: [
  112. {
  113. name: "轮次",
  114. type: "pie",
  115. radius: ["45%", "80%"],
  116. center: ["20%", "50%"],
  117. avoidLabelOverlap: false,
  118. label: {
  119. normal: {
  120. show: false,
  121. position: "inner", // 数值显示在内部
  122. formatter: (params) => {
  123. return `${params.name} : ${params.value} "km²`;
  124. },
  125. },
  126. textStyle: {
  127. //图例文字的样式
  128. color: "#fff",
  129. fontSize: 16,
  130. },
  131. },
  132. emphasis: {
  133. label: {
  134. show: true,
  135. fontSize: "16",
  136. fontWeight: "bold",
  137. },
  138. },
  139. labelLine: {
  140. show: false,
  141. length: 48,
  142. },
  143. data: [],
  144. },
  145. ],
  146. grid: {
  147. left: "0%",
  148. // right: "40%",
  149. // bottom: "10%",
  150. top: "16%",
  151. containLabel: true,
  152. },
  153. };
  154. export default {
  155. name: "pie_echart",
  156. props: {
  157. unit: {
  158. type: String,
  159. default: "km²", //"平方千米",
  160. },
  161. },
  162. components: {},
  163. data() {
  164. return {
  165. myChart: null,
  166. };
  167. },
  168. methods: {
  169. setOptions(cartData) {
  170. let _this = this;
  171. if (!this.myChart) {
  172. // var dom = document.getElementById("pie_echart");
  173. this.myChart = echarts.init(this.$refs.echart);
  174. }
  175. cartData.data.forEach((item, index) => {
  176. option.legend.data.push(item.name);
  177. item.itemStyle = {
  178. color: colors[index % colors.length],
  179. // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  180. // {
  181. // offset: 0,
  182. // color: colors[index % colors.length],
  183. // },
  184. // {
  185. // offset: 1,
  186. // color: colors[index % colors.length],
  187. // },
  188. // ]),
  189. };
  190. // dataAll += item.value;
  191. });
  192. if (cartData.type == "vertical") {
  193. option.series[0].radius = ["27%", "42%"];
  194. option.series[0].center = ["50%", "30%"];
  195. option.legend.height = "40%";
  196. option.legend.top = "60%";
  197. option.legend.orient = "horizontal";
  198. } else {
  199. option.series[0].radius = ["45%", "70%"];
  200. option.series[0].center = ["25%", "50%"];
  201. option.legend.height = "100%";
  202. option.legend.top = "center";
  203. option.legend.orient = "vertical";
  204. }
  205. option.legend.right = cartData.legend_right || "2%";
  206. let max = cartData.max || 4;
  207. option.legend.textStyle.rich.name.width = max * 15;
  208. option.legend.formatter = function (name) {
  209. const sItem = cartData.data.find((item) =>
  210. `${item.name}`.includes(`${name}`)
  211. );
  212. if (sItem) {
  213. let aname = name.length > max ? name.substr(0, max) + "..." : name;
  214. return `{name|${aname}} {value|${sItem.value}} {unit|${_this.$props.unit}} `;
  215. // return name + 'sItem.value';
  216. } else {
  217. return name;
  218. }
  219. };
  220. option.series[0].data = cartData.data;
  221. option.series[0].label.normal.formatter = function (params) {
  222. return `${params.name} : ${params.value} ${_this.$props.unit}`;
  223. };
  224. if (cartData.data.length <= 0) {
  225. option.title.show = true;
  226. } else {
  227. option.title.show = false;
  228. }
  229. this.myChart.resize();
  230. this.myChart.setOption(option);
  231. // if (cartData.isclick) {
  232. // legendselectchanged
  233. this.myChart.on("legendselectchanged", function (params) {
  234. let iseyes = params.selected[params.name];
  235. _this.$emit("echartClick", params.name, { iseyes });
  236. });
  237. this.myChart.on("click", function (params) {
  238. _this.$emit("echartClick", params.name, { color: params.color });
  239. });
  240. // }
  241. },
  242. },
  243. mounted() {
  244. // this.setOptions();
  245. },
  246. };
  247. </script>
  248. <style lang="scss" scoped>
  249. .pie_echart {
  250. width: 100%;
  251. height: 100%;
  252. }
  253. </style>