index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. console.log(233)
  46. wx.showNavigationBarLoading()
  47. this.getData()
  48. }
  49. },
  50. getData(){
  51. this.oneComponent = this.selectComponent('#mychart');
  52. wx.request({
  53. url: host + '/api/wx/index',
  54. header: {
  55. 'Authorization': this.data.userInfo.token
  56. },
  57. success: res=> {
  58. console.log(res)
  59. this.setData({
  60. datas: res.data.data
  61. })
  62. wx.hideNavigationBarLoading()
  63. var records = res.data.data.records, xdata = [], ydata = [];
  64. for (let i = 0; i < records.length; i++) {
  65. let date = records[i].stock_date.split('-');
  66. xdata.push(date[1] + '/' + date[2])
  67. ydata.push(records[i].today_fund)
  68. }
  69. xdata = xdata.reverse()
  70. ydata = ydata.reverse()
  71. this.init_one(xdata, ydata)
  72. },
  73. fail: error => {
  74. //跳转到登陆页面
  75. wx.switchTab({
  76. url: '../user/user',
  77. })
  78. }
  79. })
  80. },
  81. init_one: function (xdata, ydata) { //初始化第一个图表
  82. this.oneComponent.init((canvas, width, height) => {
  83. const chart = echarts.init(canvas, null, {
  84. width: width,
  85. height: height
  86. });
  87. setOption(chart, xdata, ydata)
  88. this.chart = chart;
  89. return chart;
  90. });
  91. },
  92. });
  93. /**折线图 */
  94. function initChart(canvas, width, height, dpr) {
  95. wx.getStorage({
  96. key: 'userInfo',
  97. success: res => {
  98. var info = res.data
  99. wx.request({
  100. url: host + '/api/wx/index',
  101. header: {
  102. 'Authorization': info.token
  103. },
  104. success: res => {
  105. var records = res.data.data.records, xdata=[],ydata=[];
  106. for (let i = 0; i < records.length; i++) {
  107. let date = records[i].stock_date.split('-');
  108. xdata.push(date[1] + '/' + date[2])
  109. ydata.push(records[i].today_fund)
  110. }
  111. xdata = xdata.reverse()
  112. ydata=ydata.reverse()
  113. const chart = echarts.init(canvas, null, {
  114. width: width,
  115. height: height,
  116. devicePixelRatio: dpr // new
  117. });
  118. canvas.setChart(chart);
  119. var option = {
  120. legend: {
  121. show: false
  122. },
  123. grid: {
  124. x: 35,
  125. y: 40,
  126. x2: 10,
  127. y2: 35
  128. },
  129. tooltip: {
  130. show: true,
  131. trigger: 'axis'
  132. },
  133. xAxis: {
  134. type: 'category',
  135. data: xdata,
  136. axisLabel: {
  137. interval: 0,
  138. rotate: 40,
  139. color: '#999999'
  140. }
  141. },
  142. yAxis: {
  143. axisLine: {
  144. show: true
  145. },
  146. type: 'value',
  147. name: '收益曲线',
  148. },
  149. series: [{
  150. name: 'A',
  151. type: 'line',
  152. smooth: true,
  153. symbolSize: 8,
  154. lineStyle: {
  155. color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  156. offset: 0,
  157. color: '#FF2D68'
  158. }, {
  159. offset: 1,
  160. color: '#4C4BFF'
  161. }]),
  162. },
  163. itemStyle: {
  164. borderWidth: 5,
  165. borderColor: '#FFAD52',
  166. color: '#FFAD52'
  167. },
  168. data: ydata
  169. }]
  170. };
  171. chart.setOption(option);
  172. return chart;
  173. }
  174. })
  175. },
  176. })
  177. }
  178. function setOption(chart, xdata, ydata) {
  179. var option = {
  180. legend: {
  181. show: false
  182. },
  183. grid: {
  184. x: 35,
  185. y: 40,
  186. x2: 10,
  187. y2: 35
  188. },
  189. tooltip: {
  190. show: true,
  191. trigger: 'axis'
  192. },
  193. xAxis: {
  194. type: 'category',
  195. data: xdata,
  196. axisLabel: {
  197. interval: 0,
  198. rotate: 40,
  199. color: '#999999'
  200. }
  201. },
  202. yAxis: {
  203. axisLine: {
  204. show: true
  205. },
  206. type: 'value',
  207. name: '收益曲线',
  208. },
  209. series: [{
  210. name: 'A',
  211. type: 'line',
  212. smooth: true,
  213. symbolSize: 8,
  214. lineStyle: {
  215. color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  216. offset: 0,
  217. color: '#FF2D68'
  218. }, {
  219. offset: 1,
  220. color: '#4C4BFF'
  221. }]),
  222. },
  223. itemStyle: {
  224. borderWidth: 5,
  225. borderColor: '#FFAD52',
  226. color: '#FFAD52'
  227. },
  228. data: ydata
  229. }]
  230. };
  231. chart.setOption(option)
  232. }