index.js 10 KB

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