index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_id: '',
  43. project_ids: [],
  44. sort_types: [
  45. { text: '筛选类型', value: '' },
  46. { text: '按年筛选', value: 'year' },
  47. { text: '按月筛选', value: 'month' }
  48. ],
  49. sort_type: '',
  50. project_names: '',
  51. year_money: '0',
  52. month_money: '0',
  53. projectShow: false,
  54. index: 0,
  55. show: false,
  56. isSearch: true,
  57. charts: [{
  58. name: '柱状图',
  59. type: 'bar'
  60. }, {
  61. name: '折线图',
  62. type: 'line'
  63. }, {
  64. name: '饼状图',
  65. type: 'pie'
  66. }, {
  67. name: '明细图',
  68. type: 'detail'
  69. }, {
  70. name: '雷达图',
  71. type: 'radar'
  72. }],
  73. chartIndex: 1,
  74. dateTypes: [{
  75. name: '按年',
  76. type: 'year'
  77. }, {
  78. name: '按月',
  79. type: 'month'
  80. }],
  81. dateIndex: 0,
  82. dateShow: false,
  83. years_months: [],
  84. date: '',
  85. start: '',
  86. end: '',
  87. max_date: '',
  88. min_date: '',
  89. filter: {},
  90. data: [],
  91. ec: {
  92. onInit: initChart
  93. },
  94. detail_data: [],
  95. orderBy: 'asc'
  96. },
  97. /**
  98. * 生命周期函数--监听页面加载
  99. */
  100. onLoad: function (options) {
  101. app.resetDataFilter()
  102. api.getByName(this, 'projects/getAll', 'projects', {type: 'drop_menu'});
  103. },
  104. changeChartIndex: function(e) {
  105. var type = e.currentTarget.dataset.type
  106. var chartIndex = this.data.chartIndex
  107. chartIndex = type == '+' ? (chartIndex + 1) : (chartIndex - 1)
  108. if(chartIndex < 0 || chartIndex > 2) return false;
  109. this.setData({chartIndex})
  110. this.getChartData()
  111. },
  112. updateValue: function (e) {
  113. var name = e.currentTarget.dataset.name
  114. var value = e.currentTarget.dataset.value
  115. this.setData({
  116. [name]: value
  117. })
  118. if (name == 'orderBy') {
  119. this.getChartData()
  120. }
  121. },
  122. updateDate(e) {
  123. this.setData({
  124. date: e.detail.date
  125. })
  126. this.getYearAndMonthMoney()
  127. this.getChartData()
  128. },
  129. getYearAndMonthMoney() {
  130. var that = this
  131. http({
  132. url: 'data/getYearAndMonthMoney',
  133. data: {
  134. date: this.data.date
  135. },
  136. success: function (res) {
  137. if (res.code == 0) {
  138. that.setData(res.data)
  139. }
  140. }
  141. })
  142. },
  143. getChartData: function () {
  144. var chartIndex = this.data.chartIndex
  145. var that = this
  146. var data = {
  147. date: this.data.date,
  148. orderBy: this.data.orderBy,
  149. chartIndex: chartIndex,
  150. project_id: this.data.project_id,
  151. sort_type: this.data.sort_type
  152. }
  153. data = Object.assign({}, data, this.data.filter)
  154. http({
  155. url: 'data/projectStat',
  156. data: data,
  157. success: function (res) {
  158. if (res.code == 0) {
  159. that.setData({
  160. data: res.data
  161. })
  162. that.updateChart()
  163. }
  164. }
  165. })
  166. },
  167. onChange(e) {
  168. var name = e.currentTarget.dataset.name
  169. var val = e.detail
  170. this.setData({
  171. [name]: val
  172. })
  173. this.getChartData()
  174. },
  175. updateChart: function () {
  176. var data = this.data.data
  177. var chartIndex = this.data.chartIndex
  178. if(chartIndex == 0) {
  179. option.xAxis = [{
  180. type: 'value'
  181. }]
  182. option.legend = {
  183. data: data.legends,
  184. bottom: 40
  185. }
  186. var title = '设备租赁费用明细'
  187. var date = this.data.date
  188. date = date.length > 7 ? date.substr(0, 4) + '年' : ''
  189. title = (data.project ? data.project.name + '-' : '-') + date + title
  190. option.title = {
  191. text: title,
  192. left: 'center',
  193. bottom: 20
  194. }
  195. option.yAxis = [{
  196. type: 'category',
  197. data: data.names
  198. }];
  199. var values = data.values
  200. option.series = [{
  201. label: {
  202. show: true
  203. },
  204. name: data.name,
  205. type: 'bar',
  206. data: values
  207. }]
  208. option.grid = {
  209. left: 20,
  210. right: 20,
  211. bottom: 60,
  212. top: 0,
  213. containLabel: true
  214. }
  215. } else if (chartIndex == 1) {
  216. option.xAxis = [{
  217. type: 'value'
  218. }]
  219. option.legend = {
  220. data: data.legends,
  221. bottom: 40
  222. }
  223. var title = '项目租赁总费用排行'
  224. var date = this.data.date
  225. date = date.length > 7 ? date.substr(0, 7) : ''
  226. title = date + title
  227. option.title = {
  228. text: title,
  229. left: 'center',
  230. bottom: 20
  231. }
  232. option.yAxis = [{
  233. type: 'category',
  234. data: data.names
  235. }];
  236. var values = data.values
  237. option.series = [{
  238. label: {
  239. show: true
  240. },
  241. name: '租赁费用',
  242. type: 'bar',
  243. data: values
  244. }]
  245. option.grid = {
  246. left: 20,
  247. right: 20,
  248. bottom: 60,
  249. top: 0,
  250. containLabel: true
  251. }
  252. // chart.resize({width: '80vw', height: '80vh'})
  253. } else {
  254. option.xAxis = [{
  255. type: 'value'
  256. }]
  257. option.legend = {
  258. data: data.legends,
  259. bottom: 40
  260. }
  261. var title = '设备最大单价费用排行'
  262. option.title = {
  263. text: title,
  264. left: 'center',
  265. bottom: 20
  266. }
  267. option.yAxis = [{
  268. type: 'category',
  269. data: data.names
  270. }];
  271. var values = data.values
  272. option.series = [{
  273. label: {
  274. show: true
  275. },
  276. name: '租赁最大单价',
  277. type: 'bar',
  278. data: values
  279. }]
  280. option.grid = {
  281. left: 20,
  282. right: 20,
  283. bottom: 60,
  284. top: 0,
  285. containLabel: true
  286. }
  287. }
  288. chart.setOption(option)
  289. },
  290. radioChange: function (e) {
  291. var dateIndex = e.currentTarget.dataset.index
  292. if (dateIndex == this.data.dateIndex) return false
  293. var start_date = this.data.start_date
  294. var end_date = this.data.end_date
  295. var start_date = dateIndex == 1 ? start_date + '-01' : start_date.substr(0, 7)
  296. var end_date = dateIndex == 1 ? end_date + '-01' : end_date.substr(0, 7)
  297. this.setData({
  298. dateIndex,
  299. start_date,
  300. end_date
  301. })
  302. },
  303. switchShow: function (e) {
  304. var name = e.currentTarget.dataset.name
  305. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  306. this.setData({
  307. [name]: show
  308. })
  309. if (name == 'dateShow' && !show) {
  310. this.getData()
  311. }
  312. },
  313. /**
  314. * 生命周期函数--监听页面初次渲染完成
  315. */
  316. onReady: function () {
  317. },
  318. /**
  319. * 生命周期函数--监听页面显示
  320. */
  321. onShow: function () {
  322. this.getTabBar().init();
  323. var filter = wx.getStorageSync('sg-data-filters')
  324. this.setData({
  325. filter: filter
  326. })
  327. this.getChartData()
  328. },
  329. navigate: function (e) {
  330. var url = e.currentTarget.dataset.url
  331. wx.navigateTo({
  332. url: url,
  333. })
  334. },
  335. /**
  336. * 生命周期函数--监听页面隐藏
  337. */
  338. onHide: function () {
  339. },
  340. /**
  341. * 生命周期函数--监听页面卸载
  342. */
  343. onUnload: function () {
  344. },
  345. /**
  346. * 页面相关事件处理函数--监听用户下拉动作
  347. */
  348. onPullDownRefresh: function () {
  349. },
  350. /**
  351. * 页面上拉触底事件的处理函数
  352. */
  353. onReachBottom: function () {
  354. },
  355. /**
  356. * 用户点击右上角分享
  357. */
  358. onShareAppMessage: function () {
  359. }
  360. })