avg.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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
  114. for(let i=0;i<55;i++){
  115. weeks.push({
  116. name:'',
  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=weeks.filter(item=>item.data.length>0)
  126. var w_text=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五']
  127. weeks.forEach((item,index)=>{
  128. if(item.data.length>0){
  129. item.name='第'+w_text[index]+'周'
  130. var total=0
  131. var money = 10000.0
  132. item.data.forEach(i=>{
  133. var incom=Number(i.today_income.replace('%',''))
  134. money += money*incom/100.0
  135. i.date=(new Date(i.stock_date).getMonth()+1)+'/'+(new Date(i.stock_date).getDate())
  136. })
  137. total = (money-10000)*100.0/10000.0
  138. item.total=total.toFixed(2)
  139. }
  140. })
  141. this.setData({
  142. ec: {
  143. onInit: initChart
  144. },
  145. days:weeks.reverse()
  146. })
  147. }
  148. })
  149. },
  150. tabChange(e){
  151. this.setData({
  152. cur:e.target.dataset.id
  153. })
  154. this.setData({
  155. ec:null,
  156. days:[]
  157. })
  158. if(e.target.dataset.id == 0){
  159. this.loadAvg()
  160. } else{
  161. this.loadGroupAvg()
  162. }
  163. },
  164. /**
  165. * 生命周期函数--监听页面初次渲染完成
  166. */
  167. onReady() {
  168. },
  169. /**
  170. * 生命周期函数--监听页面显示
  171. */
  172. onShow() {
  173. },
  174. /**
  175. * 生命周期函数--监听页面隐藏
  176. */
  177. onHide() {
  178. },
  179. /**
  180. * 生命周期函数--监听页面卸载
  181. */
  182. onUnload() {
  183. },
  184. /**
  185. * 页面相关事件处理函数--监听用户下拉动作
  186. */
  187. onPullDownRefresh() {
  188. },
  189. /**
  190. * 页面上拉触底事件的处理函数
  191. */
  192. onReachBottom() {
  193. },
  194. /**
  195. * 用户点击右上角分享
  196. */
  197. onShareAppMessage() {
  198. }
  199. })
  200. /**折线图 */
  201. function initChart(canvas, width, height, dpr) {
  202. var xdata = [], ydata = [];
  203. records = records.reverse()
  204. for (let i = 0; i < records.length; i++) {
  205. let date = records[i].stock_date.split('-');
  206. xdata.push(date[1] + '/' + date[2])
  207. let y = records[i].total_income.replace('%', '')
  208. ydata.push(Number(y))
  209. }
  210. xdata = xdata.reverse()
  211. ydata = ydata.reverse()
  212. const chart = echarts.init(canvas, null, {
  213. width: width,
  214. height: height,
  215. devicePixelRatio: dpr // new
  216. });
  217. canvas.setChart(chart);
  218. var option = {
  219. legend: {
  220. show: false
  221. },
  222. grid: {
  223. x: 40,
  224. y: 10,
  225. x2: 10,
  226. y2: 35
  227. },
  228. tooltip: {
  229. show: true,
  230. trigger: 'axis',
  231. formatter: '{b0}: {c0}%',
  232. },
  233. xAxis: {
  234. type: 'category',
  235. data: xdata,
  236. axisLabel: {
  237. interval: 0,
  238. rotate: 40,
  239. color: '#999999',
  240. interval: 2
  241. }
  242. },
  243. yAxis: {
  244. axisLine: {
  245. show: true,
  246. },
  247. type: 'value',
  248. // name: '收益曲线',
  249. axisLabel: {
  250. color: '#999999',
  251. formatter: function (value, index) {//隐藏 0
  252. let texts = [];
  253. texts.push(value + '%')
  254. return texts;
  255. },
  256. show: true
  257. },
  258. },
  259. series: [{
  260. name: 'A',
  261. type: 'line',
  262. smooth: true,
  263. symbolSize: 4,
  264. lineStyle: {
  265. color: '#FF2D68'
  266. // color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  267. // offset: 0,
  268. // color: '#FF2D68'
  269. // }, {
  270. // offset: 1,
  271. // color: '#4C4BFF'
  272. // }]),
  273. },
  274. itemStyle: {
  275. borderWidth: 5,
  276. borderColor: '#FFAD52',
  277. color: '#FFAD52'
  278. },
  279. data: ydata
  280. }]
  281. };
  282. chart.setOption(option);
  283. return chart;
  284. }