search
HomeWeChat AppletMini Program DevelopmentHow to use echarts in the WeChat mini program and possible pitfalls, come and collect it to avoid lightning! !

This article will share with you some methods of using echarts in WeChat mini programs and a summary of problems, so that you can avoid pitfalls and thunderstorms. I hope it can help you!

How to use echarts in the WeChat mini program and possible pitfalls, come and collect it to avoid lightning! !

How to use the WeChat mini program and some pitfalls, I hope it can help you

Using echarts in the WeChat mini program

1. Download the mini program version of echarts

Download address: https://github.com/ecomfe/echarts- for-weixin

2. Use steps

##1.Introduce project dependencies

After pulling the WeChat applet version echarts from github, copy the ec-canvas file in the file to your own project. It is actually the dependency file of the WeChat applet version echarts.

[Related learning recommendations:

小program development tutorial

How to use echarts in the WeChat mini program and possible pitfalls, come and collect it to avoid lightning! !

## 2. Import the library

Introduce echarts into the json file of the page where echarts is used. The imported path is imported according to your own project structure.

You can also introduce it in the global configuration file app.json, so that everything is The page is universal and does not need to be introduced one by one. When multiple pages use echarts, it is more convenient

How to use echarts in the WeChat mini program and possible pitfalls, come and collect it to avoid lightning! !Introduce echarts into the js file of the page using echarts. The imported path is imported according to your own project structure

How to use echarts in the WeChat mini program and possible pitfalls, come and collect it to avoid lightning! !

Usage method

Use the component in wxml, Both id and canvas-id can be named by yourself

<view class="container">
  <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
</view>

js code

For option configuration, you can go to the official website to read the documentation or refer to the example, https://echarts.apache.org /zh/option.html#title

import * as echarts from &#39;../../ec-canvas/echarts&#39;;

const app = getApp();

function initChart(canvas, width, height, dpr) {
  //主要是这个 echarts 的创建
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);
  
  // option 的配置可以根据自己的需求去 echarts 官网查看配置的属性方法
  var option = {
    backgroundColor: "#ffffff",
    series: [{
      label: {
        normal: {
          fontSize: 14
        }
      },
      type: &#39;pie&#39;,
      center: [&#39;50%&#39;, &#39;50%&#39;],
      radius: [&#39;20%&#39;, &#39;40%&#39;],
      data: [{
        value: 55,
        name: &#39;北京&#39;
      }, {
        value: 20,
        name: &#39;武汉&#39;
      }, {
        value: 10,
        name: &#39;杭州&#39;
      }, {
        value: 20,
        name: &#39;广州&#39;
      }, {
        value: 38,
        name: &#39;上海&#39;
      }]
    }]
  };

  chart.setOption(option);
  return chart;
}

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

  onReady() {
  }
});

css code
.container{
  width: 100%;
  height: 100vw;
}
ec-canvas {
  width: 100%;
  height: 100%;
}

avoid pitfalls and step on thunder

1. Regarding the introduction of dependencies, introduce Library, after using it according to the official usage method, the problem of echarts picture not being displayed

Solution: When using echarts in HTML, the width and height of the view tag wrapped in the outer layer must also be set (The official method style does not set the width and height of the outer element, but it can be displayed in the official example, which misleads many people and will lead to a trap)

How to use echarts in the WeChat mini program and possible pitfalls, come and collect it to avoid lightning! !2 , About the role of

devicePixelRatio

You can see that when echarts is initially created in the official code, the code is as shown below. After you set devicePixelRatio, the chart of echarts can be seen in the WeChat developer tools The pixels of the echarts chart are very poor. When previewed on a mobile phone, they are very clear. After removing the devicePixelRatio, the pixels of the echarts chart seen in the WeChat developer tools become very clear, but the pixels seen on the mobile phone are poor. , so this should be adapted to the pixels of the chart according to the pixel unit of the device

How to use echarts in the WeChat mini program and possible pitfalls, come and collect it to avoid lightning! !For more programming-related knowledge, please visit:

Programming Video

! !

The above is the detailed content of How to use echarts in the WeChat mini program and possible pitfalls, come and collect it to avoid lightning! !. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!