index.js 5.6 KB

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