index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. const chart = echarts.init(canvas, null, {
  89. width: width,
  90. height: height,
  91. devicePixelRatio: dpr // new
  92. });
  93. canvas.setChart(chart);
  94. var option = {
  95. legend: {
  96. show: false
  97. },
  98. grid: {
  99. x: 35,
  100. y: 40,
  101. x2: 10,
  102. y2: 35
  103. },
  104. tooltip: {
  105. show: true,
  106. trigger: 'axis'
  107. },
  108. xAxis: {
  109. type: 'category',
  110. data: xdata,
  111. axisLabel: {
  112. interval: 0,
  113. rotate: 40,
  114. color: '#999999'
  115. }
  116. },
  117. yAxis: {
  118. axisLine: {
  119. show: true
  120. },
  121. type: 'value',
  122. name: '收益曲线',
  123. },
  124. series: [{
  125. name: 'A',
  126. type: 'line',
  127. smooth: true,
  128. symbolSize: 8,
  129. lineStyle: {
  130. color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  131. offset: 0,
  132. color: '#FF2D68'
  133. }, {
  134. offset: 1,
  135. color: '#4C4BFF'
  136. }]),
  137. },
  138. itemStyle: {
  139. borderWidth: 5,
  140. borderColor: '#FFAD52',
  141. color: '#FFAD52'
  142. },
  143. data: ydata
  144. }]
  145. };
  146. chart.setOption(option);
  147. return chart;
  148. }
  149. })
  150. },
  151. })
  152. }