index.js 8.1 KB

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