data_statistics.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // parents//pages/growth_track/growth_track.js
  2. import F2 from '../../../utils/f2-canvas/lib/f2';
  3. const wxCharts = require("../../../utils/wxcharts-min.js");
  4. let chart;
  5. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  6. import { DayCount, MonthCount } from '../../../utils/api.js'
  7. const { navigateTo, redirectTo, navigateBack } = routers()
  8. const { globalData, hasLibraryService, hasVipService } = getApp()
  9. const { baseImgUrl, qrcodeInvalidToastText } = globalData
  10. /**
  11. * 进度条颜色RGB
  12. */
  13. const colorRGB = '255,215,108'
  14. const colorRGB1 = '255,128,168'
  15. const colorRGB2 = '0,183,255'
  16. const colorRGB3 = '255,97,97'
  17. const colorRGB4 = '121,189,232'
  18. const colorRGB5 = '250,202,106'
  19. const colorRGB6 = '143,232,121'
  20. const colorRGB7 = '117,227,243'
  21. const colorRGB8 = '255,145,128'
  22. const colorRGB9 = '119,143,242'
  23. Page({
  24. /**
  25. * 页面的初始数据
  26. */
  27. data: {
  28. baseImgUrl,
  29. isLoaded: 1,
  30. date: '',
  31. month:'',
  32. ec: {
  33. lazyLoad: true // 延迟加载组件
  34. },
  35. pageNo: 1,
  36. pageSize: 5,
  37. isAll: 0,
  38. opts: {
  39. lazyLoad: true // 延迟加载组件
  40. },
  41. dayInfo:[],
  42. monthInfo:[],
  43. themeSelect:false,
  44. rankList:[
  45. ],
  46. messarr: [
  47. {
  48. color: '#f7c11b',
  49. flownum: '0',
  50. },
  51. {
  52. color: '#17d0bc',
  53. flownum: '0',
  54. }
  55. ],
  56. footerData: {
  57. actIndex:1,
  58. list: [
  59. {
  60. text: '找好书',
  61. icon: '../../../assets/book_gray.png',
  62. actIcon: '../../../assets/book_blue.png',
  63. url: 'parents/pages/main/main',
  64. zindex: 3,
  65. },
  66. {
  67. text: '成长轨迹',
  68. icon: '../../../assets/wish_gray.png',
  69. actIcon: '../../../assets/wish_blue.png',
  70. url: 'parents/pages/growth_track/growth_track',
  71. zindex: 3,
  72. },
  73. {
  74. text: '我的',
  75. icon: '../../../assets/me_gray.png',
  76. actIcon: '../../../assets/me_blue.png',
  77. url: 'parents/pages/me/me',
  78. zindex: 3,
  79. }
  80. ]
  81. },
  82. nodataArray: {
  83. text: "暂无阅读计划",
  84. margin: '140rpx auto'
  85. }
  86. },
  87. /**
  88. * 生命周期函数--监听页面加载
  89. */
  90. onLoad: function (options) {
  91. this.GetDate()
  92. this.GetMonthDate()
  93. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`)
  94. options.libId = getGlobalVal('list').length > 0 ? getGlobalVal('list')[0].libId : ''
  95. this.setData(options)
  96. this.getData(res => {
  97. this.setData({ isLoaded: 1 })
  98. })
  99. this.getMonthData(res => {
  100. this.setData({ isLoaded: 1 })
  101. })
  102. },
  103. /**
  104. * 生命周期函数--监听页面初次渲染完成
  105. */
  106. onReady: function () {
  107. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  108. for (const i in obj) {
  109. this[i] = obj[i]
  110. }
  111. },
  112. /**
  113. * 生命周期函数--监听页面显示
  114. */
  115. onShow: function () {
  116. this.getData()
  117. // this.nearTwentyEight()
  118. // this.everyReading()
  119. },
  120. /**
  121. * 生命周期函数--监听页面隐藏
  122. */
  123. onHide: function () {
  124. },
  125. /**
  126. * 生命周期函数--监听页面卸载
  127. */
  128. onUnload: function () {
  129. },
  130. /**
  131. * 页面相关事件处理函数--监听用户下拉动作
  132. */
  133. onPullDownRefresh: function () {
  134. this.setData(() => {
  135. wx.stopPullDownRefresh()
  136. })
  137. },
  138. /**
  139. * 页面上拉触底事件的处理函数
  140. */
  141. onReachBottom: function () {
  142. },
  143. /**
  144. * 用户点击右上角分享
  145. */
  146. onShareAppMessage: function () {
  147. if (isFn(sharePage)) return sharePage()
  148. },
  149. /**
  150. * 选择跳转进入页
  151. */
  152. selectNavCtl: function (e) {
  153. const { index } = e.currentTarget.dataset
  154. const { date, month } = this.data
  155. if (index == 1) {
  156. wx.setStorageSync('date', date);
  157. navigateTo({
  158. url: 'president/pages/library_sign_num/library_sign_num?isMonthData=1',
  159. })
  160. }
  161. if (index == 2) {
  162. wx.setStorageSync('monthValue', month);
  163. navigateTo({
  164. url: 'president/pages/library_sign_num/library_sign_num?isMonthData=2',
  165. })
  166. }
  167. },
  168. /**
  169. * 好书推荐图表
  170. */
  171. goodBooksPicture: function () {
  172. const { messarr } = this.data
  173. // console.log(themeInfoList)
  174. var that = this
  175. if (messarr.length === 0) return
  176. this.selectComponent('#pieSelect').init(function (canvas, width, height) {
  177. // 数据
  178. const data = (list => {
  179. const arr = []
  180. for (const v of list) {
  181. const { themeName = '', percentage = 0, } = v
  182. arr.push({
  183. const: 'const',
  184. type: themeName,
  185. percent: ~~(+percentage * 100)
  186. })
  187. }
  188. return arr
  189. })(messarr)
  190. // console.log(data)
  191. // 生成画布
  192. const chart = new F2.Chart({
  193. el: canvas,
  194. width,
  195. height
  196. })
  197. chart.source(data)
  198. chart.coord('polar', {
  199. transposed: true,
  200. radius: 0.75,
  201. innerRadius: 0.48
  202. })
  203. chart.axis(false)
  204. chart.legend(false)
  205. chart.tooltip(false)
  206. chart.interval().position('const*percent').adjust('stack').color('type', [
  207. `rgba(${colorRGB}, 1)`,
  208. `rgba(${colorRGB9}, 1)`,
  209. ])
  210. chart.interaction('pie-select', {
  211. cancelable: false,
  212. animate: {
  213. duration: 300,
  214. easing: 'backOut'
  215. },
  216. onEnd: res => {
  217. const { data } = res
  218. that.setData({
  219. pageNo: 1,
  220. themeSelect: true
  221. })
  222. }
  223. });
  224. chart.pieLabel({
  225. sidePadding: 30,
  226. sidePadding: 70,
  227. adjustOffset:1000,
  228. activeShape: false,
  229. label1: function label1(data) {
  230. return {
  231. text: data.percent + '%',
  232. fill: '#333333',
  233. fontWeight: 'bold'
  234. };
  235. },
  236. label2: function label2(data) {
  237. return {
  238. text: data.type,
  239. fill: '#cccccc',
  240. };
  241. },
  242. // onClick: function onClick(e) {
  243. // const { data = {} } = e
  244. // console.log(data)
  245. // }
  246. });
  247. chart.render()
  248. return chart
  249. })
  250. },
  251. /**
  252. * 日统计数据
  253. */
  254. getData: function (cb) {
  255. const continuousFn = { fn: this.getData, param: { ...arguments } }
  256. const { libId, date} = this.data
  257. DayCount({ data: { date, libId }, continuousFn }).then(res => {
  258. const { dayInfo } = res.data
  259. console.log(dayInfo)
  260. this.setData({ dayInfo })
  261. // debugger
  262. cb && cb(res)
  263. }).catch(res => {
  264. cb && cb(res)
  265. })
  266. },
  267. GetDate: function(){
  268. const now = new Date();
  269. const year = now.getFullYear();    //年
  270. const month = now.getMonth() + 1; //月
  271. const day = now.getDate();   //日
  272. const nowdate = year + '-' + month.toString().padStart(2, '0') + '-' + day.toString().padStart(2, '0') //月
  273. this.setData({ date: nowdate })
  274. },
  275. GetMonthDate: function () {
  276. const now = new Date();
  277. const year = now.getFullYear();    //年
  278. const month = now.getMonth() + 1;
  279. const nowdate = year + '-' + month.toString().padStart(2, '0') //月
  280. this.setData({ month: nowdate })
  281. },
  282. /**
  283. * 获取月统计数据
  284. */
  285. getMonthData: function (cb) {
  286. const continuousFn = { fn: this.getData, param: { ...arguments } }
  287. const { libId, month, messarr } = this.data
  288. MonthCount({ data: { month: month,libId }, continuousFn }).then(res => {
  289. const { monthInfo } = res.data
  290. messarr[0].percentage = Math.round(monthInfo.activeCount / monthInfo.allCount * 100) / 100
  291. messarr[1].percentage = (1 - Math.round(monthInfo.activeCount / monthInfo.allCount * 100) / 100)
  292. messarr[0].themeName = "活跃用户"
  293. messarr[1].themeName = "沉默用户"
  294. console.log(messarr)
  295. this.setData({ monthInfo })
  296. console.log(monthInfo)
  297. this.goodBooksPicture()
  298. if (isFn(cb)) cb()
  299. }).catch(res => {
  300. this.setData({ isLoaded: true })
  301. if (isFn(cb)) cb()
  302. })
  303. },
  304. /**
  305. * 选择日期
  306. */
  307. changeDate: function (e) {
  308. const { value } = e.detail;
  309. this.setData({ date: value })
  310. this.getData()
  311. },
  312. /**
  313. * 选择月
  314. */
  315. changeMonthDate: function (e) {
  316. const { value } = e.detail;
  317. this.setData({ month: value })
  318. this.getMonthData()
  319. },
  320. })