一、效果图如下:
二、代码配置
结构如下:
(1)父组件
<template>
<div class="page-con">
<div class="main-con">
<item></item>
</div>
</div>
</template>
<script>
import item from '../bigdata/components/item.vue'
export default {
components: {
item
}
}
</script>
<style lang="scss" scoped>
.page-con {
width: 100%;
height: 100%;
.main-con {
width: 40%;
height: 33%;
}
}
</style>
(2)子组件
<template>
<div class="bar">
<div ref="volumn" class="volume" />
</div>
</template>
<script>
import echarts from "echarts";
export default {
data() {
return {
myChart: null,
};
},
mounted() {
// 获取数据。
if (this.$refs.volumn) {
this.reloadChart();
// 自适应浏览器。
window.addEventListener("resize", () => {
this.reloadChart();
});
}
},
// 组件销毁。
beforeDestroy() {
this.disposeChart();
},
methods: {
drawChart() {
this.myChart = this.$echarts.init(this.$refs.volumn);
// 绘制图表
let myDate = [
"樱桃",
"麒麟瓜",
"阳光玫瑰葡萄",
"草莓",
"贝贝南瓜",
"辣椒",
"架豆王",
];
let numA = [806, 910, 990, 1062, 805, 1051, 970];
let numB = [6, 6, 6, 6, 6, 6, 6];
var option = {
tooltip: {
trigger: "axis",
axisPointer: {
type: "shadow",
textStyle: {
color: "#fff",
},
},
},
grid: {
borderWidth: 0,
top: "10%",
bottom: "12%",
textStyle: {
color: "#fff",
},
},
calculable: true,
xAxis: [
{
type: "category",
axisLine: {
lineStyle: {
color: "#ffffff",
},
},
splitLine: {
show: false,
},
axisTick: {
show: false,
},
splitArea: {
show: false,
},
axisLabel: {
interval: 0,
},
axisLine: {
show: false,
lineStyle: {
color: "#ffffff",
},
},
data: myDate,
},
],
yAxis: {
type: "value",
axisLine: {
show: false,
lineStyle: {
color: "#1C82C5",
},
},
splitLine: {
show: true,
lineStyle: {
color: "rgba(4, 187, 255, .2)",
type: "dashed",
},
},
axisLabel: {
textStyle: {
color: "#fff",
fontSize: "13px",
},
},
axisTick: {
show: false,
},
},
series: [
{
name: "",
type: "bar",
stack: "总量",
barMaxWidth: 24,
barGap: "10%",
itemStyle: {
normal: {
show: true,
color: new echarts.graphic.LinearGradient(
0,
0,
0,
1,
[
{
offset: 0,
color: "#015CC7",
},
{
offset: 1,
color: "#031E3E",
},
],
false
),
barBorderRadius: 0,
label: {
show: true,
position: "top",
color: "#56C1F8",
formatter: function (p) {
return p.value > 0 ? p.value : "";
},
},
},
},
data: numA,
},
{
name: "",
type: "bar",
stack: "总量",
barMaxWidth: 24,
barGap: "10%",
itemStyle: {
normal: {
show: true,
color: new echarts.graphic.LinearGradient(
0,
0,
0,
1,
[
{
offset: 0,
color: "#0CCCF5",
},
{
offset: 1,
color: "#0CCCF5",
},
],
false
),
barBorderRadius: 0,
},
},
data: numB,
},
{
// 分隔
type: "pictorialBar",
tooltip: {
show: false,
},
symbolRotate: "-45",
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
{
offset: 0,
color: "#023D75",
},
{
offset: 1,
color: "#023D75",
},
]),
},
},
symbolRepeat: "fixed",
symbolMargin: -8,
symbol: "rect",
symbolClip: true,
symbolSize: [2, 28],
symbolPosition: "start",
symbolOffset: [-1, 10],
data: numA,
z: 66,
animationEasing: "elasticOut",
},
],
};
this.myChart.setOption(option);
},
// 字体自适应。
fontSize(res) {
const clientWidth =
window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth;
if (!clientWidth) return;
const fontSize = 40 * (clientWidth / 1920);
return res * fontSize;
},
// 销毁图表。
disposeChart() {
if (this.myChart) {
this.myChart.dispose();
}
},
// 重新加载图表。
reloadChart() {
this.disposeChart();
this.drawChart();
},
},
};
</script>
<style lang="scss" scoped>
.bar {
width: 100%;
height: 100%;
.volume {
width: 100%;
height: 100%;
}
}
</style>