当前位置: 首页 > article >正文

微信小程序动态加载图表[echart]

1.引入Echarts
(1)将ec-canvas文件拷贝下来放到你自己的项目中:
在这里插入图片描述

(2)在你需要使用Echarts的页面的json文件中引入Echarts

"usingComponents": {
    "ec-canvas": "../utils/ec-canvas/ec-canvas"
  }

2.使用Echarts
在需要显示图表的页面的wxml中使用Echarts。

<view>
    <ec-canvas id="mychart-dom-line" canvas-id="mychart-line" ec="{{ ec }}" />
</view>

在wxss设置宽度和高度

ec-canvas {
  width: 100%;
  height: 100%;
}

3.js页面设置Echarts以及动态刷新
有三个注意的点:
1:独立option
2:onReady获取节点
3:更新数据:同时更新init和option

import * as echarts from "../utils/ec-canvas/echarts";
// 1、独立option
function setOptionData(chart, name1, name2, xdata, ydata) {
  const option = {
    tooltip: {
      trigger: "axis",
      backgroundColor: "#092646",
      axisPointer: {
        type: "shadow",
        label: {
          show: true,
          backgroundColor: "#7B7DDC",
          color: "#FFF",
        },
      },
      textStyle: {
        color: "white", //设置文字颜色
      },
    },
    legend: {
      data: [name1, name2],
      itemWidth: 20,
      itemHeight: 10,
      textStyle: {
        color: "#B4B4B4",
        fontSize: 10,
      },
      top: "1%",
    },
    grid: {
      top: "12%",
      left: "1%",
      right: "5%",
      bottom: "2%",
      containLabel: true,
    },
    xAxis: {
      data: xdata,
      axisLine: {
        lineStyle: {
          color: "rgba(255,255,255,.3)",
        },
      },
      axisLabel: {
        color: "rgba(255,255,255,.5)",
        fontSize: 10,
      },
      axisTick: {
        show: false,
      },
    },
    yAxis: {
      x: "center",
      type: "value",
      splitLine: { show: false },
      axisLabel: {
        color: "rgba(255,255,255,.5)",
        fontSize: 10,
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: "#195384",
        },
      },
    },
    series: [
      {
        name: name2,
        type: "bar",
        barWidth: 20,
        itemStyle: {
          normal: {
            barBorderRadius: 5,
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: "#956FD4" },
              { offset: 1, color: "#3EACE5" },
            ]),
          },
        },
        data: ydata,
      }
    ],
  };
  chart.setOption(option);
}
Page({
	data:{
		ec: {
	      onInit: null,
	      lazyLoad: true
      	}
	},
	onLoad(){
		this.getStatData()
	},
	onReady() {
		// 2、在页面渲染后拿到节点
    	this.oneComponent = this.selectComponent('#mychart-dom-line');
    },
    getStatData(){
    	const name1 = "name1"
    	const name2 = "name2"
		const xdata = ['a','b','c']
		const ydata = [4,5,6]
    	this.initChart(name1, name2, xdata, ydata)
    },
    initChart(name1, name2, xdata, ydata) {
    	// 3、根据拿到的节点重新init 并 更新数据option
    	this.oneComponent.init((canvas, width, height, dpr) => {
	        const chart = echarts.init(canvas, null, {
	            width: width,
	            height: height,
	            devicePixelRatio: dpr // new
	        });
	        setOptionData(chart, name1, name2, xdata, ydata)
	        this.chart = chart;
	        return chart;
    	});
  	}
})

http://www.kler.cn/news/161946.html

相关文章:

  • 假设检验(三)(单侧假设检验)
  • MongoDB日期查询详解
  • 【DevOps】Jenkins:配置jenkins 流水线/多分支流水线任务构建成功通知企业微信@相关人(二)
  • [GPT-1]论文实现:Improving Language Understanding by Generative Pre-Training
  • 【CSP】202303-1_田地丈量Python实现
  • CSS3 属性: transition过渡 与 transform动画
  • 用CHAT写一篇小短文
  • okhttp3 的简单使用
  • 类和对象,this指针
  • 通过51单片机控制28byj48步进电机按角度正反转旋转
  • 数据结构练习——素数统计
  • Redisson配置
  • 【STM32】TIM定时器输入捕获
  • WordCount 源码解析 Mapper,Reducer,Driver
  • 【环境搭建】ubuntu22安装ros2
  • 麒麟KYLINOS操作系统修改GRUB字体大小
  • lodash常用方法
  • 2023.12.1 --数据仓库之 拉链表
  • ⭐Unity 搭建UDP客户端(01) 配合网络调试助手测试
  • BUUCTF-[GYCTF2020]FlaskApp flask爆破pin
  • Docker入门:容器化原理
  • Clean 架构下的现代 Android 架构指南
  • 实验3.5 路由器的单臂路由配置
  • 装配式技术助力EHS平台系统:打造全方位的安全在线监测平台!
  • 【PTA-C语言】编程练习4 - 数组Ⅱ
  • 【面试经典150 | 二叉树】翻转二叉树
  • ubuntu内移除snap
  • VUE2+THREE.JS 按照行动轨迹移动人物模型并相机视角跟随人物
  • 智能优化算法应用:基于材料生成算法无线传感器网络(WSN)覆盖优化 - 附代码
  • Thymeleaf生成pdf表格合并单元格描边不显示