avg.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // pages/avg/avg.js
  2. import * as echarts from '../../ec-canvas/echarts';
  3. const $api = require('../../utils/api.js').API;
  4. var records = []
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. tab:['全体选手','冠军组','千万组','百万组'],
  11. list:[],
  12. cur:0,
  13. match_name:'',
  14. ec: {
  15. onInit: null
  16. },
  17. datas:[],
  18. days:[],
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad(options) {
  24. let nlist = JSON.parse(decodeURIComponent(options.list))
  25. nlist.unshift(nlist[0])
  26. // nlist[0].name='全体选手'
  27. console.log(nlist)
  28. this.setData({
  29. cur:options.index,
  30. list:nlist,
  31. match_name:nlist[0].match_name,
  32. })
  33. if(options.index == 0){
  34. this.loadAvg()
  35. } else{
  36. this.loadGroupAvg()
  37. }
  38. },
  39. loadGroupAvg(){
  40. var group = this.data.list[this.data.cur]
  41. var param = {
  42. match_id:group.match_id,
  43. group_id:group.match_group,
  44. }
  45. $api.getAllDay(param).then(res => {
  46. // console.log(res.data.data.list)
  47. if(res.data.code == 0){
  48. var week=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五']
  49. var days=res.data.data.list,weeks=[[]]
  50. days=days.reverse()
  51. week.forEach((item,index)=>{
  52. weeks[index]={
  53. name:'第'+item+'周',
  54. data:[],
  55. total:0
  56. }
  57. })
  58. var i=0
  59. days.forEach(item=>{
  60. var w=new Date(item.stock_date).getDay()
  61. item.day=w
  62. if(w==1){
  63. i++
  64. }
  65. weeks[i].data.push(item)
  66. })
  67. weeks.forEach(item=>{
  68. if(item.data.length>0){
  69. var total=0
  70. var money = 10000.0
  71. item.data.forEach(i=>{
  72. var incom=Number(i.today_income.replace('%',''))
  73. money += money*incom/100.0
  74. i.date=(new Date(i.stock_date).getMonth()+1)+'/'+(new Date(i.stock_date).getDate())
  75. })
  76. total = (money-10000)*100.0/10000.0
  77. item.total=total.toFixed(2)
  78. }
  79. })
  80. records = res.data.data.list
  81. this.setData({
  82. ec: {
  83. onInit: initChart
  84. },
  85. days:weeks.reverse()
  86. })
  87. }
  88. })
  89. },
  90. //计算周数
  91. getWeekNumber:function(dateString) {
  92. const date = new Date(dateString);
  93. const year = date.getFullYear();
  94. // 将年份的第一个周一作为第一周的开始
  95. const firstWeekStart = new Date(year, 0, 1);
  96. firstWeekStart.setDate(firstWeekStart.getDate() - firstWeekStart.getDay() + 1);
  97. // 计算给定日期是一年中的第几周
  98. const currentDate = new Date(date);
  99. currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1);
  100. let milliseconds = currentDate.getTime() - firstWeekStart.getTime();
  101. const weeks = Math.round(milliseconds / (7 * 24 * 60 * 60 * 1000));
  102. return weeks + 1; // 因为getWeek返回的是年初至今的周数,所以加1得到完整的周数
  103. },
  104. loadAvg(){
  105. var group = this.data.list[0]
  106. var param = {
  107. match_id:group.match_id,
  108. }
  109. $api.getAvg(param).then(res => {
  110. if(res.data.code == 0){
  111. records = res.data.data.list.reverse()
  112. var days=res.data.data.list,weeks=[]
  113. days=days.reverse()
  114. for(let i=0;i<52;i++){
  115. weeks.push({
  116. name:'第'+(i+1)+'周',
  117. data:[],
  118. total:0
  119. })
  120. }
  121. days.forEach(item=>{
  122. var w=this.getWeekNumber(item.stock_date)
  123. weeks[w-1].data.push(item)
  124. })
  125. weeks.forEach(item=>{
  126. if(item.data.length>0){
  127. var total=0
  128. var money = 10000.0
  129. item.data.forEach(i=>{
  130. var incom=Number(i.today_income.replace('%',''))
  131. money += money*incom/100.0
  132. i.date=(new Date(i.stock_date).getMonth()+1)+'/'+(new Date(i.stock_date).getDate())
  133. })
  134. total = (money-10000)*100.0/10000.0
  135. item.total=total.toFixed(2)
  136. }
  137. })
  138. this.setData({
  139. ec: {
  140. onInit: initChart
  141. },
  142. days:weeks.reverse()
  143. })
  144. }
  145. })
  146. },
  147. tabChange(e){
  148. this.setData({
  149. cur:e.target.dataset.id
  150. })
  151. this.setData({
  152. ec:null,
  153. days:[]
  154. })
  155. if(e.target.dataset.id == 0){
  156. this.loadAvg()
  157. } else{
  158. this.loadGroupAvg()
  159. }
  160. },
  161. /**
  162. * 生命周期函数--监听页面初次渲染完成
  163. */
  164. onReady() {
  165. },
  166. /**
  167. * 生命周期函数--监听页面显示
  168. */
  169. onShow() {
  170. },
  171. /**
  172. * 生命周期函数--监听页面隐藏
  173. */
  174. onHide() {
  175. },
  176. /**
  177. * 生命周期函数--监听页面卸载
  178. */
  179. onUnload() {
  180. },
  181. /**
  182. * 页面相关事件处理函数--监听用户下拉动作
  183. */
  184. onPullDownRefresh() {
  185. },
  186. /**
  187. * 页面上拉触底事件的处理函数
  188. */
  189. onReachBottom() {
  190. },
  191. /**
  192. * 用户点击右上角分享
  193. */
  194. onShareAppMessage() {
  195. }
  196. })
  197. /**折线图 */
  198. function initChart(canvas, width, height, dpr) {
  199. var xdata = [], ydata = [];
  200. records = records.reverse()
  201. for (let i = 0; i < records.length; i++) {
  202. let date = records[i].stock_date.split('-');
  203. xdata.push(date[1] + '/' + date[2])
  204. let y = records[i].total_income.replace('%', '')
  205. ydata.push(Number(y))
  206. }
  207. xdata = xdata.reverse()
  208. ydata = ydata.reverse()
  209. const chart = echarts.init(canvas, null, {
  210. width: width,
  211. height: height,
  212. devicePixelRatio: dpr // new
  213. });
  214. canvas.setChart(chart);
  215. var option = {
  216. legend: {
  217. show: false
  218. },
  219. grid: {
  220. x: 40,
  221. y: 10,
  222. x2: 10,
  223. y2: 35
  224. },
  225. tooltip: {
  226. show: true,
  227. trigger: 'axis',
  228. formatter: '{b0}: {c0}%',
  229. },
  230. xAxis: {
  231. type: 'category',
  232. data: xdata,
  233. axisLabel: {
  234. interval: 0,
  235. rotate: 40,
  236. color: '#999999',
  237. interval: 2
  238. }
  239. },
  240. yAxis: {
  241. axisLine: {
  242. show: true,
  243. },
  244. type: 'value',
  245. // name: '收益曲线',
  246. axisLabel: {
  247. color: '#999999',
  248. formatter: function (value, index) {//隐藏 0
  249. let texts = [];
  250. texts.push(value + '%')
  251. return texts;
  252. },
  253. show: true
  254. },
  255. },
  256. series: [{
  257. name: 'A',
  258. type: 'line',
  259. smooth: true,
  260. symbolSize: 4,
  261. lineStyle: {
  262. color: '#FF2D68'
  263. // color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  264. // offset: 0,
  265. // color: '#FF2D68'
  266. // }, {
  267. // offset: 1,
  268. // color: '#4C4BFF'
  269. // }]),
  270. },
  271. itemStyle: {
  272. borderWidth: 5,
  273. borderColor: '#FFAD52',
  274. color: '#FFAD52'
  275. },
  276. data: ydata
  277. }]
  278. };
  279. chart.setOption(option);
  280. return chart;
  281. }