lineChart.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. // pages/lineChart/lineChart.js
  2. import * as echarts from '../../components/ec-canvas/echarts.min'
  3. var app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. option1: [], //下拉菜单选项
  10. value1: 0, //下拉菜单默认选中项
  11. screenHeight: '', //屏幕可视高度
  12. areaID: '', //区域id
  13. user: {
  14. lazyLoad: true // 延迟加载
  15. },
  16. order: {
  17. lazyLoad: true // 延迟加载
  18. },
  19. userDate: '',
  20. userData: '',
  21. newOrderChartData: {},
  22. earningsChartData: {},
  23. dayOrderChartData: {},
  24. earnShow: false,
  25. navBarHeight: app.globalData.navBarHeight, // 导航栏高度
  26. menuRight: app.globalData.menuRight, // 胶囊距右方间距(方保持左、右间距一致)
  27. menuTop: app.globalData.menuTop, // 胶囊距底部间距(保持底部间距一致)
  28. menuHeight: app.globalData.menuHeight, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad: function (options) {
  34. console.log(wx.getStorageSync('areaID'))
  35. var that = this;
  36. that.setData({
  37. screenHeight: app.globalData.screenHeight,
  38. option1: wx.getStorageSync('allArea'),
  39. option3: wx.getStorageSync('allState')
  40. })
  41. console.log(that.data.option1)
  42. let curVal = wx.getStorageSync('curVal')
  43. console.log(curVal, 'log_curVal')
  44. if (curVal) {
  45. that.setData({
  46. value1: curVal
  47. })
  48. }
  49. let areaID = wx.getStorageSync('curId')
  50. console.log(areaID, 'log_areaID')
  51. if (areaID) {
  52. that.setData({
  53. areaID
  54. })
  55. } else {
  56. let id = that.data.option1[0].areaID
  57. that.setData({
  58. areaID: id
  59. })
  60. }
  61. that.menu = that.selectComponent("#menu");
  62. // that.menu.changeTitle(that.data.option1[that.data.value1].text);
  63. that.getData();
  64. },
  65. back() {
  66. wx.navigateBack({
  67. delta: 1
  68. })
  69. },
  70. change(e) {
  71. let that = this;
  72. let index = e.detail.value
  73. wx.setStorageSync('curVal', index)
  74. wx.setStorageSync('curId', that.data.option1[index].areaID)
  75. this.setData({
  76. areaID: that.data.option1[index].areaID,
  77. value1:index
  78. })
  79. this.getData();
  80. },
  81. userInit: function () {
  82. this.userEcharts = this.selectComponent('#userChart');
  83. this.userEcharts.init((canvas, width, height, dpr) => {
  84. // 初始化图表
  85. const Chart = echarts.init(canvas, null, {
  86. width: width,
  87. height: height,
  88. devicePixelRatio: dpr //解决小程序视图模糊的问题,必写
  89. });
  90. Chart.setOption(this.userOption());
  91. // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  92. return Chart;
  93. });
  94. },
  95. orderInit: function () {
  96. this.orderEcharts = this.selectComponent('#orderChart');
  97. this.orderEcharts.init((canvas, width, height, dpr) => {
  98. // 初始化图表
  99. const Chart = echarts.init(canvas, 'light', {
  100. width: width,
  101. height: height,
  102. devicePixelRatio: dpr //解决小程序视图模糊的问题,必写
  103. });
  104. Chart.setOption(this.orderOption());
  105. // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  106. return Chart;
  107. });
  108. },
  109. dayOrderInit: function () {
  110. this.orderEcharts = this.selectComponent('#dayOrderChart');
  111. this.orderEcharts.init((canvas, width, height, dpr) => {
  112. // 初始化图表
  113. const Chart = echarts.init(canvas, 'light', {
  114. width: width,
  115. height: height,
  116. devicePixelRatio: dpr //解决小程序视图模糊的问题,必写
  117. });
  118. Chart.setOption(this.dayOrderOption());
  119. // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  120. return Chart;
  121. });
  122. },
  123. earningsInit: function () {
  124. this.earningsChart = this.selectComponent('#earningsChart');
  125. this.earningsChart.init((canvas, width, height, dpr) => {
  126. // 初始化图表
  127. const Chart = echarts.init(canvas, 'light', {
  128. width: width,
  129. height: height,
  130. devicePixelRatio: dpr //解决小程序视图模糊的问题,必写
  131. });
  132. Chart.setOption(this.earningsOption());
  133. // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  134. return Chart;
  135. });
  136. },
  137. getData() {
  138. wx.showLoading({
  139. title: '加载中...',
  140. })
  141. let data = {
  142. area_id: this.data.areaID,
  143. days: 'thirtyDays'
  144. }
  145. app.request('user/newUsersChart', data, 'GET').then(res => {
  146. console.log(res)
  147. var user = res.data;
  148. var userDate = [];
  149. var userData = [];
  150. user.map(i => {
  151. userDate.push(i.date)
  152. userData.push(i.value)
  153. })
  154. this.setData({
  155. userDate,
  156. userData
  157. })
  158. this.userInit();
  159. })
  160. app.request('order/newOrderChart', data, 'GET').then(res => {
  161. console.log(res.data, '订单')
  162. let order = res.data;
  163. let newOrderChartData = {
  164. expectedData: {
  165. data: [],
  166. date: [],
  167. legendData: [],
  168. }
  169. }
  170. order.map(i => {
  171. newOrderChartData.expectedData.legendData.push(i.name)
  172. let arr = []
  173. let date = []
  174. i.data.map(e => {
  175. arr.push(e.value)
  176. date.push(e.date)
  177. })
  178. newOrderChartData.expectedData.data.push(arr)
  179. newOrderChartData.expectedData.date = date
  180. })
  181. this.setData({
  182. newOrderChartData
  183. })
  184. this.orderInit();
  185. })
  186. app.request('order/profitChart', data, 'GET').then(res => {
  187. console.log(res.data, '收益')
  188. let earnings = res.data;
  189. if (earnings.length > 0) {
  190. console.log(1111)
  191. this.setData({
  192. earnShow: true
  193. })
  194. let earningsChartData = {
  195. expectedData: {
  196. data: [],
  197. date: [],
  198. legendData: [],
  199. }
  200. }
  201. earnings.map(i => {
  202. earningsChartData.expectedData.legendData.push(i.name)
  203. let arr = []
  204. let date = []
  205. i.data.map(e => {
  206. arr.push(e.value)
  207. date.push(e.date)
  208. })
  209. earningsChartData.expectedData.data.push(arr)
  210. earningsChartData.expectedData.date = date
  211. })
  212. this.setData({
  213. earningsChartData
  214. })
  215. this.earningsInit();
  216. }
  217. })
  218. let day = {
  219. area_id: this.data.areaID,
  220. }
  221. app.request('order/hourOrderNumber', day, 'GET').then(res => {
  222. if (res.statusCode == 200) {
  223. console.log(res, '测试')
  224. let dayOrderChart = res.data;
  225. let dayOrderChartData = {
  226. expectedData: {
  227. data: [],
  228. date: [],
  229. legendData: [],
  230. }
  231. }
  232. dayOrderChartData.expectedData.data = dayOrderChart.data
  233. dayOrderChartData.expectedData.date = dayOrderChart.date
  234. this.setData({
  235. dayOrderChartData
  236. })
  237. this.dayOrderInit();
  238. }
  239. })
  240. wx.hideLoading();
  241. },
  242. userOption() {
  243. let that = this;
  244. var option = {
  245. xAxis: {
  246. data: that.data.userDate,
  247. boundaryGap: false,
  248. axisTick: {
  249. show: false,
  250. },
  251. axisLabel: {
  252. rotate: 60,
  253. },
  254. axisLine: {
  255. lineStyle: {
  256. color: '#409EFF'
  257. }
  258. }
  259. },
  260. grid: {
  261. left: 10,
  262. right: 10,
  263. bottom: 20,
  264. top: 30,
  265. containLabel: true,
  266. },
  267. tooltip: {
  268. trigger: 'axis',
  269. axisPointer: {
  270. type: 'cross',
  271. },
  272. padding: [5, 10],
  273. },
  274. yAxis: {
  275. axisTick: {
  276. show: false,
  277. },
  278. axisLine: {
  279. lineStyle: {
  280. color: '#409EFF'
  281. }
  282. }
  283. },
  284. legend: {
  285. data: ['expected'],
  286. },
  287. series: [{
  288. name: '每日新增',
  289. itemStyle: {
  290. normal: {
  291. color: '#FF005A',
  292. lineStyle: {
  293. color: '#FF005A',
  294. width: 2,
  295. },
  296. },
  297. },
  298. smooth: true,
  299. type: 'line',
  300. data: that.data.userData,
  301. animationDuration: 2800,
  302. animationEasing: 'cubicInOut',
  303. }],
  304. };
  305. return option;
  306. },
  307. orderOption() {
  308. let that = this;
  309. var expectedData = that.data.newOrderChartData.expectedData
  310. let seriesDatas = []
  311. expectedData.data.map((i, index) => {
  312. seriesDatas.push({
  313. name: expectedData.legendData[index],
  314. type: 'line',
  315. smooth: true,
  316. animationDuration: 2800,
  317. animationEasing: 'cubicInOut',
  318. data: i,
  319. })
  320. })
  321. var option = {
  322. xAxis: {
  323. data: expectedData.date,
  324. boundaryGap: false,
  325. axisTick: {
  326. show: false,
  327. },
  328. axisLabel: {
  329. rotate: 60,
  330. },
  331. axisLine: {
  332. lineStyle: {
  333. color: '#409EFF'
  334. }
  335. }
  336. },
  337. grid: {
  338. left: '2%',
  339. right: 10,
  340. bottom: 20,
  341. top: 30,
  342. containLabel: true,
  343. },
  344. tooltip: {
  345. trigger: 'axis',
  346. axisPointer: {
  347. type: 'cross',
  348. },
  349. padding: [5, 10],
  350. },
  351. yAxis: {
  352. axisTick: {
  353. show: false,
  354. },
  355. axisLine: {
  356. lineStyle: {
  357. color: '#409EFF'
  358. }
  359. }
  360. },
  361. legend: {
  362. data: expectedData.legendData,
  363. },
  364. series: seriesDatas,
  365. }
  366. return option;
  367. },
  368. earningsOption() {
  369. let that = this;
  370. var earningsData = that.data.earningsChartData.expectedData
  371. let seriesDatas = []
  372. earningsData.data.map((i, index) => {
  373. seriesDatas.push({
  374. name: earningsData.legendData[index],
  375. type: 'line',
  376. smooth: true,
  377. animationDuration: 2800,
  378. animationEasing: 'cubicInOut',
  379. data: i,
  380. })
  381. })
  382. var option = {
  383. xAxis: {
  384. data: earningsData.date,
  385. boundaryGap: false,
  386. axisTick: {
  387. show: false,
  388. },
  389. axisLabel: {
  390. rotate: 60,
  391. },
  392. axisLine: {
  393. lineStyle: {
  394. color: '#409EFF'
  395. }
  396. }
  397. },
  398. grid: {
  399. left: '2%',
  400. right: 10,
  401. bottom: 20,
  402. top: 30,
  403. containLabel: true,
  404. },
  405. tooltip: {
  406. trigger: 'axis',
  407. axisPointer: {
  408. type: 'cross',
  409. },
  410. padding: [5, 10],
  411. },
  412. yAxis: {
  413. axisTick: {
  414. show: false,
  415. },
  416. axisLine: {
  417. lineStyle: {
  418. color: '#409EFF'
  419. }
  420. }
  421. },
  422. legend: {
  423. data: earningsData.legendData,
  424. },
  425. series: seriesDatas,
  426. }
  427. return option;
  428. },
  429. dayOrderOption() {
  430. let that = this;
  431. var expectedData = that.data.dayOrderChartData.expectedData
  432. // console.log(expectedData)
  433. var options1 = []
  434. expectedData.date.map(i => {
  435. options1.push({
  436. title: {
  437. text: i
  438. },
  439. series: [{
  440. data: expectedData.data[i]
  441. }],
  442. })
  443. })
  444. var option = {
  445. baseOption: {
  446. timeline: {
  447. // y: 0,
  448. axisType: 'category',
  449. realtime: true,
  450. loop: true,
  451. autoPlay: false,
  452. // currentIndex: 2,
  453. playInterval: 1000,
  454. // controlStyle: {
  455. // position: 'left'
  456. // },
  457. data: expectedData.date,
  458. label: {
  459. formatter: function (s) {
  460. return (new Date(s).getMonth() + 1) + '-' + new Date(s).getDate()
  461. },
  462. color: '#409EFF'
  463. },
  464. lineStyle: {
  465. color: '#409EFF'
  466. },
  467. itemStyle: {
  468. color: '#409EFF',
  469. },
  470. controlStyle: {
  471. color: '#409EFF',
  472. borderColor: '#409EFF',
  473. }
  474. },
  475. title: {
  476. // subtext: '数据来自国家统计局',
  477. },
  478. tooltip: {},
  479. legend: {
  480. x: 'right',
  481. data: ['新订单'],
  482. },
  483. calculable: true,
  484. grid: {
  485. top: 80,
  486. bottom: 100,
  487. tooltip: {
  488. trigger: 'axis',
  489. axisPointer: {
  490. type: 'shadow',
  491. label: {
  492. show: true,
  493. formatter: function (params) {
  494. return params.value.replace('\n', '')
  495. },
  496. },
  497. },
  498. },
  499. },
  500. xAxis: [{
  501. type: 'category',
  502. axisLabel: {
  503. interval: 0
  504. },
  505. axisLine: {
  506. lineStyle: {
  507. color: '#409EFF'
  508. }
  509. },
  510. data: [
  511. '0',
  512. '\n1',
  513. '2',
  514. '\n3',
  515. '4',
  516. '\n5',
  517. '6',
  518. '\n7',
  519. '8',
  520. '\n9',
  521. '10',
  522. '\n11',
  523. '12',
  524. '\n13',
  525. '14',
  526. '\n15',
  527. '16',
  528. '\n17',
  529. '18',
  530. '\n19',
  531. '20',
  532. '\n21',
  533. '22',
  534. '\n23',
  535. '24',
  536. ],
  537. splitLine: {
  538. show: false
  539. },
  540. }, ],
  541. yAxis: [{
  542. type: 'value',
  543. name: '数量',
  544. axisLine: {
  545. lineStyle: {
  546. color: '#409EFF'
  547. }
  548. }
  549. }],
  550. series: [{
  551. name: '新订单',
  552. type: 'bar'
  553. }],
  554. },
  555. options: options1,
  556. }
  557. return option;
  558. },
  559. /**
  560. * 生命周期函数--监听页面初次渲染完成
  561. */
  562. onReady: function () {
  563. },
  564. /**
  565. * 生命周期函数--监听页面显示
  566. */
  567. onShow: function () {
  568. },
  569. /**
  570. * 生命周期函数--监听页面隐藏
  571. */
  572. onHide: function () {
  573. },
  574. /**
  575. * 生命周期函数--监听页面卸载
  576. */
  577. onUnload: function () {
  578. },
  579. /**
  580. * 页面相关事件处理函数--监听用户下拉动作
  581. */
  582. onPullDownRefresh: function () {
  583. },
  584. /**
  585. * 页面上拉触底事件的处理函数
  586. */
  587. onReachBottom: function () {
  588. },
  589. /**
  590. * 用户点击右上角分享
  591. */
  592. onShareAppMessage: function () {
  593. }
  594. })