index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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({
  112. projects
  113. })
  114. },
  115. closeProject: function (e) {
  116. var project_ids = []
  117. var project_names = []
  118. var projects = this.data.projects
  119. for (var i = 0; i < projects.length; ++i) {
  120. if (projects[i].checked) {
  121. project_ids.push(projects[i].id)
  122. project_names.push(projects[i].name)
  123. }
  124. }
  125. project_names = project_names.join(',')
  126. this.setData({
  127. project_ids,
  128. project_names
  129. })
  130. this.switchShow(e)
  131. this.getData()
  132. this.getTotalInfo()
  133. },
  134. getSearchItems: function () {
  135. var ids = this.data.project_ids
  136. if (ids.length <= 0) return false
  137. var filter = this.data.filter
  138. var chart_type = this.data.charts[this.data.chartIndex]
  139. // var chat
  140. return {
  141. project_ids: ids,
  142. start_date: this.data.start_date,
  143. end_date: this.data.end_date,
  144. date: this.data.date,
  145. type: this.data.dateIndex == 0 ? 'year' : 'month',
  146. device_ids: filter.device_ids,
  147. device_name_ids: filter.device_name_ids,
  148. spec_ids: filter.spec_ids,
  149. rent_type_ids: filter.rent_type_ids,
  150. chart_type: chart_type.type
  151. }
  152. },
  153. getTotalInfo() {
  154. var data = this.getSearchItems()
  155. var that = this
  156. http({
  157. url: 'data/getTotalInfo',
  158. data: data,
  159. success: function (res) {
  160. if (res.code == 0) {
  161. that.setData(res.data)
  162. }
  163. }
  164. })
  165. },
  166. getDetailData() {
  167. var data = this.getSearchItems()
  168. var that = this
  169. http({
  170. url: 'data/getDetailData',
  171. data: data,
  172. success: function (res) {
  173. if (res.code == 0) {
  174. that.setData({
  175. detail_data: res.data
  176. })
  177. }
  178. }
  179. })
  180. },
  181. getData() {
  182. var data = this.getSearchItems()
  183. var that = this
  184. if (!data.project_ids || data.project_ids.length <= 0) return false
  185. var chart_type = this.data.charts[this.data.chartIndex]
  186. http({
  187. url: 'data/getStat',
  188. data: data,
  189. success: function (res) {
  190. if (res.code == 0) {
  191. if (chart_type == 'detail') {
  192. that.setData({
  193. detail_data: res.data
  194. })
  195. } else {
  196. that.setData({
  197. data: res.data
  198. })
  199. that.updateChart()
  200. }
  201. }
  202. }
  203. })
  204. },
  205. getDateInfo() {
  206. var that = this
  207. http({
  208. url: 'data/getDateInfo',
  209. data: {},
  210. success: function (res) {
  211. if (res.code == 0) {
  212. that.setData(res.data)
  213. }
  214. }
  215. })
  216. },
  217. onChange: function (e) {
  218. var value = e.detail.value
  219. var name = e.currentTarget.dataset.name
  220. if (this.data[name] == value) return false;
  221. this.setData({
  222. [name]: value
  223. })
  224. if (name == 'chartIndex') {
  225. var chart = this.data.charts[this.data.chartIndex]
  226. if (chart.type == 'detail') {
  227. this.getDetailData()
  228. } else {
  229. this.getData()
  230. }
  231. } else if (name == 'date') {
  232. this.getTotalInfo()
  233. }
  234. },
  235. updateChart: function () {
  236. var data = this.data.data
  237. option.xAxis.data = data.names;
  238. option.legend = {
  239. data: data.legends
  240. }
  241. var type = this.data.charts[this.data.chartIndex].type
  242. if (type == 'pie') {
  243. option.series = [{
  244. data: data.data,
  245. type: type,
  246. label: {
  247. position: 'inner',
  248. formatter: '{b}\n{d}%'
  249. }
  250. }]
  251. option.xAxis.show = false
  252. } else if (type == 'radar') {
  253. option.xAxis.show = false
  254. option.radar = {
  255. // shape: 'circle',
  256. indicator: data.indicator
  257. }
  258. option.series = [{
  259. type: type,
  260. data: data.data
  261. }]
  262. } else {
  263. option.xAxis.show = true
  264. var values = data.values
  265. for (var i = 0; i < values.length; ++i) {
  266. option.series[i] = {
  267. label: {
  268. show: true,
  269. position: 'top'
  270. },
  271. name: data.legends[i],
  272. type: type,
  273. data: values[i]
  274. }
  275. }
  276. }
  277. chart.setOption(option)
  278. },
  279. radioChange: function (e) {
  280. var dateIndex = e.currentTarget.dataset.index
  281. if (dateIndex == this.data.dateIndex) return false
  282. var start_date = this.data.start_date
  283. var end_date = this.data.end_date
  284. var start_date = dateIndex == 1 ? start_date + '-01' : start_date.substr(0, 7)
  285. var end_date = dateIndex == 1 ? end_date + '-01' : end_date.substr(0, 7)
  286. this.setData({
  287. dateIndex,
  288. start_date,
  289. end_date
  290. })
  291. },
  292. switchShow: function (e) {
  293. var name = e.currentTarget.dataset.name
  294. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  295. this.setData({
  296. [name]: show
  297. })
  298. if (name == 'dateShow' && !show) {
  299. this.getData()
  300. }
  301. },
  302. /**
  303. * 生命周期函数--监听页面初次渲染完成
  304. */
  305. onReady: function () {
  306. },
  307. /**
  308. * 生命周期函数--监听页面显示
  309. */
  310. onShow: function () {
  311. this.getTabBar().init();
  312. var filter = wx.getStorageSync('sg-data-filters')
  313. this.setData({
  314. filter: filter
  315. })
  316. this.getData()
  317. },
  318. navigate: function (e) {
  319. var url = e.currentTarget.dataset.url
  320. wx.navigateTo({
  321. url: url,
  322. })
  323. },
  324. /**
  325. * 生命周期函数--监听页面隐藏
  326. */
  327. onHide: function () {
  328. },
  329. /**
  330. * 生命周期函数--监听页面卸载
  331. */
  332. onUnload: function () {
  333. },
  334. /**
  335. * 页面相关事件处理函数--监听用户下拉动作
  336. */
  337. onPullDownRefresh: function () {
  338. },
  339. /**
  340. * 页面上拉触底事件的处理函数
  341. */
  342. onReachBottom: function () {
  343. },
  344. /**
  345. * 用户点击右上角分享
  346. */
  347. onShareAppMessage: function () {
  348. }
  349. })