index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. },
  9. datas:{},
  10. userInfo:{}
  11. },
  12. onLoad(){
  13. wx.showNavigationBarLoading()
  14. /**获取token */
  15. wx.getStorage({
  16. key: 'userInfo',
  17. success: res => {
  18. if (res.data){
  19. this.setData({
  20. userInfo: res.data
  21. })
  22. this.getData();
  23. }else{
  24. wx.switchTab({
  25. url: '../user/user',
  26. })
  27. }
  28. },
  29. fail:error=>{
  30. //跳转到登陆页面
  31. wx.switchTab({
  32. url: '../user/user',
  33. })
  34. }
  35. })
  36. },
  37. onShow(){
  38. if (!this.data.userInfo.token) {
  39. this.onLoad()
  40. }else{
  41. console.log(233)
  42. wx.showNavigationBarLoading()
  43. this.getData()
  44. }
  45. },
  46. getData(){
  47. wx.request({
  48. url: host + '/api/wx/index',
  49. header: {
  50. 'Authorization': this.data.userInfo.token
  51. },
  52. success: res=> {
  53. console.log(res)
  54. this.setData({
  55. datas: res.data.data
  56. })
  57. wx.hideNavigationBarLoading()
  58. },
  59. fail: error => {
  60. //跳转到登陆页面
  61. wx.switchTab({
  62. url: '../user/user',
  63. })
  64. }
  65. })
  66. },
  67. onReady() {
  68. },
  69. });
  70. /**折线图 */
  71. function initChart(canvas, width, height, dpr) {
  72. wx.getStorage({
  73. key: 'userInfo',
  74. success: res => {
  75. var info = res.data
  76. wx.request({
  77. url: host + '/api/wx/index',
  78. header: {
  79. 'Authorization': info.token
  80. },
  81. success: res => {
  82. var records = res.data.data.records, xdata=[],ydata=[];
  83. for (let i = 0; i < records.length; i++) {
  84. let date = records[i].stock_date.split('-');
  85. xdata.push(date[1] + '/' + date[2])
  86. ydata.push(records[i].today_fund)
  87. }
  88. xdata = xdata.reverse()
  89. ydata=ydata.reverse()
  90. const chart = echarts.init(canvas, null, {
  91. width: width,
  92. height: height,
  93. devicePixelRatio: dpr // new
  94. });
  95. canvas.setChart(chart);
  96. var option = {
  97. legend: {
  98. show: false
  99. },
  100. grid: {
  101. x: 35,
  102. y: 40,
  103. x2: 10,
  104. y2: 35
  105. },
  106. tooltip: {
  107. show: true,
  108. trigger: 'axis'
  109. },
  110. xAxis: {
  111. type: 'category',
  112. data: xdata,
  113. axisLabel: {
  114. interval: 0,
  115. rotate: 40,
  116. color: '#999999'
  117. }
  118. },
  119. yAxis: {
  120. axisLine: {
  121. show: true
  122. },
  123. type: 'value',
  124. name: '收益曲线',
  125. },
  126. series: [{
  127. name: 'A',
  128. type: 'line',
  129. smooth: true,
  130. symbolSize: 8,
  131. lineStyle: {
  132. color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  133. offset: 0,
  134. color: '#FF2D68'
  135. }, {
  136. offset: 1,
  137. color: '#4C4BFF'
  138. }]),
  139. },
  140. itemStyle: {
  141. borderWidth: 5,
  142. borderColor: '#FFAD52',
  143. color: '#FFAD52'
  144. },
  145. data: ydata
  146. }]
  147. };
  148. chart.setOption(option);
  149. return chart;
  150. }
  151. })
  152. },
  153. })
  154. }