pie.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <div class="charts-box" ref="eChart" id="main"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts';
  6. export default {
  7. props: {
  8. //图表数据
  9. chartData: {
  10. type: Array,
  11. default: () => {},
  12. },
  13. },
  14. data() {
  15. return {};
  16. },
  17. watch:{
  18. chartData(newVal, oldVal){
  19. console.log(newVal, oldVal);
  20. this.init()
  21. }
  22. },
  23. mounted() {
  24. console.log(this.chartData);
  25. this.init()
  26. },
  27. methods: {
  28. init() {
  29. var chartDom = this.$refs.eChart
  30. var myChart = echarts.init(chartDom);
  31. var option;
  32. option = {
  33. tooltip: {
  34. trigger: 'axis',
  35. triggerOn: 'click',
  36. axisPointer: {
  37. // Use axis to trigger tooltip
  38. type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'
  39. },
  40. },
  41. legend: {
  42. bottom: '0%',
  43. left: 'center'
  44. },
  45. series: [{
  46. name: '',
  47. type: 'pie',
  48. radius: ['40%', '70%'],
  49. avoidLabelOverlap: false,
  50. itemStyle: {
  51. borderRadius: 10,
  52. borderColor: '#fff',
  53. borderWidth: 2
  54. },
  55. label: {
  56. show: false,
  57. position: 'center'
  58. },
  59. emphasis: {
  60. label: {
  61. show: true,
  62. fontSize: 40,
  63. fontWeight: 'bold'
  64. }
  65. },
  66. labelLine: {
  67. show: false
  68. },
  69. data: this.chartData
  70. }]
  71. };
  72. option && myChart.setOption(option);
  73. }
  74. },
  75. };
  76. </script>
  77. <style scoped>
  78. /* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
  79. .charts-box {
  80. width: 100%;
  81. height: 400px;
  82. }
  83. </style>