uCharts基本使用方法

图表组件uCharts, 小程序上开发者们如果有图表的需求可以尝试使用

首先下载ucharts文件

https://gitee.com/uCharts/uCharts

在这里插入图片描述
下载下来看到有这些文件,小伙伴们可以先去示例项目里面看

H5端

引入u-charts.js文件,主要构建就是new uCharts和配置context,其他的就跟其他charts配置一样
可以看例子写的,也可以自己试验一波

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #aaa {
            width: 100%;
            height: 500px;
        }
    </style>
</head>

<body>
    <canvas id="aaa"></canvas>
</body>

</html>
<script src="../statics/js/jquery.min.js"></script>  // 自行替换
<script src="../statics/js/assets/js/u-charts.js"></script>  // 自行替换
<script>
    var option = {
        animation: true,
        background: "#FFFFFF",
        categories: ["2016", "2017", "2018", "2019", "2020", "2021"],
        color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4", "#ea7ccc"],
        extra: {
            column: {
                type: "group",
                width: 30,
                activeBgColor: "#000000",
                activeBgOpacity: 0.08
            }
        },
        legend: {},
        padding: [15, 15, 0, 5],
        series: [
            {
                name: "目标值",
                data: [35, 36, 31, 33, 13, 34]
            },
            {
                name: "完成量",
                data: [18, 27, 21, 24, 6, 28]
            }
        ],
        type: "column",
        xAxis: {
            disableGrid: true
        },
        yAxis: {
            data: [
                {
                    min: 0
                }
            ]
        }
    }


    setTimeout(() => {
        let uChartsInstance = {}
        const canvas = document.getElementById('aaa');
        const ctx = canvas.getContext("2d");
        canvas.width = canvas.offsetWidth;
        canvas.height = canvas.offsetHeight;
        option.height = canvas.height
        option.width = canvas.width
        option.context = ctx;   // 找到目标元素
        uChartsInstance.aaa = new uCharts(option)  // 元素包裹着方便找到模块,方便注册事件
        canvas.onclick = function (e) {    
            uChartsInstance.aaa.touchLegend(getH5Offset(e));
            uChartsInstance.aaa.showToolTip(getH5Offset(e));
        };
        canvas.onmousemove = function (e) {
            uChartsInstance.aaa.showToolTip(getH5Offset(e));
        };
        console.log(uChartsInstance)
    }, 1000);


</script>

微信小程序( uniapp )

方法写入两种方式

第一种方式 ucharts下载下来的文件,只引入js文件
在这里插入图片描述
在项目中引入
在这里插入图片描述
第二种方式 直接在插件市场里导入到项目

就可以看到有一个完整的模块插件

两种方法的区别在于,只引入js的 需要自己配置参数,直接导入的只需要获取数据即可

https://demo.ucharts.cn/#/

ucharts提供了一个可以实时编译的平台,可以在线调整完之后在替换项目内容

以下具体实现
第一个只引入js的方法

<template>
	<view class="qiun-columns">
		<view class="qiun-charts" >
			<canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" @touchstart="touchLineA"></canvas>
		</view>
	</view>
</template>

<script>
	// 引入uCharts 方法组件。
	import uCharts from '@/components/u-charts/u-charts.js';
	// 定义全局变量
	var _self;
	var canvaLineA=null;
	export default {
		data() {
			return {
				cWidth:'',
				cHeight:'',
				pixelRatio:1,
			}
		},
		// 页面加载执行的函数
		onLoad() {
			_self = this;
			// uni.upx2px(750) 这是uni-app自带的自适应,以750的尺寸为基准。动态变化
			this.cWidth=uni.upx2px(750);
			this.cHeight=uni.upx2px(500);
			this.getServerData();
		},
		methods: {
			// 获取数据,发请求 (我这里写死)
			getServerData(){
				setTimeout(() => {
					this.chartData = {
						categories: ['2016', '2017', '2018', '2019', '2020', '2021'],
						series: [{
							name: "成交量",
							data: [35, 32, 36, 34, 38, 30]
						}]
					}
					_self.showLineA("canvasLineA", this.chartData);
				}, 800)
			},
			// 展示图标的函数 接收参数,一个块的id,一个数据
			showLineA(canvasId,chartData){
				canvaLineA=new uCharts({
					$this:_self,
					canvasId: canvasId,
					// 图标类型
					type: 'line',
					fontSize:11,
					legend:{show:true},
					dataLabel:false,
					dataPointShape:true,
					background:'#FFFFFF',
					pixelRatio:_self.pixelRatio,
					categories: chartData.categories,
					series: chartData.series,
					animation: true,
					context:uni.createCanvasContext(canvasId, _self), // 这里很重要
					// x轴显示的内容
					xAxis: {
						type:'grid',
						gridColor:'#CCCCCC',
						gridType:'dash',
						dashLength:8
					},
					// y轴显示的内容
					yAxis: {
						gridType:'dash',
						gridColor:'#CCCCCC',
						dashLength:8,
						splitNumber:5,
						min:10,
						max:180,
						format:(val)=>{return val.toFixed(0)+'元'}
					},
					width: _self.cWidth*_self.pixelRatio,
					height: _self.cHeight*_self.pixelRatio,
					extra: {
						line:{
							type: 'straight'
						}
					}
				});
				
			},
			// 点击图表显示的内容
			touchLineA(e) {
				// 使用声明的变量canvaLineA
				canvaLineA.showToolTip(e, {
					format: function (item, category) {
						return category + ' ' + item.name + ':' + item.data 
					}
				});
			}
		}
	}
