index.js 3.5 KB

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