index.js 5.4 KB

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