pieContentChart.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <div class="pieContentChart" ref="pieContentChart" />
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. export default {
  7. name: '',
  8. props: {
  9. // title: {
  10. // type: String,
  11. // default: ''
  12. // },
  13. // yAxisNames: {
  14. // type: Array,
  15. // default: []
  16. // },
  17. // yAxisNumbers: {
  18. // type: Array,
  19. // default: []
  20. // },
  21. // seriesData: {
  22. // type: Array,
  23. // default: []
  24. // }
  25. },
  26. data () {
  27. return {
  28. uEcharts: null,
  29. uOption: null,
  30. pieContentData: [
  31. {
  32. name: "材料费",
  33. value: 411,
  34. unit: '万元'
  35. },
  36. {
  37. name: "外协费",
  38. value: 417,
  39. unit: '万元'
  40. },
  41. {
  42. name: "事务费",
  43. value: 221,
  44. unit: '万元'
  45. },
  46. {
  47. name: "专用费",
  48. value: 121,
  49. unit: '万元'
  50. },
  51. {
  52. name: "人工费",
  53. value: 127,
  54. unit: '万元'
  55. },
  56. ]
  57. }
  58. },
  59. watch: {
  60. seriesData: {
  61. handler(newValue, oldValue){
  62. console.log(newValue, oldValue)
  63. if(newValue){
  64. this.setSeriesData()
  65. }
  66. },
  67. immediate: true
  68. }
  69. },
  70. created () {
  71. },
  72. mounted () {
  73. this.$nextTick(() => {
  74. this.initChart()
  75. })
  76. },
  77. methods: {
  78. initChart(){
  79. var _this = this
  80. this.uEcharts = echarts.init(this.$refs.pieContentChart)
  81. let dashedPic = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAM8AAAAOBAMAAAB6G1V9AAAAD1BMVEX////Kysrk5OTj4+TJycoJ0iFPAAAAG0lEQVQ4y2MYBaNgGAMTQQVFOiABhlEwCugOAMqzCykGOeENAAAAAElFTkSuQmCC';
  82. let color = ['#42CEAF', '#FF882A', '#16B4FE', '#747AFE'];
  83. let arrName = [];
  84. let arrValue = [];
  85. let sum = 0;
  86. let pieSeries = [],
  87. lineYAxis = [];
  88. // 数据处理
  89. this.pieContentData.forEach((v, i) => {
  90. arrName.push(v.name);
  91. arrValue.push(v.value);
  92. sum = sum + v.value;
  93. })
  94. // 图表option整理
  95. this.pieContentData.forEach((v, i) => {
  96. pieSeries.push({
  97. name: '学历',
  98. type: 'pie',
  99. clockWise: false,
  100. hoverAnimation: false,
  101. radius: [65 - i * 15 + '%', 57 - i * 15 + '%'],
  102. center: ["35%", "50%"],
  103. label: {
  104. show: false
  105. },
  106. data: [{
  107. value: v.value,
  108. name: v.name
  109. }, {
  110. value: sum - v.value,
  111. name: '',
  112. itemStyle: {
  113. color: "rgba(0,0,0,0)"
  114. }
  115. }]
  116. });
  117. pieSeries.push({
  118. name: '',
  119. type: 'pie',
  120. silent: true,
  121. z: 1,
  122. clockWise: false, //顺时加载
  123. hoverAnimation: false, //鼠标移入变大
  124. radius: [65 - i * 15 + '%',57 - i * 15 + '%'],
  125. center: ["35%", "50%"],
  126. label: {
  127. show: false
  128. },
  129. data: [{
  130. value: 7.5,
  131. itemStyle: {
  132. color: "#E3F0FF"
  133. }
  134. }, {
  135. value: 2.5,
  136. name: '',
  137. itemStyle: {
  138. color: "rgba(0,0,0,0)"
  139. }
  140. }]
  141. });
  142. v.percent = (v.value / sum * 100).toFixed(1) + "%";
  143. lineYAxis.push({
  144. value: i,
  145. textStyle: {
  146. rich: {
  147. circle: {
  148. color: color[i],
  149. padding: [0, 5]
  150. }
  151. }
  152. }
  153. });
  154. })
  155. // 配置参数
  156. this.uOption = {
  157. color: color,
  158. grid: {
  159. top: '15%',
  160. bottom: '55%',
  161. left: "30%",
  162. containLabel: false
  163. },
  164. yAxis: [{
  165. type: 'category',
  166. inverse: true,
  167. axisLine: {
  168. show: false
  169. },
  170. axisTick: {
  171. show: false
  172. },
  173. axisLabel: {
  174. formatter: function(params) {
  175. let item = _this.pieContentData[params];
  176. console.log(item)
  177. return '{line|}{circle|●}{name|'+ item.name +'}{bd||}{percent|'+item.percent+'}{value|'+ item.value+'}{unit|元}'
  178. },
  179. interval: 0,
  180. inside: true,
  181. textStyle: {
  182. color: "#333",
  183. fontSize: 14,
  184. rich: {
  185. line: {
  186. width: 40,
  187. height: 10,
  188. backgroundColor: {image: dashedPic}
  189. },
  190. name: {
  191. color: '#666',
  192. fontSize: 14,
  193. },
  194. bd: {
  195. color: '#ccc',
  196. padding: [0, 5],
  197. fontSize: 14,
  198. },
  199. percent:{
  200. color: '#333',
  201. fontSize: 14,
  202. },
  203. value: {
  204. color: '#333',
  205. fontSize: 12,
  206. fontWeight: 500,
  207. padding: [0, 0, 0, 10]
  208. },
  209. unit: {
  210. fontSize: 14
  211. }
  212. }
  213. },
  214. show: true
  215. },
  216. data: lineYAxis
  217. }],
  218. xAxis: [{
  219. show: false
  220. }],
  221. series: pieSeries
  222. }
  223. // 使用刚指定的配置项和数据显示图表
  224. this.uOption && this.uEcharts.setOption(this.uOption);
  225. window.addEventListener('resize', ()=>{
  226. this.uEcharts.resize()
  227. })
  228. },
  229. setSeriesData(){
  230. console.log()
  231. console.log(this.uEcharts)
  232. }
  233. }
  234. }
  235. </script>
  236. <style lang="less" scoped>
  237. .pieContentChart{
  238. width: 100%;
  239. height: 100%;
  240. }
  241. </style>