pieChart.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div class="chart" :id="id" />
  3. </template>
  4. <script>
  5. const colors = ["#db6749", "#f8cd55", "#3c97d6", "#989bf8"];
  6. import * as echarts from 'echarts'
  7. export default {
  8. props: {
  9. id: { type: String, required: true },
  10. dataset: { type: Array, required: true },
  11. encode: { type: Object, required: true },
  12. labelFormat: { type: Array, required: false },
  13. isBig: { type: Boolean, default: false },
  14. colorArr: { type: Array, default: () => colors },
  15. radiusArr: { type: Array, default: () => ["20%", "60%"] },
  16. centerArr: { type: Array, default: () => ["50%", "50%"] },
  17. legend: { type: Object },
  18. title: { type: Object },
  19. startAngle: { type: Number, default: 45 },
  20. labelSize: { type: Number, default: 12 },
  21. // labelLineHeight: { type: Number, default: 5 },
  22. outCircle: { type: Boolean, default: false },
  23. innerCircle: { type: Boolean, default: false },
  24. roseType: { type: String | Boolean, default: "radius" },
  25. withBorder: { type: Boolean, default: false },
  26. hoverIn: { type: Boolean, default: true }, // 是否鼠标悬浮效果
  27. tooltip: { type: Function },
  28. },
  29. data() {
  30. const outRadius = this.outCircle
  31. ? parseInt(this.radiusArr[1].match(/([0-9]*)%/)[1]) + 5 + "%"
  32. : 0;
  33. const innerRadius = this.innerCircle
  34. ? parseInt(this.radiusArr[0].match(/([0-9]*)%/)[1]) - 10 + "%"
  35. : 0;
  36. return {
  37. option: {
  38. color: this.colorArr,
  39. title: {},
  40. legend: {
  41. icon: "circle",
  42. show: this.legend,
  43. textStyle: {
  44. color: "#8BDFFD",
  45. fontSize: this.isBig ? 18 : 12,
  46. },
  47. ...this.legend,
  48. },
  49. tooltip: {
  50. show: this.tooltip,
  51. backgroundColor: "t",
  52. formatter: this.tooltip,
  53. },
  54. dataset: { source: [] },
  55. series: [
  56. {
  57. type: "pie",
  58. hoverAnimation: this.hoverIn,
  59. silent: !this.hoverIn,
  60. radius: this.radiusArr,
  61. center: this.centerArr,
  62. roseType: this.roseType,
  63. startAngle: this.startAngle,
  64. minAngle: 10,
  65. avoidLabelOverlap: true,
  66. labelLine: {
  67. length: 20,
  68. length2: 10,
  69. lineStyle: { type: "solid", width: 2 },
  70. },
  71. itemStyle: this.withBorder
  72. ? { borderColor: "#020F59", borderWidth: 10 }
  73. : null,
  74. encode: this.encode,
  75. label: {
  76. show: this.labelFormat,
  77. fontSize: this.labelSize,
  78. formatter: this.labelFormat && this.labelFormat.join("\n"),
  79. rich: {
  80. hr: {
  81. backgroundColor: "t",
  82. width: 3,
  83. height: 3,
  84. borderRadius: 3,
  85. fontSize: this.isBig ? 18 : this.labelSize,
  86. padding: [3, 3, 0, -12],
  87. },
  88. name: {
  89. color: "#8BDFFD",
  90. align: "center",
  91. lineHeight: this.labelSize,
  92. padding: this.isBig ? [-20, 10, -40, 10] : [-20, 10, -30, 10],
  93. fontSize: this.isBig ? 18 : this.labelSize,
  94. height: 20,
  95. },
  96. sub: {
  97. color: "#ffffff",
  98. lineHeight: this.labelSize,
  99. padding: [-20, 10, 0, 10],
  100. fontSize: this.isBig ? 18 : this.labelSize,
  101. },
  102. },
  103. },
  104. },
  105. {
  106. name: "外圈",
  107. type: "pie",
  108. labelLine: false,
  109. show: this.outCircle,
  110. radius: [outRadius, outRadius],
  111. data: [1],
  112. itemStyle: {
  113. borderColor: "#03298B",
  114. borderWidth: 2,
  115. },
  116. },
  117. {
  118. name: "里圈",
  119. type: "pie",
  120. labelLine: false,
  121. show: this.innerCircle,
  122. radius: innerRadius,
  123. data: [1],
  124. itemStyle: {
  125. color: "#0249E7",
  126. },
  127. },
  128. ],
  129. },
  130. };
  131. },
  132. watch: {
  133. dataset: function (newV) {
  134. const option = this.option;
  135. option.dataset.source = newV;
  136. this.pieChart.setOption(option);
  137. },
  138. title: function (newV) {
  139. const option = this.option;
  140. option.title = newV;
  141. this.pieChart.setOption(option);
  142. },
  143. },
  144. mounted() {
  145. const option = this.option;
  146. option.dataset.source = this.dataset;
  147. option.title = this.title;
  148. this.pieChart = echarts.init($(`#${this.id}`)[0]);
  149. this.pieChart.setOption(option);
  150. },
  151. };
  152. </script>
  153. <style lang="less" scoped>
  154. .chart {
  155. width: 100%;
  156. height: 100%;
  157. }
  158. </style>