index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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.xAxis.show = false
  286. option = {
  287. legend: {
  288. orient: "vertical",
  289. left: "left",
  290. data: data.legends
  291. },
  292. series: [{
  293. type: type,
  294. data: data.data,
  295. left:"center",
  296. }],
  297. width: "70%",
  298. xAxis: {},
  299. yAxis: {},
  300. tooltip: {},
  301. grid: {},
  302. }
  303. } else if (type == 'radar') {
  304. option.xAxis.show = false
  305. option = {
  306. legend: {
  307. data: data.legends
  308. },
  309. radar: {
  310. // shape: 'circle',
  311. indicator: data.indicator
  312. },
  313. series: [{
  314. type: type,
  315. data: data.data
  316. }],
  317. xAxis: {},
  318. yAxis: {},
  319. tooltip: {},
  320. }
  321. } else if (type == 'line') {
  322. option.xAxis.show = true
  323. option = {
  324. xAxis: {
  325. type: "category",
  326. data: data.legends,
  327. axisLabel: {
  328. color: "#000",
  329. interval: 0,
  330. formatter: function (value) {
  331. if (value.length > 4) {
  332. return value.substring(0, 4) + "...";
  333. } else {
  334. return value;
  335. }
  336. }
  337. },
  338. },
  339. yAxis: {},
  340. series: [{
  341. data: data.values.flat(),
  342. type: type
  343. }],
  344. tooltip: {
  345. show: true,
  346. trigger: 'axis',
  347. triggerOn: 'click',
  348. axisPointer: {
  349. type: 'cross',
  350. axis: "x",
  351. },
  352. showContent: false
  353. },
  354. grid: {
  355. left: '15%'
  356. },
  357. }
  358. } else if (type == 'bar') {
  359. option = {
  360. xAxis: {
  361. data: data.legends,
  362. axisLabel: {
  363. color: "#000",
  364. interval: 0,
  365. formatter: function (value) {
  366. if (value.length > 4) {
  367. return value.substring(0, 4) + "...";
  368. } else {
  369. return value;
  370. }
  371. }
  372. },
  373. },
  374. yAxis: {},
  375. series: [{
  376. type: type,
  377. data: data.values.flat()
  378. }],
  379. tooltip: {
  380. show: true,
  381. trigger: 'axis',
  382. triggerOn: 'click',
  383. axisPointer: {
  384. type: 'cross',
  385. axis: "x",
  386. },
  387. showContent: false
  388. },
  389. grid: {
  390. left: '15%'
  391. },
  392. },
  393. option.title = {
  394. text: data.info.flat().join('-'),
  395. left: 'center',
  396. bottom: 10,
  397. textStyle: {
  398. fontSize: 14,
  399. color: "#5992fd"
  400. },
  401. }
  402. } else {
  403. option.xAxis.show = true
  404. var values = data.values
  405. for (var i = 0; i < values.length; ++i) {
  406. option.series[i] = {
  407. label: {
  408. show: true,
  409. position: 'top'
  410. },
  411. name: data.legends[i],
  412. type: type,
  413. data: values[i]
  414. }
  415. }
  416. }
  417. chart.setOption(option, true)
  418. },
  419. radioChange: function (e) {
  420. var dateIndex = e.currentTarget.dataset.index
  421. if (dateIndex == this.data.dateIndex) return false
  422. var start_date = this.data.start_date
  423. var end_date = this.data.end_date
  424. var start_date = dateIndex == 1 ? start_date + '-01' : start_date.substr(0, 7)
  425. var end_date = dateIndex == 1 ? end_date + '-01' : end_date.substr(0, 7)
  426. this.setData({
  427. dateIndex,
  428. start_date,
  429. end_date
  430. })
  431. },
  432. switchShow: function (e) {
  433. var name = e.currentTarget.dataset.name
  434. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  435. this.setData({
  436. [name]: show
  437. })
  438. if (name == 'dateShow' && !show) {
  439. this.getData()
  440. }
  441. },
  442. /**
  443. * 生命周期函数--监听页面初次渲染完成
  444. */
  445. onReady: function () {
  446. },
  447. /**
  448. * 生命周期函数--监听页面显示
  449. */
  450. onShow: function () {
  451. // this.getTabBar().init();
  452. var filter = wx.getStorageSync('sg-data-filters')
  453. this.setData({
  454. filter: filter
  455. })
  456. this.getData()
  457. },
  458. navigate: function (e) {
  459. var url = e.currentTarget.dataset.url
  460. wx.navigateTo({
  461. url: url + '?status=2',
  462. })
  463. },
  464. detailInfo: function () {
  465. wx.navigateTo({
  466. url: '../detailInfo/detailinfo',
  467. })
  468. },
  469. /**
  470. * 生命周期函数--监听页面隐藏
  471. */
  472. onHide: function () {
  473. },
  474. /**
  475. * 生命周期函数--监听页面卸载
  476. */
  477. onUnload: function () {
  478. },
  479. /**
  480. * 页面相关事件处理函数--监听用户下拉动作
  481. */
  482. onPullDownRefresh: function () {
  483. },
  484. /**
  485. * 页面上拉触底事件的处理函数
  486. */
  487. onReachBottom: function () {
  488. },
  489. /**
  490. * 用户点击右上角分享
  491. */
  492. onShareAppMessage: function () {
  493. }
  494. })