StackBarChart.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <div :id="baseConfig.ids" :style="{ width: width, height: height }"></div>
  3. </template>
  4. <script>
  5. import resize from "./resize";
  6. export default {
  7. props: {
  8. data: {
  9. default: () => ({ name: [], data: [] }),
  10. },
  11. width: {
  12. type: String,
  13. default: "100%",
  14. },
  15. height: {
  16. type: String,
  17. default: "100%",
  18. },
  19. baseConfig: {
  20. default: function () {
  21. return {
  22. ids: "EchartsBar",
  23. title: "",
  24. unit: "人",
  25. };
  26. },
  27. },
  28. },
  29. mixins: [resize],
  30. data() {
  31. return {
  32. chart: null,
  33. colorConfig: [
  34. { code: "100001", name: "人员定位", color: "#00E1FF" }, // 人员
  35. { code: "100002", name: "周界入侵", color: "#00B387" }, //周界
  36. { code: "100003", name: "火灾监测", color: "#00FFCC" }, // 火灾
  37. { code: "100004", name: "激光云台", color: "#00B0B0" }, // 泄漏
  38. { code: "100001", name: "人员报警", color: "#FFD552" },
  39. { code: "100002", name: "周界入侵", color: "#02F2F9" },
  40. { code: "100003", name: "火灾监测", color: "#007DFF" },
  41. ],
  42. colors: [
  43. "#FF83DF",
  44. "#8023FF",
  45. "#0064FF",
  46. "#00ACFF",
  47. "#50E3C2",
  48. "#39CD1A",
  49. "#B8E986",
  50. "#F8E71C",
  51. "#FF9F00",
  52. "#EC576A",
  53. ],
  54. };
  55. },
  56. mounted() {
  57. this.drawChart();
  58. },
  59. beforeDestroy() {
  60. if (!this.chart) {
  61. return;
  62. }
  63. this.chart.dispose();
  64. this.chart = null;
  65. },
  66. watch: {
  67. data: {
  68. //深度监听,可监听到对象、数组的变化
  69. handler() {
  70. this.$nextTick(() => {
  71. this.drawChart();
  72. });
  73. },
  74. deep: true,
  75. },
  76. },
  77. methods: {
  78. drawChart() {
  79. this.chart = this.$echarts.init(document.getElementById(this.baseConfig.ids));
  80. let option = {
  81. title: {
  82. show: false,
  83. },
  84. grid: {
  85. left: 0,
  86. right: 0,
  87. top: 0,
  88. bottom: 50,
  89. containLabel: true,
  90. },
  91. tooltip: {
  92. trigger: "axis",
  93. axisPointer: {
  94. type: "shadow",
  95. },
  96. // formatter: "{b}: {c}" + this.baseConfig.unit,
  97. },
  98. color: this.colors,
  99. legend: {
  100. left: "center",
  101. right: 0,
  102. bottom: 0,
  103. width: "70%",
  104. textStyle: {
  105. color: "#fff",
  106. },
  107. // itemWidth: 20,
  108. },
  109. yAxis: [
  110. {
  111. type: "category",
  112. data: this.data.name,
  113. axisLabel: {
  114. textStyle: {
  115. color: "#fff",
  116. },
  117. margin: 10,
  118. // interval: 0,
  119. },
  120. axisTick: {
  121. show: false,
  122. },
  123. axisLine: {
  124. show: true,
  125. lineStyle: {
  126. color: "#04bbff",
  127. },
  128. },
  129. },
  130. ],
  131. xAxis: {
  132. axisLine: {
  133. show: false,
  134. },
  135. axisTick: {
  136. show: false,
  137. },
  138. axisLabel: {
  139. textStyle: {
  140. color: "#fff",
  141. },
  142. },
  143. axisLine: {
  144. show: true,
  145. lineStyle: {
  146. color: "#04bbff",
  147. },
  148. },
  149. splitLine: {
  150. show: false,
  151. },
  152. },
  153. series: this.data.data.map((item, index) => {
  154. return {
  155. name: item.name,
  156. type: "bar",
  157. stack: "workPlan",
  158. // barWidth: 24,
  159. barMaxWidth: 16,
  160. itemStyle: {
  161. opacity: 0.8,
  162. },
  163. emphasis: {
  164. itemStyle: {
  165. opacity: 1,
  166. },
  167. },
  168. data: item.data,
  169. };
  170. }),
  171. };
  172. this.chart.setOption(option);
  173. },
  174. },
  175. };
  176. </script>