// pages/moment/price/index.js import { getCategoryList, getCategoryPrice } from "../../../../api/moment" const util = require('../../../../utils/util') const storage = require('../../../../utils/storage') const event = require('../../../../utils/event') var wxCharts = require('../../../libs/wxcharts') const app = getApp() var lineChart = null const colors = ['#FF4229', '#FFAD2A', '#297BFF', '#29FF46', '#AD29FF'] Page({ /** * 页面的初始数据 */ data: { height: app.globalData.fragmentHeight, chartList: null, idList: '', loading: true, requesting: false, finished: false, orderList: [], form: { pageNum: 1 , pageSize:15} }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { event.on('EventMessage', this, this.onEvent) var cateIds = storage.get('cateIdList' + app.globalData.userInfo.userId) if(!util.isEmpty(cateIds)){ this.data.idList = cateIds this.fetchChartList() } else { wx.showLoading({ title: '加载中', mask: true }) getCategoryList().then(result => { this.data.idList = '' var size = result.data.records.length >= 5 ? 5 : result.data.records.length for (let index = 0; index < size; index++) { if(util.isEmpty(this.data.idList)){ this.data.idList = result.data.records[index].id } else { this.data.idList += ',' + result.data.records[index].id } } storage.put('cateIdList' + app.globalData.userInfo.userId, this.data.idList) this.fetchChartList() wx.hideLoading() }).catch(err => { //异常回调 wx.hideLoading() }) } }, onEvent: function (message) { if (message.what == 502) { var cateIds = storage.get('cateIdList' + app.globalData.userInfo.userId) if(!util.isEmpty(cateIds)){ this.data.idList = cateIds } this.fetchChartList() } }, //***********************************fetchChartList*****************************************// fetchChartList: function(){ getCategoryPrice({idList: this.data.idList}).then(result => { if(result.data.length <= 0){ return } var respList = result.data var categoriesList = [] var serieList = [] var min = Number.MAX_VALUE var max = 0 for (let index = 0; index < respList.length; index++) { const element = respList[index] var dataList = [] if(index == 0){ element.list.forEach(item => { categoriesList.push(item.data.substring(5)) // var price = math.times(item.unitPrice, 1000) var price = item.unitPrice if(min > price){ min = price } if(max < price){ max = price } dataList.push(price) }) } else { element.list.forEach(item => { var price = item.unitPrice if(min > price){ min = price } dataList.push(price) }) } var series = { name: element.name, data: dataList, format: function (val, name) { return val + '元/吨' }, color: colors[index] } serieList.push(series) } this.setData({ chartList: { categories: categoriesList, serieList: serieList } }) // if(min < 50){ // min = 50 // } this.initChart(min - 50, max + 50) }) }, touchHandler: function (e) { if(lineChart){ lineChart.scrollStart(e) } }, moveHandler: function (e) { if(lineChart){ lineChart.scroll(e) } }, touchEndHandler: function (e) { if(lineChart){ lineChart.scrollEnd(e) lineChart.showToolTip(e, { format: function (item, category) { return category + ' ' + item.name + ':' + item.data } }) } }, initChart: function (min, max) { lineChart = new wxCharts({ canvasId: 'lineChart', type: 'line', categories: this.data.chartList.categories, animation: false, title: { name: '纸品单价 (元/吨)' }, series: this.data.chartList.serieList, xAxis: { disableGrid: false }, yAxis: { // title: '纸品单价 (元/吨)', format: function (val) { return val }, min, max }, width: 350, height: 200, dataLabel: false, dataPointShape: 'circle', enableScroll: false, extra: { lineStyle: 'curve' } }, this) }, toPaperList: function(){ wx.navigateTo({ url: '/pages/moment/paper/index' }) }, onUnload: function(){ event.remove('EventMessage', this) } })