index.js 9.0 KB

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