赞
踩
为了避免echarts过大,我只选取了部分图表,此过程较为繁琐,直接放我自己打包好的,或者自行搜索"微信小程序引入echarts"
链接:https://pan.quark.cn/s/9831bda4af95
提取码:FbqR
import * as echarts from "../../component/echarts/echarts"
<van-tabs bind:click="onTabClick"> <van-tab title="日"> <van-u-calendar cal-type="day" bind:click="getDayDate" /> </van-tab> <van-tab title="周"> <van-u-calendar cal-type="week" bindtap="getWeekDate" data-type="2" /> </van-tab> <van-tab title="月"> <van-u-calendar cal-type="month" bind:click="getMonthDate" /> </van-tab> </van-tabs> <view class="ec-container"> <ec-canvas wx:if="{{chartsIndex == 1}}" canvas-id="echart-pie" ec="{{ec}}" style="width: 750rpx; height: 378rpx; display: block; box-sizing: border-box"></ec-canvas> <ec-canvas wx:if="{{chartsIndex == 2}}" canvas-id="echart-pie" ec="{{ec}}" style="width: 750rpx; height: 378rpx; display: block; box-sizing: border-box"></ec-canvas> <ec-canvas wx:if="{{chartsIndex == 3}}" canvas-id="echart-pie" ec="{{ec}}" style="width: 750rpx; height: 378rpx; display: block; box-sizing: border-box"></ec-canvas> </view>
三个wx:if是为了点击事件触发后重新渲染饼图
.ec-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100vw;
height: 30vh;
}
ec-canvas {
width: 100%;
height: 100%;
}
var app = getApp(); const $api = require('../../utils/request').API; import * as echarts from "../../component/echarts/echarts" Page({ /** * 页面的初始数据 */ data: { ec: { onInit: initChart }, active: 0, chartsIndex: 1, emptyList:[{value:0,name:"暂无数据"}], }, // 获取数据 getDietaryEcharts(type){ const userInfo = wx.getStorageSync('userInfo') const data = { userId: userInfo.id, type: this.data.dietaryType } if(type != '' && type != undefined && type != null){ data.type = type } $api.getDietartEcharts(data).then(res=>{ if (res.data.code === 10000) { console.log("echarts: " + JSON.stringify(res.data.data)); if (res.data.data != null && res.data.data != '' && res.data.data != undefined && res.data.data.length > 0 ) { const result = res.data.data.reduce((acc, cur) => { const types = cur.split(','); for (let i = 0; i < types.length; i++) { const type = types[i]; if (acc[type]) { acc[type]++; } else { acc[type] = 1; } } return acc; }, {}); console.log(result); let arr = [] for (const key in result) { if (Object.hasOwnProperty.call(result, key)) { arr.push({ name: key, value: result[key] }) } } app.globalData.dietaryChartData = arr; }else{ app.globalData.dietaryChartData = this.data.emptyList } } }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.getDietaryEcharts() }, }) // 初始化echarts function initChart(canvas, width, height, dpr) { const chart = echarts.init(canvas, null, { width: width, height: height, devicePixelRatio: dpr }); canvas.setChart(chart); var option = { backgroundColor: 'rgba(255,255,255,0.8)', tooltip: { trigger: 'item' }, legend: {//显示图例 show: true, top: '5%', left: 'center' }, series: [{ label: { normal: { fontSize: 14 } }, type: 'pie', center: ['50%', '60%'],//位置 radius: ['20%', '30%'],//圈大小 data: app.globalData.dietaryChartData }] }; chart.setOption(option); return chart; }
因为渲染图表的方法initChart在Page外面,我无法获取page内的data,所以走了个歪门
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。