index.js 11 KB

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