index.js 8.5 KB

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