index.js 4.1 KB

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