How to Integrate AntV/F2 and ECharts into Uni‑App for WeChat Mini‑Programs

This tutorial demonstrates step‑by‑step how to wrap popular H5 chart libraries like AntV/F2 and ECharts inside a Uni‑App component, enabling seamless chart rendering in WeChat mini‑programs using Vue syntax and handling canvas context compatibility.

WeDoctor Frontend Technology
WeDoctor Frontend Technology
WeDoctor Frontend Technology
How to Integrate AntV/F2 and ECharts into Uni‑App for WeChat Mini‑Programs

Preface

This article explains how to use common H5 chart libraries (AntV/F2, ECharts) in WeChat mini‑programs by leveraging Uni‑App’s cross‑platform capabilities and the familiar Vue framework.

antv/f2

F2 3.x draws charts on a standard CanvasRenderingContext2D. In mini‑programs the provided context object is not a standard CanvasRenderingContext2D, so the core idea is to align the mini‑program context with the standard API. The f2‑context repository shows how to do this for Alipay and WeChat; the same approach works for other mini‑programs.

We create a new component f2-uni.vue under the components folder.

<template>
  <canvas type="2d" class="f2-canvas" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas>
</template>

Set the canvas size:

<style lang="less">
.f2-canvas {
  width: 100%;
  height: 600rpx;
}
</style>

The component receives an onInit prop to obtain the F2 constructor and configuration object.

props: {
  onInit: {
    type: Function,
    default: () => {}
  }
}

During initialization we query the canvas node, adjust its pixel ratio, build the config, and invoke the provided onInit to create the chart:

const query = uni.createSelectorQuery().in(this)
query.select('.f2-canvas')
  .fields({ node: true, size: true })
  .exec(res => {
    const { node, width, height } = res[0]
    const context = node.getContext('2d') // supported from WeChat base library 2.7.0
    const pixelRatio = uni.getSystemInfoSync().pixelRatio
    node.width = width * pixelRatio
    node.height = height * pixelRatio
    const config = { context, width, height, pixelRatio }
    const chart = this.onInit(F2, config)
    if (chart) {
      this.canvasEl = chart.get('el')
    }
  })

Touch event wrappers forward events to the canvas element:

function wrapEvent(e) {
  if (!e) return
  if (!e.preventDefault) {
    e.preventDefault = function() {}
  }
  return e
}

touchStart(e) {
  const canvasEl = this.canvasEl
  if (!canvasEl) return
  canvasEl.dispatchEvent('touchstart', wrapEvent(e))
}

After the component is ready, use it in a page:

<f2-uni class="f2-chart" :onInit="onInitChart"></f2-uni>
methods: {
  onInitChart: (F2Constructor, config) => {
    const chart = new F2Constructor.Chart(config)
    const data = [
      { value: 63.4, city: 'New York', date: '2011-10-01' },
      { value: 62.7, city: 'Alaska', date: '2011-10-01' },
      { value: 72.2, city: 'Austin', date: '2011-10-01' },
      // ... more data points ...
    ]
    chart.source(data, {
      date: { range: [0, 1], type: 'timeCat', mask: 'MM-DD' },
      value: { max: 300, tickCount: 4 }
    })
    chart.area().position('date*value').color('city').adjust('stack')
    chart.line().position('date*value').color('city').adjust('stack')
    chart.render()
    return chart // return the chart instance
  }
}

The chart renders correctly, as shown below:

This guide provides the basic idea; the actual wrapper can be further refined for flexibility.

echarts

The approach for ECharts is similar: adapt the mini‑program DOM selector and canvas context. You can refer to the DCloud community plugin “ECharts for Uni‑App” ( link ) and inspect its source code.

If you only target the WeChat mini‑program platform and conditional compilation is not effective, a pure version is available as a code snippet ( link ).

Illustrations:

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

VueEChartsuni-appAntVF2chart integrationwechat mini-program
WeDoctor Frontend Technology
Written by

WeDoctor Frontend Technology

Official WeDoctor Group frontend public account, sharing original tech articles, events, job postings, and occasional daily updates from our tech team.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.