index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div class="w-full h-full">
  3. <div class="c-title">可靠性完成情况</div>
  4. <div
  5. ref="barChart2"
  6. id="barChart2"
  7. :style="{ height: height, width: width }"
  8. ></div>
  9. </div>
  10. </template>
  11. <script>
  12. import * as echarts from "echarts";
  13. export default {
  14. props: {
  15. chartOptions: {
  16. type: Object,
  17. default: () => {},
  18. },
  19. width: {
  20. type: String,
  21. default: "100%",
  22. },
  23. height: {
  24. type: String,
  25. default: "200px",
  26. },
  27. },
  28. mounted() {
  29. this.initEcharts();
  30. },
  31. watch: {
  32. chartOptions: {
  33. handler(newVal, oldVal) {
  34. this.initEcharts();
  35. },
  36. // 这里的deep是深度监听,因为我们传递过来的是一个对象
  37. deep: true,
  38. },
  39. },
  40. methods: {
  41. initEcharts() {
  42. let myChart = echarts.getInstanceByDom(
  43. document.getElementById("barChart2")
  44. );
  45. if (myChart == null) {
  46. myChart = echarts.init(document.getElementById("barChart2"));
  47. }
  48. // 指定图表的配置项和数据
  49. var option = {
  50. tooltip: {
  51. trigger: "item",
  52. },
  53. legend: {
  54. top: "5%",
  55. left: "center",
  56. },
  57. grid: {
  58. left: "2%", //图表距离容器左侧多少距离
  59. right: "2%", //图表距离容器右侧侧多少距离
  60. bottom: "0%", //图表距离容器上面多少距离
  61. top: "2%", //图表距离容器下面多少距离
  62. containLabel: true,
  63. },
  64. series: [
  65. {
  66. name: "Access From",
  67. type: "pie",
  68. center: ["50%", "60%"],
  69. radius: ["40%", "70%"],
  70. avoidLabelOverlap: false,
  71. label: {
  72. show: false,
  73. position: "center",
  74. },
  75. emphasis: {
  76. label: {
  77. show: true,
  78. fontSize: 40,
  79. fontWeight: "bold",
  80. },
  81. },
  82. labelLine: {
  83. show: false,
  84. },
  85. data: [
  86. { value: 1048, name: "Search Engine" },
  87. { value: 735, name: "Direct" },
  88. { value: 580, name: "Email" },
  89. { value: 484, name: "Union Ads" },
  90. { value: 300, name: "Video Ads" },
  91. ],
  92. },
  93. ],
  94. };
  95. // 使用刚指定的配置项和数据显示图表。
  96. myChart.setOption(option);
  97. window.addEventListener("resize", () => {
  98. myChart.resize();
  99. });
  100. },
  101. },
  102. };
  103. </script>
  104. <style lang="scss" scoped>
  105. .c-title {
  106. border-left: 4px solid rgb(64, 158, 255);
  107. padding-left: 10px;
  108. }
  109. </style>