index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. console.log(ydata)
  124. const chart = echarts.init(canvas, null, {
  125. width: width,
  126. height: height,
  127. devicePixelRatio: dpr // new
  128. });
  129. canvas.setChart(chart);
  130. var option = {
  131. legend: {
  132. show: false
  133. },
  134. grid: {
  135. x: 35,
  136. y: 40,
  137. x2: 10,
  138. y2: 35
  139. },
  140. tooltip: {
  141. show: true,
  142. trigger: 'axis'
  143. },
  144. xAxis: {
  145. type: 'category',
  146. data: xdata,
  147. axisLabel: {
  148. interval: 0,
  149. rotate: 40,
  150. color: '#999999'
  151. }
  152. },
  153. yAxis: {
  154. axisLine: {
  155. show: true
  156. },
  157. type: 'value',
  158. name: '收益曲线',
  159. },
  160. series: [{
  161. name: 'A',
  162. type: 'line',
  163. smooth: true,
  164. symbolSize: 8,
  165. lineStyle: {
  166. color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  167. offset: 0,
  168. color: '#FF2D68'
  169. }, {
  170. offset: 1,
  171. color: '#4C4BFF'
  172. }]),
  173. },
  174. itemStyle: {
  175. borderWidth: 5,
  176. borderColor: '#FFAD52',
  177. color: '#FFAD52'
  178. },
  179. data: ydata
  180. }]
  181. };
  182. chart.setOption(option);
  183. return chart;
  184. }
  185. })
  186. },
  187. })
  188. }
  189. function setOption(chart, xdata, ydata) {
  190. var option = {
  191. legend: {
  192. show: false
  193. },
  194. grid: {
  195. x: 50,
  196. y: 40,
  197. x2: 10,
  198. y2: 35
  199. },
  200. tooltip: {
  201. show: true,
  202. trigger: 'axis',
  203. formatter: '{b0}: {c0}%'
  204. },
  205. xAxis: {
  206. type: 'category',
  207. data: xdata,
  208. axisLabel: {
  209. interval: 0,
  210. rotate: 40,
  211. color: '#999999'
  212. }
  213. },
  214. yAxis: {
  215. axisLine: {
  216. show: true
  217. },
  218. type: 'value',
  219. name: '收益曲线',
  220. axisLabel: {
  221. formatter: function (value, index) {//隐藏 0
  222. let texts = [];
  223. texts.push(value+'%')
  224. return texts;
  225. },
  226. show: true
  227. },
  228. },
  229. series: [{
  230. name: 'A',
  231. type: 'line',
  232. smooth: true,
  233. symbolSize: 8,
  234. lineStyle: {
  235. color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  236. offset: 0,
  237. color: '#FF2D68'
  238. }, {
  239. offset: 1,
  240. color: '#4C4BFF'
  241. }]),
  242. },
  243. itemStyle: {
  244. borderWidth: 5,
  245. borderColor: '#FFAD52',
  246. color: '#FFAD52'
  247. },
  248. data: ydata
  249. }]
  250. };
  251. chart.setOption(option)
  252. }