uniapp 使用echarts

注意需要下载一个echarts.min.js才能使用不然会报错。

<template>
	<view>
		<view class="echarts" :id="option.id" :prop="option" :change:prop="echarts.update" @click="echarts.onClick"></view>
	</view>
</template>

<script>
	export default {
		name: 'Echarts',
		props: {
			option: {
				type: Object,
				required: true
			}
		},
		created() {
			// 设置随机数id
			let t = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
			let len = t.length
			let id = ''
			for (let i = 0; i < 32; i++) {
				id += t.charAt(Math.floor(Math.random() * len))
			}
			this.option.id = id
		},
		methods: {
			/**
			 * renderjs内的点击事件,回调到父组件
			 * @param {Object} params
			 */
			onViewClick(params) {
				this.$emit('click', params)
			}
		}
	}
</script>

<script module="echarts" lang="renderjs">
	import echarts from '@/components/echarts/echarts.min.js'
	
	export default {
		data() {
			return {
				chart: null,
				clickData: null // echarts点击事件的值
			}
		},
		mounted() {
			this.init();
		},
		methods: {
			/**
			 * 初始化echarts
			 */
			init() {
				// 根据id初始化图表
				this.chart = echarts.init(document.getElementById(this.option.id))
				this.update(this.option)
				// echarts的点击事件
				this.chart.on('click', params => {
					// 把点击事件的数据缓存下来
					this.clickData = params
				})
			},
			/**
			 * 点击事件,可传递到外部
			 * @param {Object} event
			 * @param {Object} instance
			 */
			onClick(event, instance) {
				if (this.clickData) {
					// 把echarts点击事件相关的值传递到renderjs外
					instance.callMethod('onViewClick', {
						value: this.clickData.data,
						name: this.clickData.name,
						seriesName: this.clickData.seriesName
					})
					// 上次点击数据置空
					this.clickData = null
				}
			},
			/**
			 * 监测数据更新
			 * @param {Object} option
			 */
			update(option) {
				if (this.chart) {
					// 因App端,回调函数无法从renderjs外传递,故在此自定义设置相关回调函数
					if (option) {
						// tooltip
						if (option.tooltip) {
							// 判断是否设置tooltip的位置
							if (option.tooltip.positionStatus) {
								option.tooltip.position = this.tooltipPosition()
							}
							// 判断是否格式化tooltip
							if (option.tooltip.formatterStatus) {
								option.tooltip.formatter = this.tooltipFormatter(option.tooltip.formatterUnit, option.tooltip.formatFloat2, option.tooltip.formatThousands)
							}
						}
					}
					// 设置新的option
					this.chart.setOption(option, option.notMerge)
				}
			},
			/**
			 * 设置tooltip的位置,防止超出画布
			 */
			tooltipPosition() {
				return (point, params, dom, rect, size) => {
					// 其中point为当前鼠标的位置,size中有两个属性:viewSize和contentSize,分别为外层div和tooltip提示框的大小
					let x = point[0]
					let y = point[1]
					let viewWidth = size.viewSize[0]
					let viewHeight = size.viewSize[1]
					let boxWidth = size.contentSize[0]
					let boxHeight = size.contentSize[1]
					let posX = 0 // x坐标位置
					let posY = 0 // y坐标位置
					if (x >= boxWidth) { // 左边放的下
						posX = x - boxWidth - 1
					}
					if (y >= boxHeight) { // 上边放的下
						posY = y - boxHeight - 1
					}
					return [posX, posY]
				}
			},
			/**
			 * tooltip格式化
			 * @param {Object} unit 数值后的单位
			 * @param {Object} formatFloat2 是否保留两位小数
			 * @param {Object} formatThousands 是否添加千分位
			 */
			tooltipFormatter(unit, formatFloat2, formatThousands) {
				return params => {
					let result = ''
					unit = unit ? unit : ''
					for (let i in params) {
						if (i == 0) {
							result += params[i].axisValueLabel
						}
						let value = '--'
						if (params[i].data !== null) {
							value = params[i].data
							// 保留两位小数
							if (formatFloat2) {
								value = this.formatFloat2(value)
							}
							// 添加千分位
							if (formatThousands) {
								value = this.formatThousands(value)
							}
						}
						// #ifdef H5
						result += '\n' + params[i].seriesName + ':' + value + ' ' + unit
						// #endif
						
						// #ifdef APP-PLUS
						result += '<br/>' + params[i].marker + params[i].seriesName + ':' + value + ' ' + unit
						// #endif
					}
					return result
				}
			},
			/**
			 * 保留两位小数
			 * @param {Object} value
			 */
			formatFloat2(value) {
				let temp = Math.round(parseFloat(value) * 100) / 100
				let xsd = temp.toString().split('.')
				if (xsd.length === 1) {
					temp = (isNaN(temp) ? '0' : temp.toString()) + '.00'
					return temp
				}
				if (xsd.length > 1) {
					if (xsd[1].length < 2) {
						temp = temp.toString() + '0'
					}
					return temp
				}
			},
			/**
			 * 添加千分位
			 * @param {Object} value
			 */
			formatThousands(value) {
				if (value === undefined || value === null) {
					value = ''
				}
				if (!isNaN(value)) {
					value = value + ''
				}
				let re = /\d{1,3}(?=(\d{3})+$)/g
				let n1 = value.replace(/^(\d+)((\.\d+)?)$/, function(s, s1, s2) {
					return s1.replace(re, '$&,') + s2
				})
				return n1
			}
		}
	}
