index.js 5.3 KB

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