DateTimeBar.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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: () => ({
  10. date: [],
  11. seriesData: [],
  12. chartType: "",
  13. limitNumber: 30,
  14. }),
  15. },
  16. width: {
  17. type: String,
  18. default: "100%",
  19. },
  20. height: {
  21. type: String,
  22. default: "100%",
  23. },
  24. baseConfig: {
  25. default: function () {
  26. return {
  27. ids: "EchartsBar",
  28. title: "",
  29. unit: "人",
  30. };
  31. },
  32. },
  33. },
  34. mixins: [resize],
  35. data() {
  36. return {
  37. chart: null,
  38. colorConfig: [
  39. {
  40. name: "人员报警",
  41. code: "100001",
  42. color: "#FF83DF",
  43. },
  44. {
  45. name: "周界报警",
  46. code: "100002",
  47. color: "#8023FF",
  48. },
  49. {
  50. name: "火灾报警",
  51. code: "100003",
  52. color: "#00ACFF",
  53. },
  54. {
  55. name: "门禁报警",
  56. code: "100006",
  57. color: "#39CD1A",
  58. },
  59. {
  60. name: "生产报警",
  61. code: "100010",
  62. color: "#F8E71C",
  63. },
  64. {
  65. name: "阴保报警",
  66. code: "100011",
  67. color: "#EC576A",
  68. },
  69. ],
  70. colors: [
  71. "#FF83DF",
  72. "#8023FF",
  73. "#0064FF",
  74. "#00ACFF",
  75. "#50E3C2",
  76. "#39CD1A",
  77. "#B8E986",
  78. "#F8E71C",
  79. "#FF9F00",
  80. "#EC576A",
  81. ],
  82. dataZoomMix: 0,
  83. chartType: "bar",
  84. };
  85. },
  86. mounted() {
  87. this.drawChart();
  88. },
  89. beforeDestroy() {
  90. if (!this.chart) {
  91. return;
  92. }
  93. this.chart.dispose();
  94. this.chart = null;
  95. },
  96. watch: {
  97. data: {
  98. //深度监听,可监听到对象、数组的变化
  99. handler(newVal) {
  100. let length = newVal.date.length;
  101. //显示数据数量
  102. if (newVal.limitNumber === 0) {
  103. this.dataZoomMix = 0;
  104. } else {
  105. if (length > newVal.limitNumber) {
  106. this.dataZoomMix = (1 - newVal.limitNumber / length) * 100;
  107. } else {
  108. this.dataZoomMix = 0;
  109. }
  110. }
  111. //默认图表类型
  112. if (newVal.chartType || newVal.chartType === "") {
  113. this.chartType = "bar";
  114. } else {
  115. this.chartType = "line";
  116. }
  117. // console.log(this.dataZoomMix);
  118. this.$nextTick(() => {
  119. this.chart.clear();
  120. this.drawChart();
  121. });
  122. },
  123. deep: true,
  124. },
  125. },
  126. methods: {
  127. drawChart() {
  128. //$("#你的画布名称").removeAttr("_echarts_instance_").empty()
  129. this.chart = this.$echarts.init(
  130. document.getElementById(this.baseConfig.ids)
  131. );
  132. this.chart.off("click");
  133. let option = {
  134. title: {
  135. show: false,
  136. },
  137. grid: {
  138. left: "3%",
  139. right: "4%",
  140. top: "10%",
  141. bottom: "15%",
  142. containLabel: true,
  143. },
  144. toolbox: {
  145. feature: {
  146. // magicType: {
  147. // show: true,
  148. // type: ["line", "bar"],
  149. // },
  150. // restore: { show: true },
  151. // saveAsImage: { show: true },
  152. myTool1: {
  153. show: true,
  154. title: "切换折线图",
  155. icon: "path://M193.422222 648.533333l-45.511111-45.511111L398.222222 358.4l199.111111 199.111111 347.022223-352.711111 45.511111 45.511111-392.533334 386.844445L398.222222 438.044444z",
  156. onclick: () => {
  157. this.chartType = "line";
  158. this.chart.clear();
  159. this.drawChart();
  160. },
  161. },
  162. myTool2: {
  163. show: true,
  164. title: "切换柱状图",
  165. icon: "path://M194.56 386.048q31.744 0 46.592 19.456t14.848 43.008l0 447.488-192.512 0 0-443.392q0-31.744 19.968-49.152t43.52-17.408l67.584 0zM514.048 134.144q27.648 0 44.032 16.896t16.384 40.448l0 704.512-191.488 0 0-702.464q0-15.36 6.144-26.624t15.36-18.432 20.48-10.752 20.48-3.584l68.608 0zM832.512 577.536q9.216 0 19.968 5.12t20.48 14.336 16.384 20.992 6.656 25.088l0 252.928-191.488 0 0-250.88q0-11.264 4.608-23.552t12.8-22.016 19.456-15.872 24.576-6.144l66.56 0z",
  166. onclick: () => {
  167. this.chartType = "bar";
  168. this.chart.clear();
  169. this.drawChart();
  170. },
  171. },
  172. },
  173. iconStyle: {
  174. borderColor: "#fff",
  175. },
  176. emphasis: {
  177. iconStyle: {
  178. borderColor: "#409EFF",
  179. },
  180. },
  181. },
  182. dataZoom: [
  183. {
  184. show: true,
  185. start: this.dataZoomMix,
  186. end: 100,
  187. height: 20,
  188. bottom: 2,
  189. textStyle: {
  190. color: "#fff",
  191. },
  192. },
  193. ],
  194. tooltip: {
  195. trigger: "axis",
  196. axisPointer: {
  197. type: "line",
  198. lineStyle: {
  199. type: "dashed",
  200. },
  201. },
  202. padding: 10,
  203. textStyle: {
  204. fontSize: 12,
  205. },
  206. },
  207. color: [
  208. "#00ACFF",
  209. "#FF83DF",
  210. "#8023FF",
  211. "#0064FF",
  212. "#50E3C2",
  213. "#39CD1A",
  214. "#B8E986",
  215. "#F8E71C",
  216. "#FF9F00",
  217. "#EC576A",
  218. ],
  219. legend: {
  220. top: 2,
  221. itemWidth: 24,
  222. textStyle: {
  223. color: "#fff",
  224. },
  225. },
  226. xAxis: [
  227. {
  228. type: "category",
  229. data: this.data.date,
  230. axisLabel: {
  231. textStyle: {
  232. color: "#fff",
  233. },
  234. margin: 10,
  235. // interval: 0,
  236. },
  237. axisTick: {
  238. show: true,
  239. },
  240. axisLine: {
  241. show: true,
  242. lineStyle: {
  243. color: "#99FFFA",
  244. },
  245. },
  246. },
  247. ],
  248. yAxis: {
  249. axisTick: {
  250. show: true,
  251. },
  252. axisLabel: {
  253. textStyle: {
  254. color: "#fff",
  255. },
  256. },
  257. axisLine: {
  258. show: true,
  259. lineStyle: {
  260. color: "#99FFFA",
  261. },
  262. },
  263. splitLine: {
  264. show: false,
  265. // interval: 4,
  266. },
  267. minInterval: 1,
  268. boundaryGap: [0, "5%"],
  269. },
  270. series: this.data.seriesData.map((item, index) => {
  271. if (this.chartType === "bar") {
  272. return {
  273. name: item.name,
  274. stack: "total",
  275. type: this.chartType,
  276. barMaxWidth: 30,
  277. itemStyle: {
  278. normal: {
  279. opacity: 0.8,
  280. },
  281. emphasis: {
  282. opacity: 1,
  283. },
  284. },
  285. data: item.data,
  286. markPoint: {
  287. data: item.markPointData,
  288. },
  289. };
  290. } else {
  291. return {
  292. name: item.name,
  293. type: this.chartType,
  294. itemStyle: {
  295. normal: {
  296. opacity: 0.8,
  297. },
  298. emphasis: {
  299. opacity: 1,
  300. },
  301. },
  302. data: item.data,
  303. markPoint: {
  304. data: item.markPointData,
  305. },
  306. };
  307. }
  308. }),
  309. };
  310. // this.chart.clear();
  311. this.chart.setOption(option);
  312. this.chart.on("click", (params) => {
  313. this.$emit("handleChartClick", params);
  314. });
  315. },
  316. },
  317. };
  318. </script>