barChart.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div
  3. :class="className"
  4. :style="{
  5. height: height,
  6. width: width,
  7. background: background,
  8. marginRight: '16px',
  9. }"
  10. >
  11. <div ref="chart" class="chart"></div>
  12. </div>
  13. </template>
  14. <script>
  15. import echarts from "echarts";
  16. require("echarts/theme/macarons"); // echarts theme
  17. const animationDuration = 6000;
  18. export default {
  19. props: {
  20. className: {
  21. type: String,
  22. default: "chart",
  23. },
  24. width: {
  25. type: String,
  26. default: "100%",
  27. },
  28. height: {
  29. type: String,
  30. default: "464px",
  31. },
  32. background: {
  33. type: String,
  34. default: "#fff",
  35. },
  36. watcherList: {
  37. type: Array,
  38. default: () => [],
  39. },
  40. },
  41. data() {
  42. return {
  43. chart: null,
  44. people: [],
  45. quality: [],
  46. };
  47. },
  48. watch: {
  49. watcherList: function (newData, oldData) {
  50. if (newData !== oldData) {
  51. this.initChart();
  52. }
  53. },
  54. },
  55. methods: {
  56. initChart() {
  57. this.watcherList.forEach((item) => {
  58. this.people.push(item.watcher);
  59. this.quality.push(item.quality);
  60. });
  61. console.log("fithis.watcherListrst", this.watcherList);
  62. this.chart = echarts.init(this.$refs.chart, "macarons");
  63. this.chart.setOption({
  64. title: {
  65. text: "监督员质态统计",
  66. textStyle: {
  67. color: "#0F2B4C",
  68. },
  69. top: 10,
  70. left: 5,
  71. },
  72. tooltip: {
  73. trigger: "axis",
  74. axisPointer: {
  75. type: "shadow",
  76. },
  77. formatter: function (params) {
  78. var tar = params[0];
  79. return (
  80. "监督人:" + tar.name + "<br/>" + "质态数" + " : " + tar.value
  81. );
  82. },
  83. },
  84. xAxis: {
  85. type: "category",
  86. data: this.people,
  87. axisLabel: {
  88. rotate: 45, // 设置标签倾斜角度,单位为度
  89. },
  90. },
  91. yAxis: {
  92. type: "value",
  93. },
  94. series: [
  95. {
  96. data: this.quality,
  97. type: "bar",
  98. barWidth: "16px",
  99. itemStyle: {
  100. color: "#0147EB",
  101. },
  102. },
  103. ],
  104. });
  105. },
  106. },
  107. beforeDestroy() {
  108. if (!this.chart) {
  109. return;
  110. }
  111. this.chart.dispose();
  112. this.chart = null;
  113. },
  114. };
  115. </script>
  116. <style lang="scss">
  117. .chart {
  118. height: 100%;
  119. width: 100%;
  120. }
  121. </style>