index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import * as echarts from '../../ec-canvas/echarts';
  2. const app = getApp();
  3. var host = app.globalData.host;
  4. Page({
  5. data: {
  6. ec: {
  7. // onInit: initChart
  8. lazyLoad: true
  9. },
  10. timer: '',
  11. datas:{},
  12. userInfo:{}
  13. },
  14. onReady: function () { //这一步是一定要注意的
  15. },
  16. onLoad(){
  17. wx.showNavigationBarLoading()
  18. /**获取token */
  19. wx.getStorage({
  20. key: 'userInfo',
  21. success: res => {
  22. if (res.data){
  23. this.setData({
  24. userInfo: res.data
  25. })
  26. this.getData();
  27. }else{
  28. wx.switchTab({
  29. url: '../user/user',
  30. })
  31. }
  32. },
  33. fail:error=>{
  34. //跳转到登陆页面
  35. wx.switchTab({
  36. url: '../user/user',
  37. })
  38. }
  39. })
  40. },
  41. onShow(){
  42. if (!this.data.userInfo.token) {
  43. this.onLoad()
  44. }else{
  45. wx.showNavigationBarLoading()
  46. this.getData()
  47. }
  48. },
  49. getData(){
  50. wx.request({
  51. url: host + '/api/wx/index',
  52. header: {
  53. 'Authorization': this.data.userInfo.token
  54. },
  55. success: res=> {
  56. console.log(res)
  57. wx.hideNavigationBarLoading()
  58. if(res.data.code != 0){
  59. wx.showToast({
  60. icon: 'none',
  61. title: res.data.message,
  62. })
  63. return
  64. }
  65. this.setData({
  66. datas: res.data.data
  67. })
  68. if (res.data.data.records.length<=0){
  69. return;
  70. }
  71. var records = res.data.data.records, xdata = [], ydata = [];
  72. for (let i = 0; i < records.length; i++) {
  73. let date = records[i].stock_date.split('-');
  74. xdata.push(date[1] + '/' + date[2])
  75. let y = records[i].total_income.replace('%','')
  76. ydata.push(Number(y))
  77. }
  78. xdata = xdata.reverse()
  79. ydata = ydata.reverse()
  80. this.oneComponent = this.selectComponent('#mychart');
  81. this.init_one(xdata, ydata)
  82. },
  83. fail: error => {
  84. //跳转到登陆页面
  85. wx.switchTab({
  86. url: '../user/user',
  87. })
  88. }
  89. })
  90. },
  91. init_one: function (xdata, ydata) { //初始化第一个图表
  92. this.oneComponent.init((canvas, width, height, dpr) => {
  93. const chart = echarts.init(canvas, null, {
  94. width: width,
  95. height: height,
  96. devicePixelRatio:dpr
  97. });
  98. setOption(chart, xdata, ydata)
  99. this.chart = chart;
  100. return chart;
  101. });
  102. },
  103. });
  104. function setOption(chart, xdata, ydata) {
  105. console.log(ydata)
  106. var option = {
  107. legend: {
  108. show: false
  109. },
  110. grid: {
  111. x: 50,
  112. y: 40,
  113. x2: 10,
  114. y2: 35
  115. },
  116. tooltip: {
  117. show: true,
  118. trigger: 'axis',
  119. formatter: '{b0}: {c0}%'
  120. },
  121. xAxis: {
  122. type: 'category',
  123. data: xdata,
  124. axisLabel: {
  125. interval: 0,
  126. rotate: 40,
  127. color: '#999999'
  128. }
  129. },
  130. yAxis: {
  131. axisLine: {
  132. show: true
  133. },
  134. type: 'value',
  135. name: '收益曲线',
  136. axisLabel: {
  137. formatter: function (value, index) {//隐藏 0
  138. let texts = [];
  139. texts.push(value+'%')
  140. return texts;
  141. },
  142. show: true
  143. },
  144. },
  145. series: [{
  146. name: 'A',
  147. type: 'line',
  148. smooth: true,
  149. symbolSize: 8,
  150. lineStyle: {
  151. color:'#FF2D68'
  152. // color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  153. // offset: 0,
  154. // color: '#FF2D68'
  155. // }, {
  156. // offset: 1,
  157. // color: '#4C4BFF'
  158. // }]),
  159. },
  160. itemStyle: {
  161. borderWidth: 5,
  162. borderColor: '#FFAD52',
  163. color: '#FFAD52'
  164. },
  165. data: ydata
  166. }]
  167. };
  168. chart.setOption(option)
  169. }