uniapp横向堆叠柱状图的实现(H5+CSS)

        小程序项目中有个页面,需要展示横向堆叠柱状图,到插件市场找了一圈,只看到几种charts插件,而且只有纵向堆叠柱状图和横向柱状图的demo,没看到横向堆叠柱状图的实现方案。只能自己来写了,下面是效果预览。

完整代码如下:

<template>
	<view class="">
		<view class="is-flex is-justify-between is-align-center has-pad">
			<view class="is-flex is-justify-start is-align-center">
				<view class="has-mgr-10">
					a
				</view>
				<view :style="{backgroundColor:a}" style="width:40rpx;height:40rpx;">

				</view>
			</view>
			<view class="is-flex is-justify-start is-align-center">
				<view class="has-mgr-10">
					b
				</view>
				<view :style="{backgroundColor:b}" style="width:40rpx;height:40rpx;">

				</view>
			</view>
			<view class="is-flex is-justify-start is-align-center">
				<view class="has-mgr-10">
					c
				</view>
				<view :style="{backgroundColor:c}" style="width:40rpx;height:40rpx;">

				</view>
			</view>
			<view class="is-flex is-justify-start is-align-center">
				<view class="has-mgr-10">
					d
				</view>
				<view :style="{backgroundColor:d}" style="width:40rpx;height:40rpx;">

				</view>
			</view>
		</view>

		<!-- 未找到横向堆叠柱状图的范例,故使用css自行开发 -->
		<view v-if="maxNum" class="has-pad">
			<view class="is-flex is-justify-between is-align-center has-mgt-10" v-for="(item,index) in dataList" :key="index">
				<view class="" style="width:80rpx;">
					{{item.name}}
				</view>
				<view class="is-flex is-justify-start is-align-center" style="width:calc(100% - 200rpx);height: 40rpx;">
					<view class="" style="height: 100%;" :style="{width:100*item.a/maxNum+'%',backgroundColor:a}">

					</view>
					<view class="" style="height: 100%;" :style="{width:100*item.b/maxNum+'%',backgroundColor:b}">

					</view>
					<view class="" style="height: 100%;" :style="{width:100*item.c/maxNum+'%',backgroundColor:c}">

					</view>
					<view class="" style="height: 100%;" :style="{width:100*item.d/maxNum+'%',backgroundColor:d}">

					</view>
				</view>
				<view class="" style="width:80rpx;">
					{{item.total}}
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	let t = null;
	export default {
		components: {},
		data() {
			return {
				a: '#308CFF',
				b: '#09bb07',
				c: '#e64340',
				d: '#ffff00',

				maxNum: 0, 
				dataList: [{
					name: "A",
					a: 20,
					b: 10,
					c: 40,
					d: 30,
					total: 100
				}, {
					name: "B",
					a: 10,
					b: 0,
					c: 40,
					d: 0,
					total: 50
				}, {
					name: "C",
					a: 20,
					b: 10,
					c: 10,
					d: 25,
					total: 65
				}]
			}
		},
		onLoad() {
			t = this;

            //遍历dataList,取total最大值
			let maxNum = 0;
			for (let i of t.dataList) {
				if (i.total > maxNum) {
					maxNum = i.total;
				}
			}
			t.maxNum = maxNum;
		},
		methods: {

		}
	}
</script>

<style>
	.is-align-center {
		align-items: center;
	}

	.is-justify-start {
		justify-content: flex-start;
	}

	.is-justify-between {
		justify-content: space-between;
	}

	.is-flex {
		display: flex;
	}

	.has-pad {
		padding: 20rpx 30rpx !important;
	}

	.has-mgr-10 {
		margin-right: 20rpx !important;
	}
	
	.has-mgt-10 {
		margin-top: 20rpx !important;
	}
</style>

核心代码是<view style="height: 100%;" :style="{width:100*item.a/maxNum+'%',backgroundColor:a}">,意思是根据当前子项的数值与最大总数的比值来绘制子项图形width。

