month.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <view>
  3. <canvas canvas-id="ChkmnEIgSCrtkEvqLErNxjIEChnJNtRR" id="ChkmnEIgSCrtkEvqLErNxjIEChnJNtRR" class="charts"
  4. @touchend="tap" />
  5. </view>
  6. </template>
  7. <script>
  8. import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/u-charts.js';
  9. import moment from "moment"
  10. import {
  11. Consume
  12. } from '@/pages/api/lighting/lighting.js'
  13. var uChartsInstance = {};
  14. export default {
  15. data() {
  16. return {
  17. cWidth: 750,
  18. cHeight: 250,
  19. agoDay: '', //一个月前的年月日
  20. nowDay: '', //现在的年月日
  21. //照明能耗
  22. enValue: [],
  23. Time: [],
  24. //亮灯率
  25. lightValue: [],
  26. };
  27. },
  28. onReady() {
  29. var that = this
  30. var date1 = new Date();
  31. var date2 = new Date(date1);
  32. //-30为30天前,+30可以获得30天后的日期
  33. date2.setDate(date1.getDate() - 30);
  34. //30天前(月份判断是否小于10,小于10的前面+0)
  35. var agoDay = `${date2.getFullYear()}-${date2.getMonth() + 1<10?`0${date2.getMonth() + 1}`:date2.getMonth() + 1}-${date2.getDate() < 10?`0${date2.getDate()}`:date2.getDate()}`;
  36. //当前日期
  37. var nowDay = `${date1.getFullYear()}-${date1.getMonth() + 1<10?`0${date1.getMonth() + 1}`:date1.getMonth() + 1}-${date1.getDate() < 10?`0${date1.getDate()}`:date1.getDate()}`;
  38. that.agoDay = agoDay
  39. that.nowDay = nowDay
  40. Consume('month', agoDay, nowDay).then(res => {
  41. if (res.code == 200) {
  42. let Time = []
  43. let enValue = []
  44. let lightValue = []
  45. let energy = res.data.energy
  46. var momentTime
  47. for (let i = 0; i < energy.length; i++) {
  48. enValue.push(energy[i].columnValue)
  49. momentTime = moment(energy[i].timeLine).format("YYYY-MM-DD HH:mm:ss")
  50. momentTime = momentTime.slice(0, 10)
  51. Time.push(momentTime)
  52. }
  53. let lightRate = res.data.lightRate
  54. for (let j = 0; j < lightRate.length; j++) {
  55. lightValue.push(lightRate[j].columnValue)
  56. }
  57. enValue.reverse()
  58. lightValue.reverse()
  59. Time.reverse()
  60. that.enValue = enValue
  61. that.lightValue = lightValue
  62. that.Time = Time
  63. }
  64. })
  65. //这里的 750 对应 css .charts 的 width
  66. that.cWidth = uni.upx2px(750);
  67. //这里的 500 对应 css .charts 的 height
  68. that.cHeight = uni.upx2px(250);
  69. that.getServerData();
  70. },
  71. methods: {
  72. getServerData() {
  73. var that = this
  74. //模拟从服务器获取数据时的延时
  75. setTimeout(() => {
  76. //模拟服务器返回数据,如果数据格式和标准格式不同,需自行按下面的格式拼接
  77. let res = {
  78. categories: that.Time,
  79. series: [{
  80. index: 0,
  81. name: "照明消耗",
  82. data: that.enValue
  83. },
  84. {
  85. index: 1,
  86. name: "亮灯率",
  87. data: that.lightValue
  88. }
  89. ]
  90. };
  91. that.drawCharts('ChkmnEIgSCrtkEvqLErNxjIEChnJNtRR', res);
  92. }, 1000);
  93. },
  94. drawCharts(id, data) {
  95. const ctx = uni.createCanvasContext(id, this);
  96. uChartsInstance[id] = new uCharts({
  97. type: "line",
  98. context: ctx,
  99. width: this.cWidth,
  100. height: this.cHeight,
  101. categories: data.categories,
  102. series: data.series,
  103. animation: true,
  104. timing: "easeOut",
  105. duration: 1000,
  106. rotate: false,
  107. rotateLock: false,
  108. background: "#FFFFFF",
  109. color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
  110. "#ea7ccc"
  111. ],
  112. padding: [15, 10, 0, 15],
  113. fontSize: 13,
  114. fontColor: "#666666",
  115. dataLabel: false,
  116. dataPointShape: true,
  117. dataPointShapeType: "solid",
  118. touchMoveLimit: 60,
  119. enableScroll: false,
  120. enableMarkLine: false,
  121. legend: {
  122. show: true,
  123. position: "bottom",
  124. float: "center",
  125. padding: 5,
  126. margin: 5,
  127. backgroundColor: "rgba(0,0,0,0)",
  128. borderColor: "rgba(0,0,0,0)",
  129. borderWidth: 0,
  130. fontSize: 13,
  131. fontColor: "#666666",
  132. lineHeight: 11,
  133. hiddenColor: "#CECECE",
  134. itemGap: 10
  135. },
  136. xAxis: {
  137. disableGrid: true,
  138. disabled: false,
  139. axisLine: true,
  140. axisLineColor: "#CCCCCC",
  141. calibration: true,
  142. fontColor: "#666666",
  143. fontSize: 10,
  144. lineHeight: 20,
  145. marginTop: 10,
  146. rotateLabel: true,
  147. rotateAngle: 45,
  148. itemCount: 5,
  149. boundaryGap: "center",
  150. splitNumber: 5,
  151. gridColor: "#CCCCCC",
  152. gridType: "solid",
  153. dashLength: 4,
  154. gridEval: 1,
  155. scrollShow: false,
  156. scrollAlign: "left",
  157. scrollColor: "#A6A6A6",
  158. scrollBackgroundColor: "#EFEBEF",
  159. title: "",
  160. titleFontSize: 13,
  161. titleOffsetY: 0,
  162. titleOffsetX: 0,
  163. titleFontColor: "#666666",
  164. formatter: ""
  165. },
  166. yAxis: {
  167. gridType: "dash",
  168. dashLength: 2,
  169. disabled: false,
  170. disableGrid: false,
  171. splitNumber: 5,
  172. gridColor: "#CCCCCC",
  173. padding: 10,
  174. showTitle: true,
  175. data: [{
  176. type: "value",
  177. position: "left",
  178. disabled: false,
  179. axisLine: true,
  180. axisLineColor: "#CCCCCC",
  181. calibration: false,
  182. fontColor: "#666666",
  183. fontSize: 13,
  184. textAlign: "right",
  185. title: "照明消耗",
  186. titleFontSize: 13,
  187. titleOffsetY: 0,
  188. titleOffsetX: 0,
  189. titleFontColor: "#666666",
  190. min: 0,
  191. max: 70,
  192. tofix: null,
  193. unit: "kW.h",
  194. formatter: ""
  195. },
  196. {
  197. type: "value",
  198. position: "right",
  199. disabled: false,
  200. axisLine: true,
  201. axisLineColor: "#CCCCCC",
  202. calibration: false,
  203. fontColor: "#666666",
  204. fontSize: 13,
  205. textAlign: "right",
  206. title: "亮灯率",
  207. titleFontSize: 13,
  208. titleOffsetY: 0,
  209. titleOffsetX: 0,
  210. titleFontColor: "#666666",
  211. min: 0,
  212. max: 100,
  213. tofix: null,
  214. unit: "%",
  215. formatter: ""
  216. }
  217. ]
  218. },
  219. extra: {
  220. line: {
  221. type: "curve",
  222. width: 2,
  223. activeType: "hollow",
  224. linearType: "none",
  225. onShadow: false,
  226. animation: "vertical"
  227. },
  228. tooltip: {
  229. showBox: true,
  230. showArrow: true,
  231. showCategory: true,
  232. borderWidth: 0,
  233. borderRadius: 0,
  234. borderColor: "#000000",
  235. borderOpacity: 0.7,
  236. bgColor: "#000000",
  237. bgOpacity: 0.7,
  238. gridType: "solid",
  239. dashLength: 4,
  240. gridColor: "#CCCCCC",
  241. boxPadding: 3,
  242. fontSize: 13,
  243. lineHeight: 20,
  244. fontColor: "#FFFFFF",
  245. legendShow: true,
  246. legendShape: "auto",
  247. splitLine: true,
  248. horizentalLine: false,
  249. xAxisLabel: false,
  250. yAxisLabel: false,
  251. labelBgColor: "#ffffff",
  252. labelBgOpacity: 0.7,
  253. labelFontColor: "#666666"
  254. },
  255. markLine: {
  256. type: "solid",
  257. dashLength: 4,
  258. data: []
  259. }
  260. }
  261. });
  262. },
  263. tap(e) {
  264. uChartsInstance[e.target.id].touchLegend(e);
  265. uChartsInstance[e.target.id].showToolTip(e);
  266. }
  267. }
  268. };
  269. </script>
  270. <style scoped>
  271. .charts {
  272. width: 750rpx;
  273. height: 250rpx;
  274. }
  275. </style>