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.
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
contextobject is not a standard CanvasRenderingContext2D, so the core idea is to align the mini‑program
contextwith 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.vueunder the
componentsfolder.
<code><template>
<canvas type="2d" class="f2-canvas" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas>
</template></code>Set the canvas size:
<code><style lang="less">
.f2-canvas {
width: 100%;
height: 600rpx;
}
</style></code>The component receives an
onInitprop to obtain the F2 constructor and configuration object.
<code>props: {
onInit: {
type: Function,
default: () => {}
}
}</code>During initialization we query the canvas node, adjust its pixel ratio, build the config, and invoke the provided
onInitto create the chart:
<code>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')
}
})</code>Touch event wrappers forward events to the canvas element:
<code>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))
}
</code>After the component is ready, use it in a page:
<code><f2-uni class="f2-chart" :onInit="onInitChart"></f2-uni></code> <code>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
}
}
</code>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:
WeDoctor Frontend Technology
Official WeDoctor Group frontend public account, sharing original tech articles, events, job postings, and occasional daily updates from our tech team.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.