### 回答1: 好的,横向堆叠柱状图可以使用Python中的Matplotlib库来实现。以下是一个简单的示例代码: ```python import matplotlib.pyplot as plt import numpy as np # 数据 labels = ['A', 'B', 'C', 'D'] values1 = [20, 35, 30, 25] values2 = [10, 25, 20, 30] values3 = [5, 15, 10, 20] # 每个柱子的位置 pos = np.arange(len(labels)) # 绘制柱状图 plt.barh(pos, values1, color='blue', edgecolor='black') plt.barh(pos, values2, color='green', edgecolor='black', left=values1) plt.barh(pos, values3, color='orange', edgecolor='black', left=values1+values2) # 设置标签和标题 plt.yticks(pos, labels) plt.xlabel('Value') plt.title('Horizontal Stacked Bar Chart') # 显示图形 plt.show() ``` 运行以上代码,可以得到一个横向堆叠柱状图。你可以根据自己的数据和需求进行修改和调整。 ### 回答2: 横向堆叠柱状图在Python中可以使用matplotlib库来绘制。首先需要导入所需的库,包括matplotlib.pyplot和numpy: ```python import matplotlib.pyplot as plt import numpy as np ``` 接下来,我们定义一些示例数据来绘制横向堆叠柱状图。假设我们有三个类别的数据,每个类别有五个子类别,这样我们就有三个柱状图,其中每个柱状图有五个柱子。 ```python # 定义类别和子类别的名称 categories = ['A', 'B', 'C'] subcategories = ['X', 'Y', 'Z', 'W', 'V'] # 生成示例数据 data = np.random.rand(len(categories), len(subcategories)) ``` 接下来,我们使用plt.barh函数绘制横向堆叠柱状图。使用for循环来遍历每个类别,并在每个类别上绘制子类别的柱状图。 ```python # 获取每个类别的索引 indices = np.arange(len(categories)) # 初始化堆叠的起始位置 bottom = np.zeros(len(categories)) # 遍历每个子类别,绘制堆叠柱状图 for i, subcategory in enumerate(subcategories): plt.barh(indices, data[:,i], left=bottom) bottom += data[:,i] # 更新堆叠的起始位置 ``` 最后,我们进行一些绘图的设置和标注,包括添加标题、轴标签和图例等。 ```python # 设置轴标签和标题 plt.xlabel('Value') plt.ylabel('Category') plt.title('Stacked Horizontal Bar Chart') # 设置类别的刻度和标签 plt.yticks(indices, categories) # 添加图例 plt.legend(subcategories) # 显示图形 plt.show() ``` 上述的代码将生成一个横向堆叠柱状图,其中每个柱状图代表一个类别的子类别。每个柱状图的高度和颜色根据数据而定。 ### 回答3: 横向堆叠柱状图是一种可视化数据的方式,通常用于展示多个类别的数据在一个整体中的占比或比较。 在Python中,可以使用一些数据可视化库来实现横向堆叠柱状图的绘制,比如Matplotlib和Seaborn。下面是使用Matplotlib库绘制横向堆叠柱状图的代码示例: ```python import matplotlib.pyplot as plt # 数据 categories = ['A', 'B', 'C', 'D'] # 类别 data1 = [10, 20, 30, 40] # 数据1 data2 = [15, 25, 35, 45] # 数据2 data3 = [5, 15, 25, 35] # 数据3 # 绘图 plt.barh(categories, data1, label='Data 1') plt.barh(categories, data2, left=data1, label='Data 2') plt.barh(categories, data3, left=[d1 + d2 for d1, d2 in zip(data1, data2)], label='Data 3') # 设置图例和标题 plt.legend() plt.title('Horizontal Stacked Bar Chart') # 展示图形 plt.show() ``` 在这个示例中,我们首先定义了需要展示的类别(categories)和多个数据(data1,data2,data3)。然后使用`plt.barh`函数分别绘制了不同数据的水平柱状图,并使用`left`参数设置了每个柱状图的起始位置,以实现堆叠效果。最后,通过`plt.legend()`设置图例和`plt.title()`设置标题,最终调用`plt.show()`展示图形。 以上就是用Python绘制横向堆叠柱状图的方法。当然,这只是一个简单的示例,实际应用中你还可以根据自己的需求进一步调整和美化图形。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝莲花-爸爸去哪儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值