avg.js 6.2 KB

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