一个横向的echarts表格
<template>
<div
:ref="widgetConfig.echartContainerId"
class="doubleBarChartContainer"
></div>
</template>
<script>
import {
addDomResizeListener,
removeResizeListener,
} from "@/bi-core/utils/screenUtil";
export default {
name: "DoubleBarChart",
data() {
return {
chartData: [],
chart: null,
echartOption: {},
widgetConfig: {
echartContainerId: "bar-chart",
option: {},
},
};
},
watch: {
chartData() {
this.setData();
},
},
mounted() {
this.$nextTick(() => {
this.initEchart();
this.setData();
addDomResizeListener(
this.$refs[this.widgetConfig.echartContainerId],
() => {
this.chart.resize();
}
);
});
},
beforeDestroy() {
if (!this.chart) {
return;
}
this.chart.dispose();
this.chart = null;
removeResizeListener(
this.$refs[this.widgetConfig.echartContainerId],
function () {}
);
},
methods: {
initEchart() {
const opt = { index: 0 };
this.chart = this.$echarts.init(
this.$refs[this.widgetConfig.echartContainerId]
);
this.echartOption = {
grid: {
top: 20,
bottom: 20,
right: 60,
left: -70,
containLabel: true,
},
legend: {
x: "center",
y: "1%",
orient: "horizontal",
itemWidth: 10,
itemHeight: 10,
textStyle: {
color: "#FFFFFF",
fontSize: 14,
fontFamily: "PingFang SC",
fontWeight: "400",
},
selectedMode: false, // 不允许点击
data: [
{
name: "接口",
itemStyle: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 1,
y2: 1,
colorStops: [
{
offset: 0,
color: "rgba(22, 90, 183, 0)", // 0% 处的颜色
},
{
offset: 0.5,
color: "rgba(181, 255, 228, 0.5)", // 50% 处的颜色
},
{
offset: 1,
color: "rgba(181, 255, 228, 1)", // 100% 处的颜色
},
],
global: false, // 缺省为 false
},
},
},
{
name: "库表",
itemStyle: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 1,
y2: 1,
colorStops: [
{
offset: 0,
color: "rgba(22, 90, 183, 0)", // 0% 处的颜色
},
{
offset: 0.5,
color: "rgba(135, 219, 255, 0.5)", // 0% 处的颜色
},
{
offset: 1,
color: "rgba(135, 219, 255, 1)", // 100% 处的颜色
},
],
global: false, // 缺省为 false
},
},
},
{
name: "文档",
itemStyle: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 1,
y2: 1,
colorStops: [
{
offset: 0,
color: "rgba(255, 138, 0, 0)", // 0% 处的颜色
},
{
offset: 0.5,
color: "rgba(255, 229, 135, 0.5)", // 0% 处的颜色
},
{
offset: 1,
color: "rgba(255, 229, 135, 1)", // 100% 处的颜色
},
],
global: false, // 缺省为 false
},
},
},
],
},
xAxis: {
show: true,
splitNumber: 6,
//data:[0,200,400,600,800,1000],
min: 0,
max: 1000,
axisLine: {
show: true,
lineStyle: {
color: "rgba(230, 247, 255, 0.2)",
opacity: 0.2,
},
},
},
yAxis: [
{
show: true,
inverse: true,
data: [],
axisLine: {
show: true,
//onZero: false,
},
splitLine: {
show: false,
},
axisTick: {
show: false,
},
// boundaryGap: true,
//scale: true,
axisLabel: {
interval: 0,
color: "#E6F7FF",
align: "left",
margin: 100,
formatter: function (value, index) {
if (opt.index === 0 && index < 3) {
return (
"{idx" +
index +
"|" +
(1 + index) +
"}" +
"{title" +
index +
"|" +
value +
"}"
);
} else if (opt.index !== 0 && index + opt.index < 9) {
return (
"{idx|0" +
(1 + index + opt.index) +
"} {title|" +
value +
"}"
);
} else {
return (
"{idx|" +
(1 + index + opt.index) +
"} {title|" +
value +
"}"
);
}
},
rich: {
idx0: {
color: "rgba(255, 255, 255, 1)",
//backgroundColor: 'rgba(0, 0, 0, 0.6)',
// width: 24,
// height: 15,
lineHeight: 15,
// borderColor: 'rgba(255, 193, 0, 1)',
// borderWidth: 0.5,
// borderType: 'solid',
align: "center",
verticalAlign: "middle",
fontFamily: "Noto Sans SC",
fontSize: "14px",
padding: [5, 8],
},
idx1: {
color: "rgba(255, 255, 255, 1)",
//backgroundColor: 'rgba(0, 0, 0, 0.6)',
// width: 24,
// height: 15,
// borderColor: 'rgba(255, 153, 0, 1)',
// borderWidth: 0.5,
// borderType: 'solid',
align: "center",
verticalAlign: "middle",
fontFamily: "Noto Sans SC",
fontSize: "14px",
padding: [5, 8],
},
idx2: {
color: "rgba(255, 255, 255, 1)",
//backgroundColor: 'rgba(0, 0, 0, 0.6)',
// width: 24,
// height: 15,
lineHeight: 15,
// borderColor: 'rgba(255, 114, 70, 1)',
// borderWidth: 0.5,
// borderType: 'solid',
align: "center",
verticalAlign: "middle",
fontFamily: "Noto Sans SC",
fontSize: "14px",
padding: [5, 8],
},
idx: {
color: "rgba(255, 255, 255, 1)",
//backgroundColor: 'rgba(0, 0, 0, 1)',
borderWidth: 0.5,
// width: 24,
// height: 15,
borderType: "solid",
align: "center",
verticalAlign: "middle",
fontFamily: "Noto Sans SC",
fontSize: "14px",
padding: [5, 8],
},
title: {
fontFamily: "Noto Sans SC",
fontSize: "14px",
color: "rgba(208, 222, 238, 1)",
padding: 5,
},
title0: {
fontFamily: "Noto Sans SC",
fontSize: "14px",
color: "rgba(255, 231, 205, 1)",
padding: 5,
},
title1: {
fontFamily: "Noto Sans SC",
fontSize: "14px",
color: "rgba(208, 222, 238, 1)",
padding: 5,
},
title2: {
fontFamily: "Noto Sans SC",
fontSize: "14px",
color: "rgba(208, 222, 238, 1)",
padding: 5,
},
},
},
},
{
show: false,
position: "center",
inverse: true,
data: [],
axisLine: {
show: false,
},
splitLine: {
show: false,
},
axisTick: {
show: false,
},
axisLabel: {
show: false,
interval: 0,
// align: 'left',
margin: 10,
fontSize: 14,
fontFamily: "Noto Sans SC",
formatter: function (value, index) {
return "{value" + "|" + value + "}" + "{unit|" + "个" + "}";
},
rich: {
value: {
color: "rgba(208, 222, 238, 1)",
align: "right",
verticalAlign: "middle",
fontFamily: "PangMenZhengDao",
fontSize: "18px",
lineHeight: 18,
},
unit: {
color: "rgba(110, 136, 167, 1)",
align: "left",
verticalAlign: "middle",
fontFamily: "Noto Sans SC",
fontSize: "14px",
lineHeight: 14,
padding: 5,
},
},
},
},
],
series: [
{
name: "接口",
type: "bar",
yAxisIndex: 0,
data: [],
barWidth: 4,
zlevel: 1,
itemStyle: {
color: (val) => {
return new this.$echarts.graphic.LinearGradient(
0,
0,
1,
0,
[
{
offset: 0,
color: "rgba(181, 255, 228, 0.1)",
},
{
offset: 0.5,
color: "rgba(181, 255, 228, 0.5)",
},
{
offset: 1,
color: "rgba(181, 255, 228, 1)",
},
],
false
);
},
},
},
{
name: "库表",
type: "bar",
yAxisIndex: 0,
data: [],
barGap: "1px",
barWidth: 5,
zlevel: 1,
itemStyle: {
color: (val) => {
return new this.$echarts.graphic.LinearGradient(
0,
0,
1,
0,
[
{
offset: 0,
color: "rgba(135, 219, 255, 0.2)",
},
{
offset: 0.5,
color: "rgba(135, 219, 255, 0.5)",
},
{
offset: 1,
color: "rgba(135, 219, 255, 1)",
},
],
false
);
},
},
},
{
name: "文档",
type: "bar",
yAxisIndex: 0,
data: [],
barGap: "1px",
barWidth: 5,
zlevel: 1,
itemStyle: {
color: (val) => {
return new this.$echarts.graphic.LinearGradient(
0,
0,
1,
0,
[
{
offset: 0,
color: "rgba(255, 229, 135, 0.1)",
},
{
offset: 0.5,
color: "rgba(255, 229, 135, 0.5)",
},
{
offset: 1,
color: "rgba(255, 229, 135, 1)",
},
],
false
);
},
},
},
{
name: "背景",
type: "bar",
barWidth: 23,
yAxisIndex: 1,
// barGap: '-100%',
data: [],
label: {
normal: {
show: false,
position: "right",
// formatter: (e) => {
// if (e.dataIndex < 3) {
// return (
// "{value" +
// "|" +
// e.data.displayData +
// "}" +
// "{unit|" +
// "个" +
// "}"
// );
// } else {
// return (
// "{value2" +
// "|" +
// e.data.displayData +
// "}" +
// "{unit2|" +
// "个" +
// "}"
// );
// }
// },
// rich: {
// value: {
// color: "rgba(208, 222, 238, 1)",
// align: "right",
// verticalAlign: "middle",
// fontFamily: "PangMenZhengDao",
// fontSize: "16px",
// lineHeight: 18,
// },
// unit: {
// color: "rgba(110, 136, 167, 1)",
// align: "left",
// verticalAlign: "middle",
// fontFamily: "PangMenZhengDao",
// fontSize: "14px",
// lineHeight: 14,
// padding: 5,
// },
// value2: {
// color: "rgba(208, 222, 238, 0.7)",
// align: "right",
// verticalAlign: "middle",
// fontFamily: "PangMenZhengDao",
// fontSize: "16px",
// lineHeight: 18,
// },
// unit2: {
// color: "rgba(110, 136, 167, 0.7)",
// align: "left",
// verticalAlign: "middle",
// fontFamily: "PangMenZhengDao",
// fontSize: "14px",
// lineHeight: 14,
// padding: 5,
// },
// },
fontSize: 16,
distance: 25,
fontFamily: "PangMenZhengDao",
},
},
itemStyle: {
normal: {
color: "#152541",
},
},
},
// {
// name: "订阅数背景",
// type: "bar",
// barWidth: 5,
// yAxisIndex: 1,
// data: [],
// barGap: "2px",
// label: {
// normal: {
// show: true,
// position: "right",
// distance: 25,
// fontSize: 16,
// fontFamily: "PangMenZhengDao",
// formatter: (e) => {
// // return e.data.displayData + '个'
// if (e.dataIndex < 3) {
// return (
// "{value" +
// "|" +
// e.data.displayData +
// "}" +
// "{unit|" +
// "个" +
// "}"
// );
// } else {
// return (
// "{value2" +
// "|" +
// e.data.displayData +
// "}" +
// "{unit2|" +
// "个" +
// "}"
// );
// }
// },
// rich: {
// value: {
// color: "rgba(208, 222, 238, 1)",
// align: "right",
// verticalAlign: "middle",
// fontFamily: "PangMenZhengDao",
// fontSize: "16px",
// lineHeight: 18,
// },
// unit: {
// color: "rgba(110, 136, 167, 1)",
// align: "left",
// verticalAlign: "middle",
// fontFamily: "PangMenZhengDao",
// fontSize: "14px",
// lineHeight: 14,
// padding: 5,
// },
// value2: {
// color: "rgba(208, 222, 238, 0.7)",
// align: "right",
// verticalAlign: "middle",
// fontFamily: "PangMenZhengDao",
// fontSize: "16px",
// lineHeight: 18,
// },
// unit2: {
// color: "rgba(110, 136, 167, 0.7)",
// align: "left",
// verticalAlign: "middle",
// fontFamily: "PangMenZhengDao",
// fontSize: "14px",
// lineHeight: 14,
// padding: 5,
// },
// },
// },
// },
// itemStyle: {
// normal: {
// color: "#152541",
// },
// },
// },
],
};
this.chart.setOption(this.echartOption);
this.echartOption = this.chart.getOption();
},
setData() {
if (this.chart) {
const AxisData = [];
const SeriesData1 = [];
const SeriesData2 = [];
const SeriesData3 = [];
const BarBackgroundData1 = [];
const BarBackgroundData2 = [];
for (let i = 0; i < this.chartData.length; i++) {
AxisData.push(this.chartData[i].name);
SeriesData1.push(this.chartData[i].value1);
SeriesData2.push(this.chartData[i].value2);
SeriesData3.push(this.chartData[i].value3);
}
if (Math.max(...SeriesData1) === Math.max(...SeriesData2)) {
for (let j = 0; j < this.chartData.length; j++) {
BarBackgroundData1.push({
value: Math.max(...SeriesData1) * 1.2,
displayData: this.chartData[j].value1,
});
BarBackgroundData2.push({
value: Math.max(...SeriesData1) * 1.2,
displayData: this.chartData[j].value2,
});
}
} else if (Math.max(...SeriesData1) > Math.max(...SeriesData2)) {
for (let j = 0; j < this.chartData.length; j++) {
BarBackgroundData1.push({
value: Math.max(...SeriesData1) * 1.2,
displayData: this.chartData[j].value1,
});
BarBackgroundData2.push({
value: Math.max(...SeriesData1) * 1.2,
displayData: this.chartData[j].value2,
});
}
} else if (Math.max(...SeriesData1) < Math.max(...SeriesData2)) {
for (let j = 0; j < this.chartData.length; j++) {
BarBackgroundData1.push({
value: Math.max(...SeriesData2) * 1.2,
displayData: this.chartData[j].value1,
});
BarBackgroundData2.push({
value: Math.max(...SeriesData2) * 1.2,
displayData: this.chartData[j].value2,
});
}
}
this.echartOption.yAxis[0].data = AxisData;
this.echartOption.yAxis[1].data = []; //没有用到
this.echartOption.series[0].data = SeriesData1;
this.echartOption.series[1].data = SeriesData2;
this.echartOption.series[3].data = BarBackgroundData1;
// this.echartOption.series[3].data = BarBackgroundData2;
this.echartOption.series[2].data = SeriesData3;
this.chart.setOption(this.echartOption);
}
},
},
};
</script>
<style scoped lang="scss">
.doubleBarChartContainer {
position: relative;
width: 100%;
height: 100%;
top: 0;
left: 0;
box-sizing: border-box;
// border: 1px solid yellow;
}
</style>