index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. option = {
  17. color: ["#5992fd"],
  18. legend: {
  19. data: []
  20. },
  21. xAxis: {
  22. type: 'category',
  23. data: []
  24. },
  25. yAxis: {
  26. type: 'value',
  27. show: false
  28. },
  29. series: [{
  30. data: [],
  31. type: 'bar',
  32. }],
  33. };
  34. chart.setOption(option);
  35. return chart;
  36. }
  37. Page({
  38. /**
  39. * 页面的初始数据
  40. */
  41. data: {
  42. projects: [],
  43. project_ids: [],
  44. project_names: '',
  45. total_money: '0',
  46. month_money: '0',
  47. projectShow: false,
  48. index: 0,
  49. show: false,
  50. isSearch: true,
  51. charts: [{
  52. name: '柱状图',
  53. type: 'bar'
  54. }, {
  55. name: '折线图',
  56. type: 'line'
  57. }, {
  58. name: '饼状图',
  59. type: 'pie'
  60. }, {
  61. name: '明细图',
  62. type: 'detail'
  63. }, {
  64. name: '雷达图',
  65. type: 'radar'
  66. }],
  67. chartIndex: 0,
  68. dateTypes: [{
  69. name: '按年',
  70. type: 'year'
  71. }, {
  72. name: '按月',
  73. type: 'month'
  74. }],
  75. dateIndex: 0,
  76. dateShow: false,
  77. years_months: [],
  78. date: '',
  79. start: '',
  80. end: '',
  81. max_date: '',
  82. min_date: '',
  83. filter: {},
  84. data: [],
  85. ec: {
  86. onInit: initChart
  87. },
  88. detail_data: []
  89. },
  90. /**
  91. * 生命周期函数--监听页面加载
  92. */
  93. onLoad: function (options) {
  94. var that = this
  95. api.getByName(this, 'projects/getAll', 'projects', {}, function (res) {
  96. that.getData()
  97. });
  98. // api.getByName(this, 'data/getYearsAndMonths', 'years_months');
  99. // var maxDate = (new Date()).getTime()
  100. // var minDate = (new Date()).getTime() - 2 * 365 * 24 * 3600 * 1000
  101. // this.setData({
  102. // minDate,
  103. // maxDate
  104. // })
  105. this.getDateInfo()
  106. app.resetDataFilter()
  107. },
  108. switchCheck: function (e) {
  109. var index = e.currentTarget.dataset.index
  110. var projects = this.data.projects
  111. projects[index].checked = projects[index].checked ? false : true
  112. this.setData({
  113. projects
  114. })
  115. },
  116. closeProject: function (e) {
  117. var project_ids = []
  118. var project_names = []
  119. var projects = this.data.projects
  120. let charts = this.data.charts
  121. for (var i = 0; i < projects.length; ++i) {
  122. if (projects[i].checked) {
  123. project_ids.push(projects[i].id)
  124. project_names.push(projects[i].name)
  125. }
  126. }
  127. if (project_ids.length > 1) {
  128. charts = [{
  129. name: '柱状图',
  130. type: 'bar'
  131. }, {
  132. name: '折线图',
  133. type: 'line'
  134. }, {
  135. name: '雷达图',
  136. type: 'radar'
  137. }]
  138. } else {
  139. charts = [{
  140. name: '柱状图',
  141. type: 'bar'
  142. }, {
  143. name: '折线图',
  144. type: 'line'
  145. }, {
  146. name: '雷达图',
  147. type: 'radar'
  148. }, {
  149. name: '饼状图',
  150. type: 'pie'
  151. }, {
  152. name: '明细图',
  153. type: 'detail'
  154. }]
  155. }
  156. project_names = project_names.join(',')
  157. this.setData({
  158. project_ids,
  159. project_names,
  160. charts
  161. })
  162. this.switchShow(e)
  163. this.getData()
  164. this.getTotalInfo()
  165. },
  166. getSearchItems: function () {
  167. var ids = this.data.project_ids
  168. if (ids.length <= 0) return false
  169. var filter = this.data.filter
  170. var chart_type = this.data.charts[this.data.chartIndex]
  171. // var chat
  172. return {
  173. project_ids: ids,
  174. start_date: this.data.start_date,
  175. end_date: this.data.end_date,
  176. date: this.data.date,
  177. type: this.data.dateIndex == 0 ? 'year' : 'month',
  178. device_ids: filter.device_ids,
  179. device_name_ids: filter.device_name_ids,
  180. spec_ids: filter.spec_ids,
  181. rent_type_ids: filter.rent_type_ids,
  182. chart_type: chart_type.type
  183. }
  184. },
  185. getTotalInfo() {
  186. var data = this.getSearchItems()
  187. var that = this
  188. http({
  189. url: 'data/getTotalInfo',
  190. data: data,
  191. success: function (res) {
  192. if (res.code == 0) {
  193. that.setData(res.data)
  194. }
  195. }
  196. })
  197. },
  198. getDetailData() {
  199. var data = this.getSearchItems()
  200. var that = this
  201. http({
  202. url: 'data/getDetailData',
  203. data: data,
  204. success: function (res) {
  205. if (res.code == 0) {
  206. that.setData({
  207. detail_data: res.data
  208. })
  209. }
  210. }
  211. })
  212. },
  213. getData() {
  214. var data = this.getSearchItems()
  215. var that = this
  216. if (!data.project_ids || data.project_ids.length <= 0) return false
  217. var chart_type = this.data.charts[this.data.chartIndex]
  218. http({
  219. url: 'data/getStat',
  220. data: data,
  221. success: function (res) {
  222. if (res.code == 0) {
  223. if (chart_type == 'detail') {
  224. that.setData({
  225. detail_data: res.data
  226. })
  227. } else {
  228. that.setData({
  229. data: res.data
  230. })
  231. that.updateChart()
  232. }
  233. }
  234. }
  235. })
  236. },
  237. getDateInfo() {
  238. var that = this
  239. http({
  240. url: 'data/getDateInfo',
  241. data: {},
  242. success: function (res) {
  243. if (res.code == 0) {
  244. that.setData(res.data)
  245. }
  246. }
  247. })
  248. },
  249. onChange: function (e) {
  250. var value = e.detail.value
  251. var name = e.currentTarget.dataset.name
  252. if (this.data[name] == value) return false;
  253. this.setData({
  254. [name]: value
  255. })
  256. if (name == 'chartIndex') {
  257. var chart = this.data.charts[this.data.chartIndex]
  258. if (chart.type == 'detail') {
  259. this.getDetailData()
  260. } else {
  261. this.getData()
  262. }
  263. } else if (name == 'date') {
  264. this.getTotalInfo()
  265. }
  266. },
  267. updateChart: function () {
  268. var data = this.data.data
  269. option.xAxis.data = data.names;
  270. option.legend = {
  271. data: data.legends
  272. }
  273. var type = this.data.charts[this.data.chartIndex].type
  274. console.log(type)
  275. if (type == 'pie') {
  276. console.log(data)
  277. // option.series = [{
  278. // data: data.data,
  279. // type: type,
  280. // label: {
  281. // position: 'inner',
  282. // formatter: '{b}\n{d}%'
  283. // }
  284. // }]
  285. option = {
  286. legend: {
  287. orient: "vertical",
  288. left: "left",
  289. data: data.legends
  290. },
  291. series: [{
  292. type: type,
  293. data: data.data,
  294. left: "center",
  295. top:"30%"
  296. }],
  297. width: "70%",
  298. xAxis: {
  299. show:false
  300. },
  301. yAxis: {
  302. show:false
  303. },
  304. tooltip: {},
  305. grid: {},
  306. }
  307. } else if (type == 'radar') {
  308. option = {
  309. legend: {
  310. data: data.legends,
  311. bottom:0
  312. },
  313. radar: {
  314. // shape: 'circle',
  315. indicator: data.indicator
  316. },
  317. series: [{
  318. type: type,
  319. data: data.data
  320. }],
  321. xAxis: {
  322. show:false
  323. },
  324. yAxis: {
  325. show:false
  326. },
  327. tooltip: {},
  328. }
  329. } else if (type == 'line') {
  330. option.xAxis.show = true
  331. option = {
  332. xAxis: {
  333. type: "category",
  334. data: data.legends,
  335. axisLabel: {
  336. color: "#000",
  337. interval: 0,
  338. formatter: function (value) {
  339. if (value.length > 4) {
  340. return value.substring(0, 4) + "...";
  341. } else {
  342. return value;
  343. }
  344. }
  345. },
  346. },
  347. yAxis: {},
  348. series: [{
  349. data: data.values.flat(),
  350. type: type
  351. }],
  352. tooltip: {
  353. show: true,
  354. trigger: 'axis',
  355. triggerOn: 'click',
  356. axisPointer: {
  357. type: 'cross',
  358. axis: "x",
  359. },
  360. showContent: false
  361. },
  362. grid: {
  363. left: '15%'
  364. },
  365. }
  366. } else if (type == 'bar') {
  367. option = {
  368. xAxis: {
  369. data: data.legends,
  370. axisLabel: {
  371. color: "#000",
  372. interval: 0,
  373. formatter: function (value) {
  374. if (value.length > 4) {
  375. return value.substring(0, 4) + "...";
  376. } else {
  377. return value;
  378. }
  379. }
  380. },
  381. },
  382. yAxis: {},
  383. series: [{
  384. type: type,
  385. data: data.values.flat()
  386. }],
  387. tooltip: {
  388. show: true,
  389. trigger: 'axis',
  390. triggerOn: 'click',
  391. axisPointer: {
  392. type: 'cross',
  393. axis: "x",
  394. },
  395. showContent: false
  396. },
  397. grid: {
  398. left: '15%'
  399. },
  400. },
  401. option.title = {
  402. text: data.info.flat().join('-'),
  403. left: 'center',
  404. bottom: 10,
  405. textStyle: {
  406. fontSize: 14,
  407. color: "#5992fd"
  408. },
  409. }
  410. } else {
  411. option.xAxis.show = true
  412. var values = data.values
  413. for (var i = 0; i < values.length; ++i) {
  414. option.series[i] = {
  415. label: {
  416. show: true,
  417. position: 'top'
  418. },
  419. name: data.legends[i],
  420. type: type,
  421. data: values[i]
  422. }
  423. }
  424. }
  425. chart.setOption(option, true)
  426. },
  427. radioChange: function (e) {
  428. var dateIndex = e.currentTarget.dataset.index
  429. if (dateIndex == this.data.dateIndex) return false
  430. var start_date = this.data.start_date
  431. var end_date = this.data.end_date
  432. var start_date = dateIndex == 1 ? start_date + '-01' : start_date.substr(0, 7)
  433. var end_date = dateIndex == 1 ? end_date + '-01' : end_date.substr(0, 7)
  434. this.setData({
  435. dateIndex,
  436. start_date,
  437. end_date
  438. })
  439. },
  440. switchShow: function (e) {
  441. var name = e.currentTarget.dataset.name
  442. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  443. this.setData({
  444. [name]: show
  445. })
  446. if (name == 'dateShow' && !show) {
  447. this.getData()
  448. }
  449. },
  450. /**
  451. * 生命周期函数--监听页面初次渲染完成
  452. */
  453. onReady: function () {
  454. },
  455. /**
  456. * 生命周期函数--监听页面显示
  457. */
  458. onShow: function () {
  459. // this.getTabBar().init();
  460. var filter = wx.getStorageSync('sg-data-filters')
  461. this.setData({
  462. filter: filter
  463. })
  464. this.getData()
  465. },
  466. navigate: function (e) {
  467. var url = e.currentTarget.dataset.url
  468. wx.navigateTo({
  469. url: url + '?status=2',
  470. })
  471. },
  472. detailInfo: function () {
  473. wx.navigateTo({
  474. url: '../detailInfo/detailinfo?id=' + this.data.project_ids[0],
  475. })
  476. },
  477. /**
  478. * 生命周期函数--监听页面隐藏
  479. */
  480. onHide: function () {
  481. },
  482. /**
  483. * 生命周期函数--监听页面卸载
  484. */
  485. onUnload: function () {
  486. },
  487. /**
  488. * 页面相关事件处理函数--监听用户下拉动作
  489. */
  490. onPullDownRefresh: function () {
  491. },
  492. /**
  493. * 页面上拉触底事件的处理函数
  494. */
  495. onReachBottom: function () {
  496. },
  497. /**
  498. * 用户点击右上角分享
  499. */
  500. onShareAppMessage: function () {
  501. }
  502. })