</script>

<style scoped>
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	
	.charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
</style>




另一种引入了整个插件的方式

<template>
	<view>
		<view class="charts-box">
		  <qiun-data-charts
		    type="column"
		    :chartData="chartData"
		    background="none"
		  />
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				// chartData:{
				//   categories:[],
				//   series:[],
				// },
				chartData : {
					categories: ["2016", "2017", "2018", "2019", "2020", "2021"],
					series: [{
						"name": "目标值",
						"data": [35, 36, 31, 33, 13, 34]
					}, {
						"name": "完成量",
						"data": [18, 27, 21, 24, 6, 28]
					}]
				}
			}
		}
	}
</script>
<style>
	/* 请根据需求修改图表容器尺寸,如果父容器没有高度图表则会显示异常 */
	.charts-box{
	  width: 100%;
	  height:300px;
	}
</style>

获取到数据即可实现,如果需要更改样式,可以去在线编译处调整好在替换掉对应的类型就好,也可以自定类型的名字
在这里插入图片描述
实现图
在这里插入图片描述
在这里插入图片描述

有新姿势可以留言,谢谢各位观赏!!

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在支付宝小程序使用ucharts可以通过`<canvas>`标签和`canvas`绘图的方式来实现。具体步骤如下: 1. 下载并引入uCharts库,可以将下载好的文件夹放在项目的`utils`文件夹下,然后在需要使用的页面对应的`js`文件中引入,并创建绘图对象,例如: ```javascript import uCharts from '../../utils/u-charts/u-charts.min.js'; Page({ data: { chartData: null, pixelRatio: 1, windowWidth: 320 }, onLoad() { let windowWidth = 320; try { const res = wx.getSystemInfoSync(); windowWidth = res.windowWidth; this.setData({ pixelRatio: res.pixelRatio, windowWidth: windowWidth }); } catch (e) { console.error('获取系统信息失败:', e); } const chartData = { categories: ['1月', '2月', '3月', '4月', '5月', '6月'], series: [{ name: '成交量', data: [10, 20, 30, 40, 50, 60] }, { name: '成交额', data: [100, 200, 300, 400, 500, 600] }] }; this.setData({ chartData: chartData }); const context = wx.createCanvasContext('chart'); const chart = new uCharts({ $this: this, canvasId: 'chart', type: 'column', width: windowWidth, height: 200, pixelRatio: this.data.pixelRatio, legend: { show: true, position: 'top', fontSize: 14, fontFamily: 'Arial', fontWeight: 'bold', fontColor: '#666', lineHeight: 20 }, dataLabel: { show: true, formatter: (name, value) => { return value; }, fontSize: 12, fontFamily: 'Arial', fontWeight: 'normal', fontColor: '#666', lineHeight: 20, margin: 10 }, xAxis: { data: chartData.categories, axisLine: { show: true, lineColor: '#ccc', lineWidth: 1 }, axisTick: { show: true, lineColor: '#ccc', lineWidth: 1, length: 5, spacing: 5 }, axisLabel: { show: true, fontSize: 12, fontFamily: 'Arial', fontWeight: 'normal', fontColor: '#666', rotate: 0 }, boundaryGap: true }, yAxis: { axisLine: { show: true, lineColor: '#ccc', lineWidth: 1 }, axisTick: { show: true, lineColor: '#ccc', lineWidth: 1, length: 5, spacing: 5 }, axisLabel: { show: true, fontSize: 12, fontFamily: 'Arial', fontWeight: 'normal', fontColor: '#666', rotate: 0 }, splitLine: { show: true, lineColor: '#ccc', lineWidth: 1, type: 'solid' } }, series: chartData.series, animation: true, animationDuration: 1000, animationEasing: 'linear' }); chart.draw(); } }); ``` 2. 在小程序页面的`wxml`文件中添加`<canvas>`标签,例如: ```html <canvas id="chart" style="width:{{windowWidth}}px;height:200px;"></canvas> ``` 其中,`id`是`canvas`的唯一标识符,`style`属性用于设置`canvas`的宽度和高度。 3. 在小程序页面的`wxss`文件中设置`<canvas>`标签的样式,例如: ```css canvas { width: 100%; height: 100%; } ``` 4. 在小程序页面的`js`文件中实现`canvas`的绘图逻辑,例如: ```javascript const context = wx.createCanvasContext('chart'); context.beginPath(); context.setStrokeStyle('#000000'); context.setLineWidth(2); context.moveTo(0, 0); context.lineTo(100, 100); context.stroke(); context.draw(); ``` 以上就是在支付宝小程序使用uCharts绘制图表的基本流程,具体的图表样式和配置可以根据需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值