微信小程序--echarts实现双y轴

const App = getApp();
import * as echarts from '../../ec-canvas/echarts'; //引入echarts.js
var barec = null

Page({
   
  onShareAppMessage: function (res) {
   
    return {
   
      title: 'ECharts 可以在微信小程序中使用啦!',
      path: '/pages/index/index',
      success: function () {
    },
      fail: function () {
    }
    }
  },
  data: {
   
    ec: {
   
      onInit: function (canvas, width, height) {
   
        barec = echarts.init(canvas, null, {
   
          width: width,
          height: height
        });

        canvas.setChart(barec);
        return barec;
      }
    },
  },

  onReady() {
   
  },

  onLoad: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用ECharts柱状图的详细步骤: 1. 首先,在微信小程序项目中引入ECharts库。可以通过npm进行安装,也可以手动下载并放置在项目目录中。同时,需要在wxml文件中引入ECharts的canvas组件: ```html <!-- index.wxml --> <view class="container"> <ec-canvas id="mychart"></ec-canvas> </view> ``` 2. 在js文件中引入ECharts并获取canvas对象: ```javascript // index.js import * as echarts from '../../ec-canvas/echarts'; Page({ onReady: function () { // 获取组件 this.ecComponent = this.selectComponent('#mychart'); // 初始化图表 this.initChart(); }, // 初始化图表 initChart: function () { this.ecComponent.init((canvas, width, height) => { // 初始化echarts const chart = echarts.init(canvas, null, { width: width, height: height }); // 设置图表配置项 const option = { // ... }; // 使用刚指定的配置项和数据显示图表 chart.setOption(option); // 将图表实例绑定到this上,方便其他方法调用 this.chart = chart; return chart; }); } }) ``` 3. 在option中设置柱状图的相关属性: ```javascript // ... const option = { // 柱状图的标题 title: { text: '柱状图示例' }, // 柱状图的x轴 xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, // 柱状图的y轴 yAxis: { type: 'value' }, // 柱状图的数据 series: [{ data: [120, 200, 150, 80, 70, 110, 130], type: 'bar' }] }; // ... ``` 4. 最后,通过调用chart.setOption方法将图表渲染到canvas中: ```javascript // ... // 使用刚指定的配置项和数据显示图表 chart.setOption(option); // ... ``` 这样,我们就成功地在微信小程序中使用了ECharts柱状图。 以下是一个完整的demo,其中包含了详细的注释: ```html <!-- index.wxml --> <view class="container"> <ec-canvas id="mychart"></ec-canvas> </view> ``` ```javascript // index.js // 引入ECharts库 import * as echarts from '../../ec-canvas/echarts'; Page({ onReady: function () { // 获取组件 this.ecComponent = this.selectComponent('#mychart'); // 初始化图表 this.initChart(); }, // 初始化图表 initChart: function () { this.ecComponent.init((canvas, width, height) => { // 初始化echarts const chart = echarts.init(canvas, null, { width: width, height: height }); // 设置图表配置项 const option = { // 柱状图的标题 title: { text: '柱状图示例' }, // 柱状图的x轴 xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, // 柱状图的y轴 yAxis: { type: 'value' }, // 柱状图的数据 series: [{ data: [120, 200, 150, 80, 70, 110, 130], type: 'bar' }] }; // 使用刚指定的配置项和数据显示图表 chart.setOption(option); // 将图表实例绑定到this上,方便其他方法调用 this.chart = chart; return chart; }); } }) ``` 希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值