const app = getApp() import * as echarts from '../../../commpents/ec-canvas/echarts' let chart; Page({ data: { frameStyle: { useTop: true, name: '学员统计', leftArrow: true, useBar: false }, piedata: [] }, // 返回 back(e) { wx.navigateBack({ delta: 1 }) }, /** * 生命周期函数--监听页面加载 */ onLoad: async function (options) { const that = this; that.setData({ ec: { onInit: that.initChart } }) // 监听用户是否登录 await that.watchLogin(); }, // 监听用户是否登录 watchLogin: async function () { const that = this; wx.getStorage({ key: 'user', success: async res => { const arr = await app.$get(`/statistics/schoolStudentAge`, { school_id: res.data.info.id }); if (arr.errcode == '0') { that.setData({ piedata: arr.data }) const option = { series: [{ data: arr.data }], } if (chart) chart.setOption(option) } }, fail: async res => { wx.redirectTo({ url: '/pages/index/index' }) } }) }, // 饼图 initChart(canvas, width, height, dpr) { let that = this; var piedata = that.data.piedata; chart = echarts.init(canvas, null, { width: width, height: height, devicePixelRatio: dpr }); canvas.setChart(chart); var option = { backgroundColor: "#ffffff", series: [{ label: { normal: { show: true, formatter: '{b}:{c}人' + '\n\r' + '({d}%)' }, }, type: 'pie', center: ['50%', '50%'], radius: ['20%', '40%'], data: piedata }], legend: { orient: 'vertical', height: 40, padding: 50, }, tooltip: { trigger: 'item', formatter: '{b}:{c}人' + '\n\r' + '({d}%)' // 格式化数值百分比输出 }, }; chart.setOption(option, true); return chart; }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 页面上拉触底事件的处理函数 */ /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function (res) { }, })