index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. year_list: [],
  134. yearsdata: "",
  135. is_years_status: false
  136. },
  137. /**
  138. * 生命周期函数--监听页面加载
  139. */
  140. onLoad: function (options) {
  141. app.resetDataFilter()
  142. api.getByName(this, 'projects/getAll', 'projects', {
  143. type: 'drop_menu'
  144. });
  145. http({
  146. url: 'data/getProjectYears',
  147. success: (res) => {
  148. if (res.code == 0) {
  149. this.setData({
  150. year_list: res.data
  151. })
  152. }
  153. }
  154. })
  155. },
  156. // startX(e) {
  157. // console.log(e)
  158. // this.setData({
  159. // recordX: e.touches[0].clientX
  160. // })
  161. // },
  162. // moveX(e) {
  163. // console.log(e)
  164. // let currentX = e.changedTouches[0].clientX
  165. // var chartIndex = this.data.chartIndex
  166. // if (currentX - this.data.recordX <= -60) {
  167. // chartIndex = (chartIndex + 1)
  168. // console.log(chartIndex)
  169. // }
  170. // if (currentX - this.data.recordX >= 60) {
  171. // chartIndex = (chartIndex - 1)
  172. // console.log(chartIndex)
  173. // }
  174. // if (chartIndex < 0 || chartIndex > 2) return false;
  175. // this.setData({ chartIndex })
  176. // this.getChartData()
  177. // },
  178. changeChartIndex: function (e) {
  179. var type = e.currentTarget.dataset.type
  180. var chartIndex = this.data.chartIndex
  181. chartIndex = type == '+' ? (chartIndex + 1) : (chartIndex - 1)
  182. if (chartIndex < 0 || chartIndex > 2) return false;
  183. this.setData({
  184. chartIndex
  185. })
  186. this.getChartData()
  187. },
  188. popclose: function () {
  189. this.setData({
  190. projectShow: false
  191. })
  192. },
  193. updateValue: function (e) {
  194. var name = e.currentTarget.dataset.name
  195. var value = e.currentTarget.dataset.value
  196. this.setData({
  197. [name]: value
  198. })
  199. if (name == 'orderBy') {
  200. this.getChartData()
  201. }
  202. },
  203. updateDate(e) {
  204. this.setData({
  205. date: e.detail.date
  206. })
  207. this.getYearAndMonthMoney()
  208. this.getChartData()
  209. },
  210. getYearAndMonthMoney() {
  211. var that = this
  212. http({
  213. url: 'data/getYearAndMonthMoney',
  214. data: {
  215. date: this.data.date
  216. },
  217. success: function (res) {
  218. if (res.code == 0) {
  219. that.setData(res.data)
  220. }
  221. }
  222. })
  223. },
  224. getChartData: function () {
  225. var chartIndex = this.data.chartIndex
  226. var that = this
  227. let yearnum = ""
  228. if (this.data.year_list.length != 0 && this.data.yearsdata != "") {
  229. yearnum = (this.data.year_list.find((item) => {
  230. return item.value == this.data.yearsdata
  231. }).text).split("年")[0]
  232. }
  233. var data = {
  234. date: this.data.date,
  235. orderBy: this.data.orderBy,
  236. chartIndex: chartIndex,
  237. project_id: this.data.project_id,
  238. sort_type: this.data.sort_type,
  239. project_year: yearnum
  240. }
  241. console.log(data)
  242. data = Object.assign({}, data, this.data.filter)
  243. http({
  244. url: 'data/projectStat',
  245. data: data,
  246. success: function (res) {
  247. if (res.code == 0) {
  248. that.setData({
  249. data: res.data
  250. })
  251. that.updateChart()
  252. }
  253. }
  254. })
  255. },
  256. onChange(e) {
  257. var name = e.currentTarget.dataset.name
  258. var val = e.detail
  259. this.setData({
  260. [name]: val
  261. })
  262. if (this.data.sort_type == 'month') {
  263. this.setData({
  264. is_years_status: true
  265. })
  266. } else {
  267. this.setData({
  268. is_years_status: false
  269. })
  270. }
  271. this.getChartData()
  272. },
  273. updateChart: function () {
  274. var data = this.data.data
  275. var chartIndex = this.data.chartIndex
  276. if (chartIndex == 0) {
  277. option.xAxis = [{
  278. type: 'value',
  279. show: false
  280. }]
  281. option.legend = {
  282. data: data.legends,
  283. bottom: 40
  284. }
  285. var title = '设备租赁费用明细'
  286. var date = ""
  287. if (this.data.year_list.find((item) => {
  288. return item.value == this.data.yearsdata
  289. }).text != '请选择年份') {
  290. date = this.data.year_list.find((item) => {
  291. return item.value == this.data.yearsdata
  292. }).text
  293. }
  294. title = (data.project ? data.project.name + '\n' + '\n' : '-') + date + title
  295. option.title = {
  296. text: title,
  297. left: 'center',
  298. bottom: 10,
  299. textStyle: {
  300. fontSize: 14,
  301. color: "#5992fd"
  302. },
  303. }
  304. option.yAxis = [{
  305. type: 'category',
  306. data: data.names,
  307. axisLabel: {
  308. color: "#000",
  309. interval: 0,
  310. formatter: function (value) {
  311. if (value.length > 4) {
  312. return value.substring(0, 4) + "...";
  313. } else {
  314. return value;
  315. }
  316. }
  317. },
  318. }];
  319. var values = data.values
  320. option.series = [{
  321. label: {
  322. show: true
  323. },
  324. // name: data.name,
  325. type: 'bar',
  326. data: values
  327. }]
  328. option.grid = {
  329. left: 20,
  330. right: 45,
  331. bottom: 60,
  332. top: 0,
  333. containLabel: true
  334. }
  335. } else if (chartIndex == 1) {
  336. option.xAxis = [{
  337. type: 'value',
  338. show: false
  339. }]
  340. option.legend = {
  341. data: data.legends,
  342. bottom: 40
  343. }
  344. var title = '项目租赁总费用排行'
  345. var date = this.data.date
  346. date = date.length > 7 ? date.substr(0, 7) : ''
  347. title = date + title
  348. option.title = {
  349. text: title,
  350. left: 'center',
  351. bottom: 10,
  352. textStyle: {
  353. fontSize: 14,
  354. color: "#5992fd"
  355. },
  356. }
  357. option.yAxis = [{
  358. type: 'category',
  359. data: data.names,
  360. axisLabel: {
  361. color: "#000",
  362. interval: 0,
  363. formatter: function (value) {
  364. if (value.length > 4) {
  365. return value.substring(0, 4) + "...";
  366. } else {
  367. return value;
  368. }
  369. }
  370. },
  371. }];
  372. var values = data.values
  373. option.series = [{
  374. label: {
  375. show: true
  376. },
  377. // name: '租赁费用',
  378. type: 'bar',
  379. data: values
  380. }]
  381. option.grid = {
  382. left: 20,
  383. right: 45,
  384. bottom: 60,
  385. top: 0,
  386. containLabel: true
  387. }
  388. // chart.resize({width: '80vw', height: '80vh'})
  389. } else {
  390. option.xAxis = [{
  391. type: 'value',
  392. show: false
  393. }]
  394. option.legend = {
  395. data: data.legends,
  396. bottom: 40
  397. }
  398. var title = '设备租赁单价排行'
  399. title = (data.info.length != 0 ? (data.info[0] != null ? data.info[0] + '-' : '') + (data.info[1] != null ? data.info[1] + '-' : '') + (data.info[2] != null ? data.info[2] + '-' : '') + (data.info[3] != null ? data.info[3] : '') : '') + '\n' + '\n' + title
  400. console.log(data)
  401. option.title = {
  402. text: title,
  403. left: 'center',
  404. bottom: 10,
  405. textStyle: {
  406. fontSize: 14,
  407. color: "#5992fd"
  408. },
  409. }
  410. option.yAxis = [{
  411. type: 'category',
  412. data: data.names,
  413. axisLabel: {
  414. color: "#000",
  415. interval: 0,
  416. formatter: function (value) {
  417. if (value.length > 4) {
  418. return value.substring(0, 4) + "...";
  419. } else {
  420. return value;
  421. }
  422. }
  423. },
  424. }];
  425. var values = data.values
  426. option.series = [{
  427. label: {
  428. show: true
  429. },
  430. // name: '租赁最大单价',
  431. type: 'bar',
  432. data: values
  433. }]
  434. option.grid = {
  435. left: 20,
  436. right: 45,
  437. bottom: 60,
  438. top: 0,
  439. containLabel: true
  440. }
  441. }
  442. chart.setOption(option)
  443. },
  444. radioChange: function (e) {
  445. var dateIndex = e.currentTarget.dataset.index
  446. if (dateIndex == this.data.dateIndex) return false
  447. var start_date = this.data.start_date
  448. var end_date = this.data.end_date
  449. var start_date = dateIndex == 1 ? start_date + '-01' : start_date.substr(0, 7)
  450. var end_date = dateIndex == 1 ? end_date + '-01' : end_date.substr(0, 7)
  451. this.setData({
  452. dateIndex,
  453. start_date,
  454. end_date
  455. })
  456. },
  457. switchShow: function (e) {
  458. var name = e.currentTarget.dataset.name
  459. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  460. this.setData({
  461. [name]: show
  462. })
  463. if (name == 'dateShow' && !show) {
  464. this.getData()
  465. }
  466. },
  467. /**
  468. * 生命周期函数--监听页面初次渲染完成
  469. */
  470. onReady: function () {
  471. },
  472. /**
  473. * 生命周期函数--监听页面显示
  474. */
  475. onShow: function () {
  476. this.getTabBar().init();
  477. var filter = wx.getStorageSync('sg-data-filters')
  478. this.setData({
  479. filter: filter
  480. })
  481. this.getChartData()
  482. },
  483. navigate: function (e) {
  484. var url = e.currentTarget.dataset.url
  485. wx.navigateTo({
  486. url: url + '?status=1',
  487. })
  488. },
  489. /**
  490. * 生命周期函数--监听页面隐藏
  491. */
  492. onHide: function () {
  493. },
  494. /**
  495. * 生命周期函数--监听页面卸载
  496. */
  497. onUnload: function () {
  498. },
  499. /**
  500. * 页面相关事件处理函数--监听用户下拉动作
  501. */
  502. onPullDownRefresh: function () {
  503. },
  504. /**
  505. * 页面上拉触底事件的处理函数
  506. */
  507. onReachBottom: function () {
  508. },
  509. /**
  510. * 用户点击右上角分享
  511. */
  512. onShareAppMessage: function () {
  513. }
  514. })