RankBar.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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: [], seriesData: [] }),
  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. // color: ['rgba(143,223,254,0.4)', 'rgba(0,168,255,0.4)'],
  34. colorConfig: [
  35. //颜色配置
  36. // { code: "1", name: "已处理", color: ["#23E490", "#00AE63"] }, //周界
  37. // { code: "0", name: "未处理", color: ["#FFB500", "#E9CC75"] }, // 火灾
  38. // { code: "100001", name: "人员报警", color: ["#A1A9FF", "#707CFF"] }, // 人员
  39. // { code: "100002", name: "周界防护", color: ["#23E490", "#00AE63"] }, //周界
  40. // { code: "100003", name: "火灾报警", color: ["#FFB500", "#E9CC75"] }, // 火灾
  41. // { code: "100004", name: "泄漏监测", color: ["#00E0C3", "#4AC6E2"] }, // 泄漏
  42. { code: "1", name: "已处理", color: "#00FFCC" }, //周界
  43. { code: "0", name: "未处理", color: "#00B0B0" }, // 火灾
  44. { code: "100001", name: "人员定位", color: "#00E1FF" }, // 人员
  45. { code: "100002", name: "周界入侵", color: "#00B387" }, //周界
  46. { code: "100003", name: "火灾监测", color: "#00FFCC" }, // 火灾
  47. { code: "100004", name: "激光云台", color: "#00B0B0" }, // 泄漏
  48. ],
  49. };
  50. },
  51. mounted() {
  52. this.drawChart();
  53. },
  54. beforeDestroy() {
  55. if (!this.chart) {
  56. return;
  57. }
  58. this.chart.dispose();
  59. this.chart = null;
  60. },
  61. watch: {
  62. data: {
  63. //深度监听,可监听到对象、数组的变化
  64. handler(newVal) {
  65. this.$nextTick(() => {
  66. this.drawChart();
  67. });
  68. },
  69. deep: true,
  70. },
  71. },
  72. methods: {
  73. drawChart() {
  74. this.chart = this.$echarts.init(document.getElementById(this.baseConfig.ids));
  75. let option = {
  76. title: {
  77. show: false,
  78. },
  79. grid: {
  80. left: "3%",
  81. right: "4%",
  82. top: "10%",
  83. bottom: "15%",
  84. containLabel: true,
  85. },
  86. // toolbox: {
  87. // feature: {
  88. // magicType: { show: true, type: ["line", "bar"] },
  89. // restore: { show: true },
  90. // saveAsImage: { show: true },
  91. // },
  92. // iconStyle: {
  93. // borderColor: "#fff",
  94. // },
  95. // emphasis: {
  96. // iconStyle: {
  97. // borderColor: "#409EFF",
  98. // },
  99. // },
  100. // },
  101. tooltip: {
  102. trigger: "axis",
  103. axisPointer: {
  104. type: "line",
  105. lineStyle: {
  106. type: "dashed",
  107. },
  108. },
  109. padding: 10,
  110. textStyle: {
  111. fontSize: 12,
  112. },
  113. },
  114. color: ["#00E1FF", "#00B387", "#00FFCC", "#00B0B0"],
  115. legend: {
  116. bottom: 0,
  117. itemWidth: 14,
  118. textStyle: {
  119. color: "#fff",
  120. },
  121. },
  122. yAxis: [
  123. {
  124. type: "category",
  125. data: this.data.name,
  126. inverse: true,
  127. axisLabel: {
  128. textStyle: {
  129. color: "#fff",
  130. },
  131. margin: 10,
  132. // interval: 0,
  133. },
  134. axisTick: {
  135. show: false,
  136. },
  137. axisLine: {
  138. show: true,
  139. lineStyle: {
  140. color: "#99FFFA",
  141. },
  142. },
  143. },
  144. ],
  145. xAxis: {
  146. axisTick: {
  147. show: true,
  148. },
  149. axisLabel: {
  150. textStyle: {
  151. color: "#fff",
  152. },
  153. },
  154. axisLine: {
  155. show: true,
  156. lineStyle: {
  157. color: "#99FFFA",
  158. },
  159. },
  160. splitLine: {
  161. show: false,
  162. },
  163. minInterval: 1,
  164. position: "top",
  165. },
  166. series: this.data.seriesData.map((item, index) => {
  167. return {
  168. name: item.name,
  169. stack: "total",
  170. type: "bar",
  171. // barWidth: 24,
  172. barMaxWidth: 30,
  173. itemStyle: {
  174. normal: {
  175. // borderColor: '#7dd8fe',
  176. color: (params) => {
  177. return this.colorConfig.find((c) => c.code === item.code).color;
  178. },
  179. opacity: 0.8,
  180. },
  181. emphasis: {
  182. opacity: 1,
  183. },
  184. },
  185. data: item.data,
  186. };
  187. }),
  188. };
  189. this.chart.setOption(option, true);
  190. },
  191. },
  192. };
  193. </script>