index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. show: false
  27. },
  28. series: [{
  29. data: [],
  30. type: 'bar'
  31. }]
  32. };
  33. chart.setOption(option);
  34. return chart;
  35. }
  36. Page({
  37. /**
  38. * 页面的初始数据
  39. */
  40. data: {
  41. projects: [],
  42. project_ids: [],
  43. project_names: '',
  44. total_money: '0',
  45. month_money: '0',
  46. projectShow: false,
  47. index: 0,
  48. show: false,
  49. isSearch: true,
  50. charts: [{
  51. name: '柱状图',
  52. type: 'bar'
  53. }, {
  54. name: '折线图',
  55. type: 'line'
  56. }, {
  57. name: '饼状图',
  58. type: 'pie'
  59. }, {
  60. name: '明细图',
  61. type: 'detail'
  62. }, {
  63. name: '雷达图',
  64. type: 'radar'
  65. }],
  66. chartIndex: 0,
  67. dateTypes: [{
  68. name: '按年',
  69. type: 'year'
  70. }, {
  71. name: '按月',
  72. type: 'month'
  73. }],
  74. dateIndex: 0,
  75. dateShow: false,
  76. years_months: [],
  77. date: '',
  78. start: '',
  79. end: '',
  80. max_date: '',
  81. min_date: '',
  82. filter: {},
  83. data: [],
  84. ec: {
  85. onInit: initChart
  86. },
  87. detail_data: []
  88. },
  89. /**
  90. * 生命周期函数--监听页面加载
  91. */
  92. onLoad: function (options) {
  93. var that = this
  94. api.getByName(this, 'projects/getAll', 'projects', {}, function (res) {
  95. that.getData()
  96. });
  97. api.getByName(this, 'data/getYearsAndMonths', 'years_months');
  98. // var maxDate = (new Date()).getTime()
  99. // var minDate = (new Date()).getTime() - 2 * 365 * 24 * 3600 * 1000
  100. // this.setData({
  101. // minDate,
  102. // maxDate
  103. // })
  104. this.getDateInfo()
  105. app.resetDataFilter()
  106. },
  107. switchCheck: function(e) {
  108. var index = e.currentTarget.dataset.index
  109. var projects = this.data.projects
  110. projects[index].checked = projects[index].checked ? false : true
  111. this.setData({projects})
  112. },
  113. closeProject: function(e) {
  114. var project_ids = []
  115. var project_names = []
  116. var projects = this.data.projects
  117. for(var i = 0; i < projects.length; ++i) {
  118. if(projects[i].checked) {
  119. project_ids.push(projects[i].id)
  120. project_names.push(projects[i].name)
  121. }
  122. }
  123. project_names = project_names.join(',')
  124. this.setData({project_ids, project_names})
  125. this.switchShow(e)
  126. this.getData()
  127. this.getTotalInfo()
  128. },
  129. getSearchItems: function() {
  130. var ids = this.data.project_ids
  131. if (ids.length <= 0) return false
  132. var filter = this.data.filter
  133. var chart_type = this.data.charts[this.data.chartIndex]
  134. // var chat
  135. return {
  136. project_ids: ids,
  137. start_date: this.data.start_date,
  138. end_date: this.data.end_date,
  139. date: this.data.date,
  140. type: this.data.dateIndex == 0 ? 'year' : 'month',
  141. device_ids: filter.device_ids,
  142. device_name_ids: filter.device_name_ids,
  143. spec_ids: filter.spec_ids,
  144. rent_type_ids: filter.rent_type_ids,
  145. chart_type: chart_type.type
  146. }
  147. },
  148. getTotalInfo() {
  149. var data = this.getSearchItems()
  150. var that = this
  151. http({
  152. url: 'data/getTotalInfo',
  153. data: data,
  154. success: function(res) {
  155. if(res.code == 0) {
  156. that.setData(res.data)
  157. }
  158. }
  159. })
  160. },
  161. getDetailData() {
  162. var data = this.getSearchItems()
  163. var that = this
  164. http({
  165. url: 'data/getDetailData',
  166. data: data,
  167. success: function(res) {
  168. if(res.code == 0) {
  169. that.setData({
  170. detail_data: res.data
  171. })
  172. }
  173. }
  174. })
  175. },
  176. getData() {
  177. var data = this.getSearchItems()
  178. var that = this
  179. if(!data.project_ids || data.project_ids.length <= 0) return false
  180. var chart_type = this.data.charts[this.data.chartIndex]
  181. http({
  182. url: 'data/getStat',
  183. data: data,
  184. success: function(res) {
  185. if(res.code == 0) {
  186. if(chart_type == 'detail') {
  187. that.setData({
  188. detail_data: res.data
  189. })
  190. } else {
  191. that.setData({data: res.data})
  192. that.updateChart()
  193. }
  194. }
  195. }
  196. })
  197. },
  198. getDateInfo() {
  199. var that = this
  200. http({
  201. url: 'data/getDateInfo',
  202. data: {},
  203. success: function (res) {
  204. if (res.code == 0) {
  205. that.setData(res.data)
  206. }
  207. }
  208. })
  209. },
  210. onChange: function (e) {
  211. var value = e.detail.value
  212. var name = e.currentTarget.dataset.name
  213. if(this.data[name] == value) return false;
  214. this.setData({
  215. [name]: value
  216. })
  217. if(name == 'chartIndex') {
  218. var chart = this.data.charts[this.data.chartIndex]
  219. if(chart.type == 'detail') {
  220. this.getDetailData()
  221. } else {
  222. this.getData()
  223. }
  224. } else if(name == 'date') {
  225. this.getTotalInfo()
  226. }
  227. },
  228. updateChart: function() {
  229. var data = this.data.data
  230. option.xAxis.data = data.names;
  231. option.legend = {
  232. data: data.legends
  233. }
  234. var type = this.data.charts[this.data.chartIndex].type
  235. if(type == 'pie') {
  236. option.series = [{
  237. data: data.data,
  238. type: type,
  239. label: {
  240. position: 'inner',
  241. formatter: '{b}\n{d}%'
  242. }
  243. }]
  244. option.xAxis.show = false
  245. } else {
  246. option.xAxis.show = true
  247. var values = data.values
  248. for(var i = 0; i < values.length; ++i) {
  249. option.series[i] = {
  250. label: {
  251. show: true,
  252. position: 'top'
  253. },
  254. name: data.legends[i],
  255. type: type,
  256. data: values[i]
  257. }
  258. }
  259. }
  260. chart.setOption(option)
  261. },
  262. radioChange: function (e) {
  263. var dateIndex = e.currentTarget.dataset.index
  264. if(dateIndex == this.data.dateIndex) return false
  265. var start_date = this.data.start_date
  266. var end_date = this.data.end_date
  267. var start_date = dateIndex == 1 ? start_date + '-01' : start_date.substr(0, 7)
  268. var end_date = dateIndex == 1 ? end_date + '-01' : end_date.substr(0, 7)
  269. this.setData({
  270. dateIndex,
  271. start_date,
  272. end_date
  273. })
  274. },
  275. switchShow: function (e) {
  276. var name = e.currentTarget.dataset.name
  277. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  278. this.setData({
  279. [name]: show
  280. })
  281. if(name == 'dateShow' && !show) {
  282. this.getData()
  283. }
  284. },
  285. /**
  286. * 生命周期函数--监听页面初次渲染完成
  287. */
  288. onReady: function () {
  289. },
  290. /**
  291. * 生命周期函数--监听页面显示
  292. */
  293. onShow: function () {
  294. this.getTabBar().init();
  295. var filter = wx.getStorageSync('sg-data-filters')
  296. this.setData({
  297. filter: filter
  298. })
  299. this.getData()
  300. },
  301. navigate: function(e) {
  302. var url = e.currentTarget.dataset.url
  303. wx.navigateTo({
  304. url: url,
  305. })
  306. },
  307. /**
  308. * 生命周期函数--监听页面隐藏
  309. */
  310. onHide: function () {
  311. },
  312. /**
  313. * 生命周期函数--监听页面卸载
  314. */
  315. onUnload: function () {
  316. },
  317. /**
  318. * 页面相关事件处理函数--监听用户下拉动作
  319. */
  320. onPullDownRefresh: function () {
  321. },
  322. /**
  323. * 页面上拉触底事件的处理函数
  324. */
  325. onReachBottom: function () {
  326. },
  327. /**
  328. * 用户点击右上角分享
  329. */
  330. onShareAppMessage: function () {
  331. }
  332. })