LiveChart.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <div class="LC">
  3. <div class="type">
  4. <p :class="class1[1]" @click="tab(1)">水电</p>
  5. <p :class="class1[0]" @click="tab(0)">热水</p>
  6. <p :class="class1[2]" @click="tab(2)">空调</p>
  7. </div>
  8. <div class="live" ref="live"></div>
  9. </div>
  10. </template>
  11. <script>
  12. import echarts from "echarts";
  13. import resize from "@/mixins/resize";
  14. import dayjs from "dayjs";
  15. export default {
  16. mixins: [resize],
  17. name: "LiveChart",
  18. data() {
  19. return {
  20. class1: ["", "select", ""],
  21. water: [],
  22. waterTime: [],
  23. electricity: [],
  24. electricityTime: [],
  25. data: [],
  26. time: [],
  27. };
  28. },
  29. mounted() {
  30. this.getData();
  31. },
  32. methods: {
  33. getData() {
  34. let params = { id: sessionStorage.getItem("id") };
  35. this.API.personal
  36. .energy(params)
  37. .then((res) => {
  38. // console.log(res);
  39. this.electricity = [];
  40. this.electricityTime = [];
  41. let electricity = res.data.electricityAndColdWater.list.reverse();
  42. electricity.forEach((item) => {
  43. this.electricityTime.push(dayjs(item.date).format("M") + "月");
  44. this.electricity.push(item.totalMoney);
  45. });
  46. this.water = [];
  47. this.waterTime = [];
  48. let water = res.data.hotWater.list;
  49. water.forEach((item) => {
  50. this.waterTime.push(dayjs(item.date).format("M") + "月");
  51. this.water.push(item.totalMoney);
  52. });
  53. this.tab(1);
  54. })
  55. .catch((error) => {
  56. console.log(error);
  57. });
  58. },
  59. fontSize(res) {
  60. let docEl = document.documentElement,
  61. clientWidth =
  62. window.innerWidth ||
  63. document.documentElement.clientWidth ||
  64. document.body.clientWidth;
  65. if (!clientWidth) return;
  66. let fontSize = 100 * (clientWidth / 1920);
  67. return res * fontSize;
  68. },
  69. tab(type) {
  70. // console.log(type);
  71. this.class1 = this.class1.map((item) => (item = ""));
  72. this.class1[type] = "select";
  73. if (type == 0) {
  74. this.data = this.water;
  75. this.time = this.waterTime;
  76. this.live();
  77. } else if (type == 1) {
  78. this.data = this.electricity;
  79. this.time = this.electricityTime;
  80. this.live();
  81. }
  82. // electricity: [],
  83. // electricityTime: [],
  84. },
  85. live() {
  86. this.myEcharts = echarts.init(this.$refs.live);
  87. this.myEcharts.setOption({
  88. tooltip: {},
  89. grid: {
  90. top: "30%",
  91. bottom: "15%",
  92. right: "5%",
  93. left: "27%",
  94. },
  95. xAxis: {
  96. type: "category",
  97. data: this.time,
  98. axisTick: {
  99. show: false, //刻度线是否显示
  100. },
  101. axisLabel: {
  102. color: "rgba(255,255,255,1)",
  103. fontSize: this.fontSize(0.12),
  104. },
  105. axisLine: {
  106. //---坐标轴 轴线
  107. show: true, //---是否显示
  108. //------------------- 线 -------------------------
  109. lineStyle: {
  110. color: "rgba(88, 162, 168, 0.14)",
  111. width: 1,
  112. type: "solid",
  113. },
  114. },
  115. },
  116. yAxis: {
  117. name: "元",
  118. nameTextStyle: {
  119. color: "rgba(255,255,255,1)",
  120. align: "right",
  121. verticalAlign: "top",
  122. lineHeight: this.fontSize(-0.15),
  123. fontSize: this.fontSize(0.12),
  124. },
  125. axisLabel: {
  126. color: "rgba(255,255,255,1)", // 坐标轴文字颜色
  127. fontSize: this.fontSize(0.12),
  128. },
  129. axisTick: {
  130. show: false, //刻度线是否显示
  131. },
  132. axisLine: {
  133. //---坐标轴 轴线
  134. show: true, //---是否显示
  135. //------------------- 线 -------------------------
  136. lineStyle: {
  137. color: "rgba(255,255,255,0)",
  138. width: 1,
  139. type: "solid",
  140. },
  141. },
  142. splitLine: {
  143. // 网格线的设置
  144. lineStyle: {
  145. // 使用深浅的间隔色
  146. color: "rgba(88, 162, 168, 0.14)",
  147. },
  148. },
  149. },
  150. // Declare several bar series, each will be mapped
  151. // to a column of dataset.source by default.
  152. series: [
  153. {
  154. type: "bar",
  155. data: this.data,
  156. barWidth: this.fontSize(0.2),
  157. itemStyle: {
  158. color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
  159. // color在这里
  160. {
  161. offset: 0,
  162. color: "rgba(0, 255, 240, 0.09)",
  163. },
  164. {
  165. offset: 1,
  166. color: "rgba(0, 255, 240, 1)",
  167. },
  168. ]),
  169. },
  170. },
  171. ],
  172. });
  173. },
  174. },
  175. };
  176. </script>
  177. <style scoped lang="less">
  178. .LC {
  179. width: 100%;
  180. height: 100%;
  181. position: relative;
  182. .type {
  183. position: absolute;
  184. top: 10px;
  185. left: 345px;
  186. z-index: 99;
  187. p {
  188. width: 50px;
  189. height: 23px;
  190. border-radius: 3px;
  191. box-sizing: border-box;
  192. display: inline-block;
  193. color: #fff;
  194. text-align: center;
  195. line-height: 20px;
  196. cursor: pointer;
  197. font-size: 12px;
  198. margin-right: 5px;
  199. background: rgba(0, 0, 0, 0.15);
  200. }
  201. .select {
  202. border: 1px solid #00b7ee;
  203. }
  204. }
  205. .live {
  206. width: 536px;
  207. height: 240px;
  208. }
  209. }
  210. </style>