uniapp使用echarts

  1. 采用了renderjs来实现echarts,具体demo可以查看 renderjs-echarts-demo
  2. echarts的版本我选择了 echarts@3.8.4,高版本不支持具体点击哪一项
npm install echarts@3.8.4 --save

下载完后,我们提取 echarts.js 这个文件,放到 static/echarts这个目录下,然后把 node_modules 里面的 echarts 删掉
在这里插入图片描述
3. this.optionPie.series[0].label.normal.formatter 好像得写在echarts初始化的里面,不然会失效

<template>
	<view class="page-lay">
		<view class="echarts-layouts">
			<view @click="echarts.onClick" :prop="optionPie" :change:prop="echarts.updateEcharts" id="echartsPie"
				class="echarts">
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				optionPie: {},
			}
		},
		onLoad() {
			this.getData();
		},
		methods: {
			getData() {
				let resData = [{
						name: '回头客',
						value: 122,
						allmoney: '20%',
						color: '#5d99f4'
					},
					{
						name: '小区营销',
						value: 122,
						allmoney: '30%',
						color: '#65d164'
					},
					{
						name: '电商',
						value: 122,
						allmoney: '40%',
						color: '#ff6661'
					},
					{
						name: '活动推广',
						value: 122,
						allmoney: '50%',
						color: '#c92396'
					},
					{
						name: '材料商推广',
						value: 122,
						allmoney: '60%',
						color: '#face55'
					},
					{
						name: '其它',
						value: 122,
						allmoney: '70%',
						color: '#48e1da'
					}
				]

				let seriesData = resData.map(item => {
					return {
						name: item.name,
						value: item.value,
						percentage: item.percentage,
						itemStyle: {
							normal: {
								color: item.color
							}
						}
					}
				})

				let legendData = resData.map(item => item.name);

				this.optionPie = {
					title: {
						// text: '客户饼形分析图',
						// x: 'center',
						// top: 10
						text: '总客户',
						subtext: '102,234',
						x: 'center',
						y: 'center'
					},
					legend: {
						orient: 'horizontal',
						bottom: 'bottom',
						data: legendData
					},
					series: [{
						type: 'pie',
						radius: ['30%', '50%'],
						itemStyle: {
							borderRadius: 8
						},
						label: {
							normal: {
								show: true,
								position: 'outside',
								// formatter: '{c}'
							},
						},
						data: seriesData
					}, {
						type: 'pie',
						radius: ['30%', '50%'],
						itemStyle: {
							borderRadius: 8
						},
						label: {
							normal: {
								show: true,
								position: 'inside',
								// formatter: '{d}%'
							},
						},
						data: seriesData
					}]
				};
			},
			onViewClick(params) {
				console.log(params);
			},
		}
	}
</script>

<script module="echarts" lang="renderjs">
	let myChart, myParams;
	export default {
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
				script.src = 'static/echarts/echarts.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				console.log(echarts.version);
				myChart = echarts.init(document.getElementById('echartsPie'))
				this.optionPie.series[0].label.normal.formatter = (res) => res.name;
				this.optionPie.series[1].label.normal.formatter = (res) => res.data.allmoney;
				// 观测更新的数据在 view 层可以直接访问到
				myChart.setOption(this.optionPie);
				myChart.on('click', res => {
					myParams = res;
				})
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				/*在此加数据格式,需要将optionPie里面的formatter 注释掉,
				* 不然会报  You may have an infinite update loop in 
				* watcher with expression "optionPie" 错误
				*/
				newValue.series[0].label.normal.formatter = res => res.value;
				newValue.series[1].label.normal.formatter = res => {					
					return res.name + '\n' + res.data.allmoney + '\n' + res.percent;
				};
				myChart.setOption(newValue)
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				if (myParams) {
					ownerInstance.callMethod('onViewClick', {
						data: myParams.data,
						value: myParams.value,
						name: myParams.name,
						dataIndex: myParams.dataIndex
					})

					myParams = null;
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.page-lay {
		padding-top: 12px;
	}

	.echarts-layouts {
		width: calc(100vw - 24px);
		margin-left: 12px;
		background-color: white;
		border-radius: 5px;
	}

	/deep/ .echarts {
		width: calc(100vw - 24px);
		height: 360px;
	}
</style>

下面是效果图
在这里插入图片描述
下面是点击某一项的提示
在这里插入图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要在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 ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值