EchartsDoughnut.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div ref="myChartPie" :style="{ width: width, height: height }"></div>
  3. </template>
  4. <script>
  5. import { mapState } from 'vuex';
  6. import { formatNumberToDollar } from '@/filters';
  7. // 引入基本模板
  8. let echarts = require('echarts/lib/echarts');
  9. // 引入柱状图组件
  10. require('echarts/lib/chart/bar');
  11. // 引入提示框和title组件
  12. require('echarts/lib/component/tooltip');
  13. require('echarts/lib/component/title');
  14. import { PieChart } from 'echarts/charts';
  15. echarts.use([PieChart]);
  16. export default {
  17. props: {
  18. // 数据
  19. pieData: {
  20. type: Object,
  21. default() {
  22. return {
  23. title: '', // 标题
  24. data: [], // 数据, { name, value }
  25. count: 0, // 总数
  26. };
  27. },
  28. },
  29. // 宽度
  30. width: {
  31. type: String,
  32. default: '100%',
  33. },
  34. // 高度
  35. height: {
  36. type: String,
  37. default: '400px',
  38. },
  39. },
  40. components: {},
  41. data() {
  42. return {};
  43. },
  44. created() {},
  45. mounted() {},
  46. beforeDestroy() {},
  47. watch: {
  48. // 监听浏览器可视区域宽度是否变化
  49. screenWidth() {
  50. this.resizeByWindowFun(); // 初始化echarts图表大小
  51. },
  52. },
  53. computed: {
  54. ...mapState({
  55. screenWidth: (state) => state.common.screenWidth,
  56. }),
  57. },
  58. methods: {
  59. // 初始化数据
  60. initDataFun(pieData) {
  61. // 基于准备好的dom,初始化echarts实例
  62. let myChart = echarts.init(this.$refs.myChartPie);
  63. let data = genData(pieData.data);
  64. // 绘制图表
  65. myChart.setOption({
  66. tooltip: {
  67. trigger: 'item',
  68. formatter: '{a} <br/>{b} : {c} ({d}%)',
  69. }, // 移入的提示
  70. legend: {
  71. orient: 'vertical',
  72. icon: 'circle',
  73. radius: '100%',
  74. left: 10,
  75. top: 10,
  76. bottom: 10,
  77. data: data.legendData,
  78. selected: data.selected,
  79. }, // 颜色标识标签
  80. series: [
  81. {
  82. name: pieData.seriesLabel,
  83. type: 'pie',
  84. radius: ['35%', '60%'], // 第一项是内半径,第二项是外半径,内半径为0就是真的饼,不是环形
  85. avoidLabelOverlap: false,
  86. data: data.seriesData,
  87. label: {
  88. show: true,
  89. position: 'center',
  90. formatter: ` ${pieData.title}\r\n ${formatNumberToDollar(
  91. pieData.count
  92. )}`,
  93. textStyle: {
  94. fontSize: '20',
  95. fontWeight: 'bold',
  96. color: 'rgba(0, 0, 0, 0.65)',
  97. },
  98. },
  99. emphasis: {
  100. itemStyle: {
  101. shadowBlur: 10,
  102. shadowOffsetX: 0,
  103. shadowColor: 'rgba(0, 0, 0, 0.5)',
  104. },
  105. },
  106. },
  107. ],
  108. });
  109. function genData(dataList) {
  110. let legendData = [];
  111. let seriesData = [];
  112. dataList.forEach((item) => {
  113. legendData.push(item.name);
  114. seriesData.push({
  115. name: item.name,
  116. value: item.value,
  117. });
  118. });
  119. return {
  120. legendData,
  121. seriesData,
  122. };
  123. }
  124. },
  125. // 根据窗口大小 初始化echarts图表大小
  126. resizeByWindowFun() {
  127. echarts.init(this.$refs.myChartPie).resize();
  128. },
  129. },
  130. };
  131. </script>
  132. <style lang="less"></style>
  133. <style lang="less" scoped></style>