小程序使用echarts图表

本文介绍了如何在微信小程序中集成ECharts-for-Weixin项目,通过步骤一到四详细阐述了下载、引入组件、创建页面及配置JSON文件的过程。在WXML中使用组件,并展示了初始化图表的JavaScript代码,包括数据设置、样式配置等,以创建一个条形图。此教程适用于希望在微信小程序中实现数据可视化的开发者。

首先,下载 GitHub 上的 ecomfe/echarts-for-weixin 项目。
仅拷贝 ec-canvas 目录到新建的项目下,然后做相应的调整。
在这里插入图片描述

创建一个页面文件,.json文件引入组件
在这里插入图片描述
.wxml 中使用 组件。注意路径的相对位置要写对。
在这里插入图片描述
其中 ec 是一个我们在 index.js 中定义的对象,它使得图表能够在页面加载后被初始化并设置

import * as echarts from '../../../components/echarts-for-weixin-master/ec-canvas/echarts';
let chart = null;
// 进行初始化数据
function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  var option = {
    color: ['#37a2da'],
    xAxis: [
      {
        type: 'category',
        data: [{
          value: '地砖/墙砖',
          textStyle: {
            fontSize: 12,
            color: '#333333'
          },
        },{
          value: '木工',
          textStyle: {
            fontSize: 12,
            color: '#333333'
          }
        },{
          value: '管道疏通',
          textStyle: {
            fontSize: 12,
            color: '#333333'
          }
        },{
          value: '门锁',
          textStyle: {
            fontSize: 12,
            color: '#333333'
          }
        },{
          value: '其他',
          textStyle: {
            fontSize: 12,
            color: '#333333'
          }
        },],
        axisLine: {
          show:true,
          lineStyle: {
            color: '#f2f2f2',
          }
        },
        axisLabel: {
          color: '#333',
          show: true,
          interval: 0,
          rotate: 25
        },
      }
    ],
    yAxis:[
      {
        type: 'value',
        axisLine: {
          lineStyle: {
            color: '#f2f2f2'
          }
        },
        axisLabel: {
          color: '#999'
        },
        splitLine:{
          show: true,
          lineStyle: {
            color: '#f2f2f2'
          }
        },
      }
    ],
    series: [{
      data: [120, 250, 150, 80, 50, 110, 130],
      type: 'bar',
      barWidth : 20,
      label: {
        show: true,
        position: 'top',
        color: '#C6C6C6',
        padding: [5, 6, 5, 6]
      },
      showBackground: true,
      backgroundStyle: {
          color: 'rgba(220, 220, 220, 0.8)'
      },
      itemStyle: {
        color: new echarts.graphic.LinearGradient(
            0, 0, 0, 1,
            [
              {offset: 0, color: '#4BCFFE'},
              {offset: 0.5, color: '#41B4FE'},
              {offset: 1, color: '#3392FF'}
            ]
        )
      },
    }]
  };
  chart.setOption(option);
  return chart;
}

Page({
  data: {
    ec: {
      onInit: initChart
    }
  }
});
微信小程序使用 ECharts 图表时,图表两侧被剪切的问题通常与小程序的视图容器布局、画布(canvas)的尺寸设置以及 ECharts 自身的渲染机制有关。以下是常见的解决方案: ### 1. 调整 ECharts 图表的 `padding` 设置 ECharts 提供了 `grid` 配置项,可以控制图表与容器边缘的间距。通过设置 `grid.left` 和 `grid.right` 为百分比或具体像素值,确保图表内容不会超出容器边界。 ```javascript option = { grid: { left: '10%', right: '10%', bottom: '10%', top: '10%', containLabel: true }, xAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'] }, yAxis: {}, series: [{ data: [10, 20, 30, 40, 50], type: 'bar' }] }; ``` ### 2. 调整小程序页面的样式和布局 确保 `canvas` 容器所在的父元素具有足够的宽度,并且没有设置 `overflow: hidden` 或其他限制内容显示的样式。可以为容器设置 `white-space: nowrap` 来防止标签文本换行。 ```css .chart-container { width: 100%; overflow: visible; white-space: nowrap; } ``` ### 3. 使用 `echarts-for-weixin` 插件的 `disableScroll` 属性 如果使用的是 [echarts-for-weixin](https://github.com/ecomfe/echarts-for-weixin) 插件,可以尝试在组件中设置 `disableScroll` 属性为 `true`,以防止图表区域被裁剪或影响页面滚动。 ```json { "usingComponents": { "ec-canvas": "echarts-for-weixin/dist/ec-canvas" } } ``` ```html <view class="chart-container"> <ec-canvas id="chart" ec="{{ ec }}" disableScroll="{{ true }}"></ec-canvas> </view> ``` ### 4. 动态计算 `canvas` 尺寸 在页面加载或窗口尺寸变化时动态计算 `canvas` 的宽度和高度,确保其适配不同屏幕尺寸。可以通过 `wx.getSystemInfoSync()` 获取设备信息,并根据实际容器大小调整图表尺寸。 ```javascript Page({ onLoad() { const systemInfo = wx.getSystemInfoSync(); const canvasWidth = systemInfo.windowWidth * 0.9; // 设置为窗口宽度的90% const canvasHeight = 300; this.setData({ canvasWidth, canvasHeight }); // 初始化 ECharts 实例 const chart = echarts.init(document.getElementById('chart')); chart.setOption(option); } }); ``` ### 5. 使用 `echarts` 的 `resize` 方法 在页面布局变化后,调用 `echarts.getInstanceByDom` 获取图表实例并调用 `resize` 方法,以确保图表能够正确适配新的容器尺寸。 ```javascript const chart = echarts.getInstanceByDom(document.getElementById('chart')); chart.resize(); ``` ### 6. 避免使用 `rpx` 单位导致的尺寸问题 在微信小程序中,`rpx` 是一种相对单位,但在某些情况下可能导致 `canvas` 渲染异常。建议在设置 `canvas` 宽高时使用 `px` 单位,或在 JavaScript 中动态计算尺寸。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值