微信小程序引入echarts

一、下载

https://github.com/ecomfe/echarts-for-weixinhttps://github.com/ecomfe/echarts-for-weixin

二、引入小程序配置

1.解压下载文件
2.复制粘贴’ec-canvas‘文件

3.将复制粘贴的文件放到小程序根目录当中

4.在自己需要的页面中引入 

三、使用 Echarts 绘制图表 

1.index.wxml
<!--index.wxml-->
<!-- 统计图 -->
<scroll-view class="scroll_view" scroll-y="{{true}}" bindscroll="scrollPage" scroll-top="{{scrollTop}}" scroll-into-view="{{toView}}">
  <view style="width: 100%;height: 500rpx;"></view>
  <view class="statistical-chart">
    <ec-canvas id="mychart" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
  </view>
  <view style="width: 100%;height: 800rpx;"></view>
</scroll-view>
2.在index.js当中引入并且配置option:
// index.js
//第一步引入echarts 
import * as echarts from './../../ec-canvas/echarts';


//第二步配置optino

function initChart(canvas, width, height) {
  console.log('canvas',width, height);
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);
  var option = {
    xAxis: {
      type: 'category',
      boundaryGap: false,
      // data: ['最低价', '本房源', '最高价'],
      axisLabel: {
        show: false // 设置为false,不显示x轴刻度数字
      }
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        show: false // 设置为false,不显示y轴刻度数字
      }
    },
    series: [
      {
        data: ['20','50','80'],
        type: 'line',
        color: '#EA3333',
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              { offset: 0, color: '#ea333300' },
              { offset: 1, color: '#ea333321' }
            ],
            global: false
          }
        },
        label: {
          show: true,
          position: 'top',
          backgroundColor: '#6A6A6A', // 添加标签背景颜色
          color: '#ffff',
          height: '20',
          width: '80',
          distance: 10, // 设置文字与节点的距离为 20
          verticalAlign: 'center',
          fontSize: '10',
         
        }
      }
    ]
  };
  chart.setOption(option);
  return chart;
}


//第三步初始化折线图


Page({
  data: {
    ec: {
      onInit: initChart
    },
  },
  




})
 3.index.wxss设置样式
.statistical-chart {
  width: 100%;
  height: 200rpx;
  margin-top: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}
ec-canvas{
  width: 100%;
  height: 100%;
}
.scroll_view {
  height: 2500rpx;
}

四、微信小程序引用Echarts绘图显示模糊解决办法

微信小程序中使用ECharts可以方便地进行数据可视化展示。以下是使用ECharts的基本步骤: 1. **下载ECharts** 首先,从ECharts官网下载最新版本的ECharts库。将其解压后,将`echarts.js`文件放入小程序的`/utils`目录下。 2. **引入ECharts** 在需要使用ECharts的页面中,通过`require`引入ECharts库。例如,在页面的`.js`文件中: ```javascript const echarts = require('../../utils/echarts.js'); ``` 3. **创建图表容器** 在页面的`.wxml`文件中,创建一个用于展示图表的`canvas`容器,并设置其`id`和尺寸: ```html <canvas canvas-id="myChart" style="width: 100%; height: 400px;"></canvas> ``` 4. **初始化图表** 在页面的`.js`文件中,编写初始化图表的代码。例如: ```javascript Page({ onReady: function () { this.initChart(); }, initChart: function () { const ec = echarts.init(document.getElementById('myChart')); const option = { title: { text: 'ECharts 示例' }, tooltip: {}, xAxis: { data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }; ec.setOption(option); } }); ``` 5. **适配小程序环境** 由于微信小程序的环境与浏览器不同,需要对ECharts进行一些适配。可以通过`wx.createSelectorQuery()`获取`canvas`的实际尺寸,并调整图表的尺寸。例如: ```javascript Page({ onReady: function () { this.initChart(); }, initChart: function () { const that = this; wx.createSelectorQuery().select('#myChart').boundingClientRect(function (rect) { const ec = echarts.init(that.selectComponent('#myChart').$el); const option = { // 图表配置 }; ec.setOption(option); }).exec(); } }); ``` 通过以上步骤,你就可以在微信小程序中使用ECharts进行数据可视化展示了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值