import * as echarts from '../../ec-canvas/echarts'; const app = getApp(); var host = app.globalData.host; Page({ data: { ec: { // onInit: initChart lazyLoad: true }, timer: '', datas:{}, userInfo:{} }, onReady: function () { //这一步是一定要注意的 }, onLoad(){ wx.showNavigationBarLoading() /**获取token */ wx.getStorage({ key: 'userInfo', success: res => { if (res.data){ this.setData({ userInfo: res.data }) this.getData(); }else{ wx.switchTab({ url: '../user/user', }) } }, fail:error=>{ //跳转到登陆页面 wx.switchTab({ url: '../user/user', }) } }) }, onShow(){ if (!this.data.userInfo.token) { this.onLoad() }else{ console.log(233) wx.showNavigationBarLoading() this.getData() } }, getData(){ this.oneComponent = this.selectComponent('#mychart'); wx.request({ url: host + '/api/wx/index', header: { 'Authorization': this.data.userInfo.token }, success: res=> { console.log(res) this.setData({ datas: res.data.data }) wx.hideNavigationBarLoading() var records = res.data.data.records, xdata = [], ydata = []; for (let i = 0; i < records.length; i++) { let date = records[i].stock_date.split('-'); xdata.push(date[1] + '/' + date[2]) ydata.push(records[i].today_fund) } xdata = xdata.reverse() ydata = ydata.reverse() this.init_one(xdata, ydata) }, fail: error => { //跳转到登陆页面 wx.switchTab({ url: '../user/user', }) } }) }, init_one: function (xdata, ydata) { //初始化第一个图表 this.oneComponent.init((canvas, width, height) => { const chart = echarts.init(canvas, null, { width: width, height: height }); setOption(chart, xdata, ydata) this.chart = chart; return chart; }); }, }); /**折线图 */ function initChart(canvas, width, height, dpr) { wx.getStorage({ key: 'userInfo', success: res => { var info = res.data wx.request({ url: host + '/api/wx/index', header: { 'Authorization': info.token }, success: res => { var records = res.data.data.records, xdata=[],ydata=[]; for (let i = 0; i < records.length; i++) { let date = records[i].stock_date.split('-'); xdata.push(date[1] + '/' + date[2]) ydata.push(records[i].today_fund) } xdata = xdata.reverse() ydata=ydata.reverse() const chart = echarts.init(canvas, null, { width: width, height: height, devicePixelRatio: dpr // new }); canvas.setChart(chart); var option = { legend: { show: false }, grid: { x: 35, y: 40, x2: 10, y2: 35 }, tooltip: { show: true, trigger: 'axis' }, xAxis: { type: 'category', data: xdata, axisLabel: { interval: 0, rotate: 40, color: '#999999' } }, yAxis: { axisLine: { show: true }, type: 'value', name: '收益曲线', }, series: [{ name: 'A', type: 'line', smooth: true, symbolSize: 8, lineStyle: { color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{ offset: 0, color: '#FF2D68' }, { offset: 1, color: '#4C4BFF' }]), }, itemStyle: { borderWidth: 5, borderColor: '#FFAD52', color: '#FFAD52' }, data: ydata }] }; chart.setOption(option); return chart; } }) }, }) } function setOption(chart, xdata, ydata) { var option = { legend: { show: false }, grid: { x: 35, y: 40, x2: 10, y2: 35 }, tooltip: { show: true, trigger: 'axis' }, xAxis: { type: 'category', data: xdata, axisLabel: { interval: 0, rotate: 40, color: '#999999' } }, yAxis: { axisLine: { show: true }, type: 'value', name: '收益曲线', }, series: [{ name: 'A', type: 'line', smooth: true, symbolSize: 8, lineStyle: { color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{ offset: 0, color: '#FF2D68' }, { offset: 1, color: '#4C4BFF' }]), }, itemStyle: { borderWidth: 5, borderColor: '#FFAD52', color: '#FFAD52' }, data: ydata }] }; chart.setOption(option) }