index.js 10 KB

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