year.vue 6.6 KB

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