index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // pages/data/index.js
  2. import http from '../../utils/http'
  3. import util from '../../utils/util'
  4. import api from '../../utils/api'
  5. const app = getApp()
  6. import * as echarts from '../../ec-canvas/echarts';
  7. let chart = null;
  8. let option = {}
  9. function initChart(canvas, width, height, dpr) {
  10. chart = echarts.init(canvas, null, {
  11. width: width,
  12. height: height,
  13. devicePixelRatio: dpr // new
  14. });
  15. canvas.setChart(chart);
  16. option = {
  17. legend: {
  18. data: []
  19. },
  20. xAxis: {
  21. type: 'category',
  22. data: []
  23. },
  24. yAxis: {
  25. type: 'value'
  26. },
  27. series: [{
  28. data: [],
  29. type: 'bar'
  30. }]
  31. };
  32. chart.setOption(option);
  33. return chart;
  34. }
  35. Page({
  36. /**
  37. * 页面的初始数据
  38. */
  39. data: {
  40. projects: [],
  41. project_ids: [],
  42. project_names: '',
  43. total_project: '0',
  44. total_month: '0',
  45. projectShow: false,
  46. index: 0,
  47. show: false,
  48. isSearch: true,
  49. charts: [{
  50. name: '柱状图',
  51. type: 'bar'
  52. }, {
  53. name: '折线图',
  54. type: 'line'
  55. }, {
  56. name: '饼状图',
  57. type: 'pie'
  58. }, {
  59. name: '明细图',
  60. type: 'detail'
  61. }, {
  62. name: '雷达图',
  63. type: 'radar'
  64. }],
  65. chartIndex: 0,
  66. dateTypes: [{
  67. name: '按年',
  68. type: 'year'
  69. }, {
  70. name: '按月',
  71. type: 'month'
  72. }],
  73. dateIndex: 0,
  74. dateShow: false,
  75. years_months: [],
  76. date: '',
  77. yearDate: '',
  78. monthDate: '',
  79. max_date: (new Date()).getTime(),
  80. min_date: (new Date()).getTime(),
  81. filter: {},
  82. data: [],
  83. ec: {
  84. onInit: initChart
  85. }
  86. },
  87. /**
  88. * 生命周期函数--监听页面加载
  89. */
  90. onLoad: function (options) {
  91. var that = this
  92. api.getByName(this, 'projects/getAll', 'projects', {}, function (res) {
  93. that.getData()
  94. });
  95. api.getByName(this, 'data/getYearsAndMonths', 'years_months');
  96. // var maxDate = (new Date()).getTime()
  97. // var minDate = (new Date()).getTime() - 2 * 365 * 24 * 3600 * 1000
  98. // this.setData({
  99. // minDate,
  100. // maxDate
  101. // })
  102. this.getDateInfo()
  103. app.resetDataFilter()
  104. },
  105. switchCheck: function(e) {
  106. var index = e.currentTarget.dataset.index
  107. var projects = this.data.projects
  108. projects[index].checked = projects[index].checked ? false : true
  109. this.setData({projects})
  110. },
  111. closeProject: function(e) {
  112. var project_ids = []
  113. var project_names = []
  114. var projects = this.data.projects
  115. for(var i = 0; i < projects.length; ++i) {
  116. if(projects[i].checked) {
  117. project_ids.push(projects[i].id)
  118. project_names.push(projects[i].name)
  119. }
  120. }
  121. project_names = project_names.join(',')
  122. this.setData({project_ids, project_names})
  123. this.switchShow(e)
  124. this.getData()
  125. },
  126. getData() {
  127. var ids = this.data.project_ids
  128. if (ids.length <= 0) return false
  129. var filter = this.data.filter
  130. var data = {
  131. project_ids: ids,
  132. date: this.data.date,
  133. type: this.data.dateIndex == 0 ? 'year' : 'month',
  134. device_ids: filter.device_ids,
  135. device_name_ids: filter.device_name_ids,
  136. spec_ids: filter.spec_ids,
  137. rent_type_ids: filter.rent_type_ids
  138. }
  139. var that = this
  140. api.getByName(this, 'data/getStat', 'data', data, function(res) {
  141. that.updateChart()
  142. });
  143. },
  144. getDateInfo() {
  145. var that = this
  146. http({
  147. url: 'data/getDateInfo',
  148. data: {},
  149. success: function (res) {
  150. if (res.code == 0) {
  151. that.setData(res.data)
  152. that.setData({
  153. yearDate: res.data.date
  154. })
  155. }
  156. }
  157. })
  158. },
  159. onDateChange: function (e) {
  160. var yearDate = e.detail.value[0].value + '-' + e.detail.value[1].value
  161. this.setData({
  162. yearDate
  163. })
  164. },
  165. dateConfirm: function (e) {
  166. this.switchShow(e)
  167. this.updateDate()
  168. },
  169. monthDateConfirm: function (e) {
  170. var date = e.detail
  171. var monthDate = util.formatDate(date)
  172. this.setData({
  173. monthDate
  174. })
  175. this.switchShow(e)
  176. this.updateDate()
  177. },
  178. updateDate: function () {
  179. var date = this.data.dateIndex == 0 ? this.data.yearDate : this.data.monthDate
  180. this.setData({
  181. date
  182. })
  183. this.getData()
  184. },
  185. onChange: function (e) {
  186. var index = e.detail.value
  187. var name = e.currentTarget.dataset.name
  188. this.setData({
  189. [name]: index
  190. })
  191. if(name == 'chartIndex') {
  192. this.updateChart()
  193. }
  194. },
  195. updateChart: function() {
  196. var data = this.data.data
  197. option.xAxis.data = data.names;
  198. option.legend = {
  199. data: data.legends
  200. }
  201. var type = this.data.charts[this.data.chartIndex].type
  202. var values = data.values
  203. for(var i = 0; i < values.length; ++i) {
  204. option.series[i] = {
  205. label: {
  206. show: true,
  207. position: 'top'
  208. },
  209. name: data.legends[i],
  210. type: type,
  211. data: values[i]
  212. }
  213. }
  214. chart.setOption(option)
  215. },
  216. radioChange: function (e) {
  217. var dateIndex = e.currentTarget.dataset.index
  218. this.setData({
  219. dateIndex
  220. })
  221. },
  222. switchShow: function (e) {
  223. var name = e.currentTarget.dataset.name
  224. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  225. this.setData({
  226. [name]: show
  227. })
  228. },
  229. /**
  230. * 生命周期函数--监听页面初次渲染完成
  231. */
  232. onReady: function () {
  233. },
  234. /**
  235. * 生命周期函数--监听页面显示
  236. */
  237. onShow: function () {
  238. this.getTabBar().init();
  239. var filter = wx.getStorageSync('sg-data-filters')
  240. this.setData({
  241. filter: filter
  242. })
  243. this.getData()
  244. },
  245. navigate: function(e) {
  246. var url = e.currentTarget.dataset.url
  247. wx.navigateTo({
  248. url: url,
  249. })
  250. },
  251. /**
  252. * 生命周期函数--监听页面隐藏
  253. */
  254. onHide: function () {
  255. },
  256. /**
  257. * 生命周期函数--监听页面卸载
  258. */
  259. onUnload: function () {
  260. },
  261. /**
  262. * 页面相关事件处理函数--监听用户下拉动作
  263. */
  264. onPullDownRefresh: function () {
  265. },
  266. /**
  267. * 页面上拉触底事件的处理函数
  268. */
  269. onReachBottom: function () {
  270. },
  271. /**
  272. * 用户点击右上角分享
  273. */
  274. onShareAppMessage: function () {
  275. }
  276. })