</script>

<style lang="scss" scoped>
	.echarts {
		width: 100%;
		height: 100%;
	}
</style>

页面引用

<template>
	<view>
		<view class="pr15-pl15">
			<echarts :option="option" style="height: 300px;" @click="echartsClick"></echarts>
		</view>
	</view>
</template>

<script>
	import echartsTitle from './components/echarts-title.vue'
	let vm = null;
	export default {
		data() {
			return {
				option: {
					// title: {
					//         text: '某站点用户访问来源',
					//         subtext: '纯属虚构',
					//         left: 'center'
					//     },
					tooltip: {
						trigger: 'item'
					},
					legend: {
						orient: 'horizontal',
						bottom: '30', //图例位置
						
						textStyle: {
							color: "#666666",
							fontSize:14
						}, //字体颜色
						icon: "circle", //图例形状设置
						itemWidth: 10, // 设置宽度
						itemHeight: 10, // 设置高度
						formatter: function(name) {
							let data = vm.option.series[0].data;
							let tarValue;
							let total = data.reduce((count, cur) => {
								return count.value + cur.value
							})
							console.log(total);
							for (let i = 0; i < data.length; i++) {
								// total += data[i].value;
								if (data[i].name == name) {
									tarValue = data[i].value;
								}
							}
							let v = tarValue;
							let p = Math.round(((tarValue / total) * 100));
							return `${name} (${p}%)`
						}
					},
					series: [{
						// name: '访问来源',
						type: 'pie',
						radius: '50%',
						center: ['50%', '50%'],
						data: [{
							value: 321,
							name: '包厢',
							itemStyle: {
								color: 'rgba(90,216,166,.85)'
								
							},
							label: {
								show: false
							},
							labelLine: {
								show: false
							}
						}, {
							value: 1048,
							name: '雅座',
							itemStyle: {
								color: 'rgba(91,143,249,.85)'
							},
							label: {
								show: false
							},
							labelLine: {
								show: false
							}
						}],
						emphasis: {
							itemStyle: {
							}
						}
					}]
				},
				columns2: [
					[
						'李四',
						'杨颖',

						'张三',
						'王五'
					]
				]
			};
		},
		created() {
			vm = this
		},
		methods: {
			echartsClick(params) {
				console.log('点击数据', params)
			},
		},
		components: {
			echarts
		}
	}
</script>

<style lang="scss" scoped>

</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在uni-app使用ECharts,你可以按照以下步骤进行操作: 1. 安装依赖:你可以通过运行命令`npm install echarts mpvue-echarts --save`来安装ECharts和mpvue-echarts插件。 2. 下载ECharts库文件:从ECharts官方网站下载echarts.min.js文件,并将其放置在uni-app项目的static文件夹中。 3. 创建公共组件:在uni-app项目的components文件夹中新建一个名为echarts.vue的公共组件。 4. 在需要使用ECharts的页面引入ECharts组件:在需要使用ECharts的页面中,通过`import`命令引入之前创建的公共组件echarts.vue。 5. 在页面中使用ECharts组件:在页面的template中使用echarts组件,并根据ECharts使用文档配置相关的图表选项和数据。 这样,你就可以在uni-app使用ECharts来展示图表了。需要注意的是,确保你的依赖安装正确,并且将echarts.min.js文件放置在正确的位置,以确保图表可以正常展示。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uniapp-Echarts.zip](https://download.csdn.net/download/qq_28584685/12660082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uniapp 使用 echarts实现图表](https://blog.csdn.net/qq_32195805/article/details/125791885)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值