index.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. axisLabel: {
  199. color: "#000",
  200. interval: 0,
  201. formatter: function (value) {
  202. if (value.length > 4) {
  203. return value.substring(0, 4) + "...";
  204. } else {
  205. return value;
  206. }
  207. }
  208. },
  209. }];
  210. var values = data.values
  211. option.series = [{
  212. label: {
  213. show: true
  214. },
  215. name: data.name,
  216. type: 'bar',
  217. data: values
  218. }]
  219. option.grid = {
  220. left: 20,
  221. right: 20,
  222. bottom: 60,
  223. top: 0,
  224. containLabel: true
  225. }
  226. } else if (chartIndex == 1) {
  227. option.xAxis = [{
  228. type: 'value'
  229. }]
  230. option.legend = {
  231. data: data.legends,
  232. bottom: 40
  233. }
  234. var title = '项目租赁总费用排行'
  235. var date = this.data.date
  236. date = date.length > 7 ? date.substr(0, 7) : ''
  237. title = date + title
  238. option.title = {
  239. text: title,
  240. left: 'center',
  241. bottom: 20
  242. }
  243. option.yAxis = [{
  244. type: 'category',
  245. data: data.names,
  246. axisLabel: {
  247. color: "#000",
  248. interval: 0,
  249. formatter: function (value) {
  250. if (value.length > 4) {
  251. return value.substring(0, 4) + "...";
  252. } else {
  253. return value;
  254. }
  255. }
  256. },
  257. }];
  258. var values = data.values
  259. option.series = [{
  260. label: {
  261. show: true
  262. },
  263. name: '租赁费用',
  264. type: 'bar',
  265. data: values
  266. }]
  267. option.grid = {
  268. left: 20,
  269. right: 20,
  270. bottom: 60,
  271. top: 0,
  272. containLabel: true
  273. }
  274. // chart.resize({width: '80vw', height: '80vh'})
  275. } else {
  276. option.xAxis = [{
  277. type: 'value'
  278. }]
  279. option.legend = {
  280. data: data.legends,
  281. bottom: 40
  282. }
  283. var title = '设备最大单价费用排行'
  284. option.title = {
  285. text: title,
  286. left: 'center',
  287. bottom: 20
  288. }
  289. option.yAxis = [{
  290. type: 'category',
  291. data: data.names,
  292. axisLabel: {
  293. color: "#000",
  294. interval: 0,
  295. formatter: function (value) {
  296. if (value.length > 4) {
  297. return value.substring(0, 4) + "...";
  298. } else {
  299. return value;
  300. }
  301. }
  302. },
  303. }];
  304. var values = data.values
  305. option.series = [{
  306. label: {
  307. show: true
  308. },
  309. name: '租赁最大单价',
  310. type: 'bar',
  311. data: values
  312. }]
  313. option.grid = {
  314. left: 20,
  315. right: 20,
  316. bottom: 60,
  317. top: 0,
  318. containLabel: true
  319. }
  320. }
  321. chart.setOption(option)
  322. },
  323. radioChange: function (e) {
  324. var dateIndex = e.currentTarget.dataset.index
  325. if (dateIndex == this.data.dateIndex) return false
  326. var start_date = this.data.start_date
  327. var end_date = this.data.end_date
  328. var start_date = dateIndex == 1 ? start_date + '-01' : start_date.substr(0, 7)
  329. var end_date = dateIndex == 1 ? end_date + '-01' : end_date.substr(0, 7)
  330. this.setData({
  331. dateIndex,
  332. start_date,
  333. end_date
  334. })
  335. },
  336. switchShow: function (e) {
  337. var name = e.currentTarget.dataset.name
  338. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  339. this.setData({
  340. [name]: show
  341. })
  342. if (name == 'dateShow' && !show) {
  343. this.getData()
  344. }
  345. },
  346. /**
  347. * 生命周期函数--监听页面初次渲染完成
  348. */
  349. onReady: function () {
  350. },
  351. /**
  352. * 生命周期函数--监听页面显示
  353. */
  354. onShow: function () {
  355. this.getTabBar().init();
  356. var filter = wx.getStorageSync('sg-data-filters')
  357. this.setData({
  358. filter: filter
  359. })
  360. this.getChartData()
  361. },
  362. navigate: function (e) {
  363. var url = e.currentTarget.dataset.url
  364. wx.navigateTo({
  365. url: url,
  366. })
  367. },
  368. /**
  369. * 生命周期函数--监听页面隐藏
  370. */
  371. onHide: function () {
  372. },
  373. /**
  374. * 生命周期函数--监听页面卸载
  375. */
  376. onUnload: function () {
  377. },
  378. /**
  379. * 页面相关事件处理函数--监听用户下拉动作
  380. */
  381. onPullDownRefresh: function () {
  382. },
  383. /**
  384. * 页面上拉触底事件的处理函数
  385. */
  386. onReachBottom: function () {
  387. },
  388. /**
  389. * 用户点击右上角分享
  390. */
  391. onShareAppMessage: function () {
  392. }
  393. })