vue3 使用echarts(堆叠柱形图)

文章描述了如何在Vue应用中使用ECharts库创建一个堆叠柱状图,展示了具体的数据配置和样式设置,包括轴标签、颜色和交互选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 使用堆叠柱形图,上方显示具体数值,最终效果如下

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
//  按需引入 echarts
import * as echarts from 'echarts'

const main = ref() // 使用ref创建虚拟DOM引用,使用时用main.value
onMounted(() => {
	init()
})

function init() {
	var dataBeast = [10, 20, 30, 20]
	var dataBeauty = [0, 50, 26, 30]
	var xAxisData = ['第一季度', '第二季度', '第三季度', '第四季度']

	// 基于准备好的dom,初始化echarts实例
	var myChart = echarts.init(main.value)
	// 指定图表的配置项和数据
	var option = {
		color: ['#7785FF', '#FAC032', '#11565d'],
		tooltip: {
			show: true,
			trigger: 'axis',
			axisPointer: {
				type: 'cross',
				crossStyle: {
					color: '#ddd',
				},
			},
		},
		grid: {
			left: 5,
			right: 1,
			top: 42,
			bottom: 0,
			containLabel: true,
		},
		xAxis: {
			show: true,
			axisLabel: {
				margin: 12,
				textStyle: {
					color: '#606266',
					align: 'center',
					fontSize: 14,
					fontFamily: ' Microsoft YaHei UI-Regular, Microsoft YaHei UI',
					fontWeight: 400,
				},
			},
			axisLine: {
				//y轴线的颜色以及宽度
				show: true,
				lineStyle: {
					color: '#C0C4CC',
				},
			},
			// x轴突出的
			axisTick: {
				show: false,
			},
			data: xAxisData,
		},
		yAxis: [
			{
				type: 'value',
				axisLabel: {
					textStyle: {
						fontSize: 12,
						color: '#909399',
						fontFamily: 'MiSans-Regular, MiSans',
						fontWeight: 400,
					},
				},
				axisLine: {
					//y轴线的颜色以及宽度
					show: true,
					lineStyle: {
						color: '#C0C4CC',
					},
				},
			},
		],

		series: [
			{
				type: 'bar',
				name: '有隐患',
				stack: '人数',
				data: dataBeast,
				barWidth: '32%',
				margin: 32,
				label: {
					emphasis: {
						textStyle: {
							color: '#fff',
						},
					},
				},
				itemStyle: {
					normal: {
						borderColor: '#fff',
						borderWidth: 1,
					},
				},
			},
			{
				type: 'bar',
				name: '无隐患',
				stack: '人数',
				data: dataBeauty,
				itemStyle: {
					normal: {
						borderColor: '#fff',
						borderWidth: 1,
					},
				},
				label: {
					show: true,
					position: 'top',
					offset: [0, -4],
					textStyle: {
						fontSize: 16,
						fontFamily: 'DM Sans-Bold, DM Sans',
						fontWeight: 'bold',
						color: '#131414',
					},
					formatter: function (val) {
						let data = dataBeast[val.dataIndex]
						return val.data + '/' + data
					},
				},
			},
		],
	}
	// 使用刚指定的配置项和数据显示图表。
	myChart.setOption(option)
	window.addEventListener('resize', () => {
		myChart.resize()
	})
}
</script>

<template>
	<div ref="main" class="main" style=""></div>
</template>

<style scoped>
.main {
	width: 100%;
	height: 100%;
}
</style>

您好!对于堆叠3D柱状图,您可以使用ECharts来实现。下面是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ECharts 堆叠3D柱状图示例</title> <!-- 引入 ECharts.js --> <script src="https://cdn.jsdelivr.net/npm/echarts@5.1.2/dist/echarts.min.js"></script> </head> <body> <!-- 创建一个具有指定宽度和高度的 div 容器 --> <div id="chart" style="width: 600px;height: 400px;"></div> <script> // 初始化 ECharts 实例 var myChart = echarts.init(document.getElementById(&#39;chart&#39;)); // 配置项 var option = { tooltip: { trigger: &#39;axis&#39;, axisPointer: { type: &#39;shadow&#39; } }, legend: { data: [&#39;系列1&#39;, &#39;系列2&#39;, &#39;系列3&#39;] }, grid3D: {}, xAxis3D: { type: &#39;category&#39;, data: [&#39;柱1&#39;, &#39;柱2&#39;, &#39;柱3&#39;, &#39;柱4&#39;, &#39;柱5&#39;] }, yAxis3D: {}, zAxis3D: {}, dataset: { dimensions: [&#39;x&#39;, &#39;y&#39;, &#39;z&#39;], source: [ [0, 0, 0, 2], [0, 1, 0, 4], [0, 2, 0, 6], [0, 3, 0, 8], [0, 4, 0, 10], [1, 0, 0, 3], [1, 1, 0, 6], [1, 2, 0, 9], [1, 3, 0, 12], [1, 4, 0, 15], [2, 0, 0, 4], [2, 1, 0, 8], [2, 2, 0, 12], [2, 3, 0, 16], [2, 4, 0, 20] ] }, series: [ { type: &#39;bar3D&#39;, stack: &#39;stack&#39;, encode: { x: &#39;x&#39;, y: &#39;y&#39;, z: &#39;z&#39; } } ] }; // 使用刚指定的配置项和数据显示图表 myChart.setOption(option); </script> </body> </html> ``` 在上面的代码中,我们使用ECharts提供的`bar3D`系列来绘制3D堆叠柱状图。您可以根据自己的需求修改数据源和配置项来适应您的实际情况。希望对您有所帮助!如果您有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值