uniapp 微信小程序 使用ucharts

文章目录

前言

本文介绍一个基于 Vue 框架的小程序图表组件开发方案。该组件通过 uCharts 库实现折线图的绘制,并支持滚动、缩放、触摸提示等交互功能。文章将从代码结构、核心方法、交互实现和样式设计等方面进行详细解析。

一、组件功能概述

该组件实现了以下核心功能:

  1. 动态折线图绘制
  2. 图表滚动交互
  3. 双指缩放功能
  4. 数据点提示框
  5. 响应式布局适配

二、代码结构分析

2.1 模板结构

vue 复制代码
<template>
    <canvas
        canvas-id="chart"
        id="chart"
        @touchstart="touchstart"
        @touchmove="touchmove"
        class="charts"
        @touchend="touchend"
    />
</template>

<script>
import uCharts from '@/js_sdk/u-charts.js'

var uChartsInstance = {}

export default {
    data() {
        return {
            cWidth: 750,
            cHeight: 900,
            options: {}
        }
    },
    onReady() {
        this.cWidth = uni.upx2px(750)
        this.cHeight = uni.upx2px(900)
    },
    methods: {
        generateData(data) {
            if (!data) {
                console.error('数据未提供,请传入有效的数据对象。');
                return;
            }
            this.drawCharts('chart', data);
        },
        drawCharts(id, data) {
            try {
                const min = this.getMin(data.series);
                const ctx = uni.createCanvasContext(id, this);
                const chartOptions = {
                    type: 'line',
                    context: ctx,
                    width: this.cWidth,
                    height: this.cHeight,
                    categories: data.categories,
                    series: data.series,
                    animation: true,
                    touchMoveLimit: 24,
                    background: '#FFFFFF',
                    enableScroll: true,
                    scrollPosition: 'current',
                    padding: [15, 15, 0, 5],
                    legend: {},
                    dataLabel: false,
                    xAxis: {
                        disableGrid: true,
                        scrollShow: true,
                        itemCount: 4,
                        labelCount: 2,
                        formatter: (value) => {
                            const [a, b] = value.split(' ');
                            return b.split(':').slice(0, 2).join(':');
                        }
                    },
                    yAxis: {
                        data: [{ min }]
                    },
                    extra: {
                        line: {
                            type: 'straight',
                            width: 2,
                            activeType: 'hollow'
                        },
                        tooltip: {
                            showCategory: true
                        }
                    }
                };
                uChartsInstance[id] = new uCharts(chartOptions);
            } catch (error) {
                console.error('绘制图表时发生错误:', error);
            }
        },
        getMin(series) {
            let min = Infinity;
            series.forEach(item => {
                item.data.forEach(value => {
                    if (value < min) {
                        min = value;
                    }
                });
            });
            return min;
        },
        touchstart(e) {
            if (uChartsInstance[e.target.id]) {
                uChartsInstance[e.target.id].scrollStart(e);
            }
        },
        touchmove(e) {
            if (uChartsInstance[e.target.id]) {
                uChartsInstance[e.target.id].scroll(e);
                uChartsInstance[e.target.id].dobuleZoom(e);
            }
        },
        touchend(e) {
            if (uChartsInstance[e.target.id]) {
                uChartsInstance[e.target.id].scrollEnd(e);
                uChartsInstance[e.target.id].touchLegend(e);
                uChartsInstance[e.target.id].showToolTip(e);
            }
        }
    }
}
</script>

<style>
page {
    width: 100%;
    height: 100%;
    background: #fff;
}
</style>

<style lang="scss" scoped>
.charts {
    width: 750rpx;
    height: 900rpx;
}
</style>    

总结

本文仅仅简单介绍了ucharts在uniapp微信小程序中的使用。

相关推荐
笨笨狗吞噬者14 小时前
uni-app 编译小程序原生组件时疑似丢属性,可以给官方提 PR 了
前端·微信小程序·uni-app
摇滚侠17 小时前
JAVA 项目教程《苍穹外卖-11》,微信小程序项目,前后端分离,从开发到部署
java·开发语言·微信小程序
Emma_Maria19 小时前
【小程序】project.config.json 和project.private.config.json 区别以及对应字段含义
小程序
CHU72903519 小时前
家政保洁小程序功能架构解析
小程序
计算机徐师兄20 小时前
Java基于SSM的文玩销售小程序【附源码、文档说明】
java·小程序·文玩销售小程序·文玩销售·java文玩销售小程序·文玩销售微信小程序·java文玩销售微信小程序
雨雨雨雨雨别下啦21 小时前
【从0开始学前端】从0搭建uni-app小程序脚手架
小程序·uni-app
AnalogElectronic1 天前
uniapp学习8,电动车充电小程序
学习·小程序·uni-app
2501_915921431 天前
iPhone 定位功能测试时不越狱来修改手机位置的方法
android·ios·智能手机·小程序·uni-app·iphone·webview
TE-茶叶蛋1 天前
安卓应用(uniapp开发)分享微信-申请appid
android·微信·uni-app
AnalogElectronic1 天前
uniapp学习9,同时兼容h5和微信小程序的百度地图组件
学习·微信小程序·uni-app