微信小程序使用echarts实现条形统计图功能

微信小程序使用echarts组件实现条形统计图功能

使用echarts实现在微信小程序中统计图的功能,其实很简单,只需要简单的两步就可以实现啦,具体思路如下:

  1. 引入echarts组件
  2. 调用相应的函数方法
    由于需要引入echarts组件,代码行数较多,无法在此粘贴,需要的友友们可以进我的主页,进行免费的资源下载,具体的实现结果截图如下:


    echarts组件代码较多,components文件夹的内容可在我主页的资源免费下载或者可前往echarts官网下载呦,引入的路径一定要正确。具体关于pages文件夹的代码如下:
    1、wxml代码:
html 复制代码
<view class="charts-box">
  <view style="height: 500rpx;">
    <ec-canvas canvas-id="mychart-bar" ec="{{spread}}"></ec-canvas>
  </view>
  <view class="charts-title">
    <view class="charts-pot"></view>
    <view>{{title}}</view>
  </view>
</view>

2、wxss代码:

css 复制代码
page {
  background-color: #f1f1f1;
}

.charts-box {
  background-color: white;
  margin: 20rpx;
  border-radius: 15rpx;
  padding: 20rpx;
}

.charts-title {
  display: flex;
  flex-direction: row;
  font-size: 24rpx;
  align-items: center;
  justify-content: center;
  color: rgb(173, 173, 173);
  margin: 30rpx 0 10rpx;
}

.charts-pot {
  width: 46rpx;
  height: 24rpx;
  background-color: #fcadb0;
  margin-right: 10rpx;
  border-radius: 10rpx;
  margin-top: 2rpx;
}

3、js代码:

javascript 复制代码
import * as echarts from '../../components/ec-canvas/echarts.min';
let chart = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'我的CSDN创作统计',
    spread: {
      onInit: initChart
    }
  },
})

function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);
  let option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '0%',
      containLabel: true
    },
    xAxis: [{
      type: 'category',
      data: ['文章总数', '经典界面', '微信小程序', '学习经验', '系统示例'],
      axisTick: {
        alignWithLabel: true
      }
    }],
    yAxis: [{
      type: 'value'
    }],
    series: [{
      name: '我的创作',
      type: 'bar',
      barWidth: '50%',
      data: [25, 1, 21, 2, 4],
      itemStyle: {
        color: '#fcadb0'
      }
    }]
  };
  chart.setOption(option);
  return chart;
}

4、json代码:

javascript 复制代码
{
  "component": true,
  "navigationBarTitleText": "echarts·条形统计图",
  "navigationBarBackgroundColor": "#008B8B",
  "usingComponents": {
    "ec-canvas":"../../components/ec-canvas/ec-canvas"
  }
}

我这里简单绘制的条形统计图,如需要其他的统计图,可自行学习或者进我主页,将分享更多内容呦~

小编创作不易,口干舌燥,可以的话,请我喝杯水吧!欢迎各位的批评指导呦~

相关推荐
qq_452396237 小时前
第六篇:《CI/CD 流水线:从前端构建到自动化部署》
前端·ci/cd·自动化
show4337 小时前
2026多格式导出技术方案:小程序端免费TXT/Word/SRT生成实践
小程序·word
咏方舟【长江支流】7 小时前
【前端1】单据编辑 -EasyUI/Vue/React/Bootstrap 主流框架实现订单单据主子表显示和编辑比较,哪个你最易入门?
前端·vue.js·easyui·咏方舟-长江支流·userbaodatagrid
m0_579146657 小时前
Element UI 表格合并单元格完全指南:从原理到实战
前端·vue.js·elementui
sibylyue7 小时前
# Web端流媒体JS播放器开源库
前端·javascript·开源
码视野7 小时前
基于 Vue3 + Element Plus 的【基于物联网与深度学习的智慧工业园区能耗监测与光储充一体化微电网协同调度系统】设计与实现(附完整源码与PRD)
前端·人工智能·深度学习·物联网·vue3
铁皮饭盒7 小时前
网页端, 5.5mb谷歌模型, 识别躯干, 视频都不卡
前端·javascript·后端
雪芽蓝域zzs7 小时前
Prettier (代码格式化)配置详解
前端·vue.js
晴天167 小时前
Gradio 与 Streamlit:Python 快速构建 Web 应用的双子星-Day28
开发语言·前端·python
码哥DFS7 小时前
LRU缓存
前端·javascript·缓存