index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. label: {
  352. show: true
  353. }
  354. }],
  355. tooltip: {
  356. show: true,
  357. trigger: 'axis',
  358. triggerOn: 'click',
  359. axisPointer: {
  360. type: 'cross',
  361. axis: "x",
  362. },
  363. showContent: false
  364. },
  365. grid: {
  366. left: '15%'
  367. },
  368. }
  369. } else if (type == 'bar') {
  370. option = {
  371. xAxis: {
  372. data: data.legends,
  373. axisLabel: {
  374. color: "#000",
  375. interval: 0,
  376. formatter: function (value) {
  377. if (value.length > 4) {
  378. return value.substring(0, 4) + "...";
  379. } else {
  380. return value;
  381. }
  382. }
  383. },
  384. },
  385. yAxis: {},
  386. series: [{
  387. type: type,
  388. data: data.values.flat(),
  389. label: {
  390. show: true
  391. }
  392. }],
  393. tooltip: {
  394. show: true,
  395. trigger: 'axis',
  396. triggerOn: 'click',
  397. axisPointer: {
  398. type: 'cross',
  399. axis: "x",
  400. },
  401. showContent: false
  402. },
  403. grid: {
  404. left: '15%'
  405. },
  406. },
  407. option.title = {
  408. text: data.info.flat().join('-'),
  409. left: 'center',
  410. bottom: 10,
  411. textStyle: {
  412. fontSize: 14,
  413. color: "#5992fd"
  414. },
  415. }
  416. } else {
  417. option.xAxis.show = true
  418. var values = data.values
  419. for (var i = 0; i < values.length; ++i) {
  420. option.series[i] = {
  421. label: {
  422. show: true,
  423. position: 'top'
  424. },
  425. name: data.legends[i],
  426. type: type,
  427. data: values[i]
  428. }
  429. }
  430. }
  431. chart.setOption(option, true)
  432. },
  433. radioChange: function (e) {
  434. var dateIndex = e.currentTarget.dataset.index
  435. if (dateIndex == this.data.dateIndex) return false
  436. var start_date = this.data.start_date
  437. var end_date = this.data.end_date
  438. var start_date = dateIndex == 1 ? start_date + '-01' : start_date.substr(0, 7)
  439. var end_date = dateIndex == 1 ? end_date + '-01' : end_date.substr(0, 7)
  440. this.setData({
  441. dateIndex,
  442. start_date,
  443. end_date
  444. })
  445. },
  446. switchShow: function (e) {
  447. var name = e.currentTarget.dataset.name
  448. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  449. this.setData({
  450. [name]: show
  451. })
  452. if (name == 'dateShow' && !show) {
  453. this.getData()
  454. }
  455. },
  456. /**
  457. * 生命周期函数--监听页面初次渲染完成
  458. */
  459. onReady: function () {
  460. },
  461. /**
  462. * 生命周期函数--监听页面显示
  463. */
  464. onShow: function () {
  465. // this.getTabBar().init();
  466. var filter = wx.getStorageSync('sg-data-filters')
  467. this.setData({
  468. filter: filter
  469. })
  470. this.getData()
  471. },
  472. navigate: function (e) {
  473. var url = e.currentTarget.dataset.url
  474. let num = this.data.project_ids.length
  475. wx.navigateTo({
  476. url: url + '?status=2&project_length=' + num,
  477. })
  478. },
  479. detailInfo: function () {
  480. wx.navigateTo({
  481. url: '../detailInfo/detailinfo?id=' + this.data.project_ids[0],
  482. })
  483. },
  484. /**
  485. * 生命周期函数--监听页面隐藏
  486. */
  487. onHide: function () {
  488. },
  489. /**
  490. * 生命周期函数--监听页面卸载
  491. */
  492. onUnload: function () {
  493. },
  494. /**
  495. * 页面相关事件处理函数--监听用户下拉动作
  496. */
  497. onPullDownRefresh: function () {
  498. },
  499. /**
  500. * 页面上拉触底事件的处理函数
  501. */
  502. onReachBottom: function () {
  503. },
  504. /**
  505. * 用户点击右上角分享
  506. */
  507. onShareAppMessage: function () {
  508. }
  509. })