微信小程序中渲染Echars图

微信小程序可以使用 echarts-for-weixin 库来实现 echarts 表的渲染。对于折柱混合横向渲染的需求,可以在 echarts 的 option 设置 xAxis 和 yAxis 的 type 为 'value',同时设置 grid 的 containLabel 为 true,让表可以占满整个 canvas 区域。 具体的实现步骤如下: 1. 安装 echarts-for-weixin 库 ```bash npm install echarts-for-weixin --save ``` 2. 在页面的 JSON 配置添加 canvas 组件 ```json { "usingComponents": { "ec-canvas": "echarts-for-weixin/ec-canvas" } } ``` 3. 在页面的 WXML 添加 canvas 组件 ```html <view class="chart-container"> <ec-canvas id="mychart" canvas-id="mychart-canvas" ec="{{ ec }}" /> </view> ``` 4. 在页面的 JS 引入 echarts 库和 echarts-for-weixin 库 ```javascript import * as echarts from 'echarts'; import * as echartsForWeixin from 'echarts-for-weixin'; ``` 5. 在页面的 JS 设置 option,并将其传递给 echarts-for-weixin ```javascript Page({ data: { ec: { onInit: initChart } } }); function initChart(canvas, width, height) { const chart = echartsForWeixin.init(canvas, null, { width, height }); const option = { xAxis: { type: 'value', position: 'top' }, yAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, grid: { containLabel: true }, series: [ { name: 'bar', type: 'bar', stack: '总量', label: { show: true }, data: [320, 302, 301, 334, 390, 330, 320] }, { name: 'line', type: 'line', stack: '总量', label: { show: true, position: 'right' }, data: [220, 182, 191, 234, 290, 330, 310] } ] }; chart.setOption(option); return chart; } ``` 在这个例子,我们将 xAxis 的 type 设置为 'value',并将 yAxis 的 type 设置为 'category'。同时,我们将 xAxis 的 position 设置为 'top',让它位于表上方。为了让表能够横向渲染,我们将 grid 的 containLabel 设置为 true,这样就可以让表占满整个 canvas 区域了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值