思路:网上搜寻了许久,最终参照官网的斜切样式制作出来了,这种方法采用的是遮盖原有柱状图顶端造成视觉上看起来是斜切的效果。目前这是我能想到最好的办法了,也欢迎大家提供其他的方法。
参照echerts图表网:
echarts图表集
效果图:
完整源代码:
<template>
<!-- 横向柱状图 -->
<div class="index" ref="lineStyle"></div>
</template>
<script>
export default {
name: "index",
components: {},
props: {
obj: {
type: Object,
default() {
return {
ydata: ["第一天", "第二天", "第三天"],
legend: ["双床房", "大床房", "商务房"],
barData: [
{
name: "双床房",
data: [10, 5, 20]
},
{
name: "大床房",
data: [3, 6, 9]
},
{
name: "商务房",
data: [5, 4, 3]
}
],
legendDate: "center" //对齐方式
};
}
},
// 渐变色从深色到浅色
colors: {
type: Object,
default() {
return {
colors: [
{ color1: "#0F949B", color2: "#1bf3fe" },
{ color1: "#1794FF", color2: "#79b2f8" },
{ color1: "#19C278", color2: "#90f9bb" }
]
};
}
}
},
data() {
return {
serData: [],
myChart1: ""
};
},
computed: {},
watch: {
data: {
handler() {
this.$nextTick(() => {
this.init();
});
},
deep: true
}
},
created() {},
mounted() {
this.init();
},
beforeDestroy() {},
methods: {
init() {
// console.log(this.obj.legendDate);
if (this.myChart1) this.myChart1.dispose();
let ref = this.$refs.lineStyle;
this.myChart1 = echarts.init(ref);
let arr = []; //存储房间数数组
let arrData = []; //大床房数据
let list = []; //双床房数据
let room = []; //存储房间数据
this.obj.barData.forEach((item, i) => {
// 计算房间总数
arr.push(this.sum(item.data));
room.push(item.name);
this.serData.push(
{
name: item.name,
type: "bar",
stack: "all",
// color: item.color, //柱状图颜色
barWidth: 14,
data: item.data,
showBackground: true,
backgroundStyle: {
color: "#1344791a"
}
},
//斜切效果设置
{
type: "pictorialBar",
data: item.data.map((items, j) => {
if (i > 0 && item.name == room[i]) {
arrData.push(items + list[j]);
if (item.name == "商务房") {
return items + arrData[j];
}
return items + list[j];
} else {
list.push(items);
return items;
}
}),
xAxisIndex: 0,
yAxisIndex: 0,
zlevel: 99,
symbol:
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po1792d3dn3aSyjbAYDC/1wFp3UwLU2IsrYTLfA12Qx6FglMLkHdwlqSy8MUwvzorG8iDYutavKOeetUvSQDuGnXWTyhbCUjB8MiV+EgdXw/Ti7IaCgdLk+p7+JFqtRzPwWB4GW1f6zSbJ8RbHWUObpZ3tVDYRVoLhJYdMJ/F7d/C293xJKpM3+0yPYImXxDdf1kQ2g60rPaHh7DprB081lQ2EYTC85IhJkt7eDh418ywO1JI3e9Ddgr1JHFw0Cb+7pea7NPz2zHutAUyTyi2E9WB4qXvAuTW/MgmA0UyehoKBRgp9Y+FueXqTtPBIXoSj+SSV/pgxL/usAf4DUH06aIViPR0AAAAASUVORK5CYII=",
symbolRotate: 0,
symbolSize: [15, 14],
symbolPosition: "end",
symbolOffset: [8, 0],
itemStyle: {
color: "#1280eb",
shadowColor: "#1280eb",
shadowBlur: 10
},
tooltip: {
show: false
}
}
);
// });
});
//渐变色操作
let colorList = []; //存储渐变完成后的颜色
this.colors.colors.forEach(item => {
colorList.push(
new echarts.graphic.LinearGradient(0, 0, 1, 0, [
{ offset: 0, color: item.color1 },
{ offset: 1, color: item.color2 }
])
);
});
const option = {
// 颜色
color: colorList, //存储颜色
//图表选择栏
legend: {
top: "5%",
left: this.obj.legendDate,
orient: "horizontal", //图例方向【horizontal/vertical】
itemHeight: 12, //修改icon图形大小
itemWidth: 12, //修改icon图形大小
selectedMode: false, //不允许点击图例
itemGap: 30,
textStyle: {
fontSize: 14,
color: "#fff"
},
data: this.obj.legend,
tooltip: {
show: false
}
},
//图标离容器的距离
grid: {
left: "5%",
right: "3%",
bottom: "10%",
top: "20%",
//是否包含坐标轴
containLabel: true
},
//x坐标轴
xAxis: [
{
type: "value",
axisTick: {
show: false
},
splitLine: {
show: true,
lineStyle: {
color: "rgba(255,255,255,.3"
}
},
boundaryGap: true,
axisLine: {
show: true,
lineStyle: {
// 属性lineStyle控制线条样式
color: "#476579c1",
width: 1,
type: "solid"
}
},
axisLabel: {
color: "#fff",
fontSize: 14
}
}
],
//y坐标轴
yAxis: [
{
type: "category",
name: "",
data: this.obj.ydata,
splitLine: {
// show: false
},
inverse: true,
axisTick: {
show: false
},
axisLine: {
// show: true,
lineStyle: {
// 属性lineStyle控制线条样式
color: "#476579c1",
width: 1,
type: "solid"
}
},
axisLabel: {
textStyle: {
color: "#fff",
fontSize: 14
}
}
}
],
// 数据展示区域
series: this.serData
};
// console.log(option);
this.myChart1.setOption(option);
},
// 数组求和
sum(arr) {
return eval(arr.join("+"));
}
}
};
</script>
<style lang="less" scoped>
.index {
width: 467px;
height: 300px;
// background-color: #13447900;
}
</style>