前端可视化封装柱图
- html
<template>
<div class="barcon">
<div class="barcon-chart" :style="chartStyle" ref="chart"></div>
</div>
</template>
- js
<script>
export default {
props: {
chartStyle: {
type: Object,
default: () => {
return {
width: '340px',
height: '200px',
};
},
},
barColors: {
type: Array,
default: () => {
return [
['#00C7EA', '#00C7EA'],
['#FFBF00', '#FFBF00'],
];
},
},
fontSize: {
type: Number,
default: 12,
},
rowData: {
type: Array,
default: () => {
return [];
},
},
maxValue: {
type: Number,
default: 100,
},
unit: {
type: String,
default: () => {
return '单位:人';
},
},
isStrip: {
type: Boolean,
default: () => {
return false;
},
},
barWidth: {
type: String,
default: () => {
return '16';
},
},
grid: {
type: Object,
default: () => {
return {
bottom: '15%',
left: '10%',
right: '0%',
top: '20%',
};
},
},
lableShow: {
type: Boolean,
default: () => {
return false;
},
},
lablePosition: {
type: String,
default: () => {
return 'right';
},
},
titleShow: {
type: Boolean,
default: () => {
return true;
},
},
axInterval: {
type: Number,
default: 0,
},
isXAxisSplitLine: {
type: Boolean,
default: () => {
return false;
},
},
isShowStripLegend: {
type: Boolean,
default: () => {
return true;
},
},
singleRowMaxWord: {
type: Number,
default: 0,
},
isSplitArea: {
type: Boolean,
default: () => {
return true;
},
},
},
data() {
return {
chart: null,
option: {},
xAxisData: null,
setTime: null,
};
},
methods: {
instantiation() {
let that = this;
this.chart = this.$echarts.init(this.$refs.chart);
this.option = {
legend: {
icon: 'rectangle',
right: '0%',
top: this.selectShow ? '5%' : null,
itemGap: 7,
itemHeight: 10,
itemWidth: 20,
textStyle: {
fontSize: that.fontSize,
color: '#BFEBFF',
},
show: false,
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
animation: false,
label: {
backgroundColor: '#505765',
fontSize: that.fontSize,
},
},
},
grid: this.grid,
xAxis: {
type: 'category',
data: [],
axisLine: {
show: this.isSplitArea,
lineStyle: {
color: '#BFEBFF',
},
},
splitLine: {
show: that.isXAxisSplitLine,
},
axisTick: {
show: false,
},
axisLabel: {
show: this.isSplitArea,
fontSize: that.fontSize,
interval: that.axInterval,
},
},
title: {
show: this.titleShow,
text: `${that.unit}`,
padding: [5, 0, 0, 4],
textStyle: {
color: '#BFECFF',
fontSize: that.fontSize,
fontWeight: 400,
},
top: 'top',
left: 'left',
},
yAxis: {
type: 'value',
name: '',
max: this.maxValue,
splitLine: {
show: false,
},
axisLine: {
lineStyle: {
color: '#BFEBFF',
},
},
splitArea: {
show: this.isSplitArea,
areaStyle: {
color: ['transparent', 'rgba(238,238,238,.1)'],
},
},
axisLabel: {
interval: 0,
fontSize: that.fontSize,
},
axisTick: {
show: false,
},
},
series: [],
};
},
rowDataChart() {
let legend = [];
let xAxisData = [];
this.rowData.forEach(val => {
legend.push(val['legend']);
xAxisData.push(val['dimension']);
});
legend = new Set(legend);
legend = Array.from(legend);
xAxisData = new Set(xAxisData);
xAxisData = Array.from(xAxisData);
var that = this;
legend.forEach((val, int) => {
that.option.series.push({
type: 'bar',
name: val,
data: [],
label: { show: this.lableShow, color: '#fff', position: this.lablePosition },
barWidth: that.barWidth,
itemStyle: {
color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 1, [
{ offset: 0, color: that.barColors[int][0] },
{ offset: 1, color: that.barColors[int][1] },
]),
},
});
for (let i = 0; i < this.rowData.length; i++) {
if (val == this.rowData[i]['legend']) {
that.option.series[int].data.push(this.rowData[i]['measure']);
}
}
});
this.option.legend.data = legend;
if (this.isStrip) {
this.option.yAxis.data = xAxisData;
this.option.legend.show = this.isShowStripLegend;
this.option.yAxis.axisLabel.formatter = value => {
if (this.singleRowMaxWord && value.length > this.singleRowMaxWord) {
let _valueToArr = value.split('');
let insertIndex = this.singleRowMaxWord;
let startItemNum = _valueToArr.length;
while (insertIndex <= value.length) {
let _index = _valueToArr.length - startItemNum;
_valueToArr.splice(insertIndex + _index, 0, '\n');
insertIndex += this.singleRowMaxWord;
}
let _val = '';
for (let item of _valueToArr) {
_val += item;
}
value = _val;
}
return value;
};
} else {
this.option.xAxis.data = xAxisData;
}
this.xAxisData = xAxisData.length;
if (this.isStrip) {
this.option.xAxis.type = 'value';
this.option.yAxis.type = 'category';
}
this.chart.setOption(this.option, true);
},
tooltipShuffling() {
clearInterval(this.setTime);
let count = 0;
this.setTime = setInterval(() => {
if (count == this.xAxisData) {
count = 0;
}
this.chart.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: count,
});
count++;
}, 1000);
},
},
mounted() {
this.instantiation();
this.rowDataChart();
this.tooltipShuffling();
},
beforeDestroy() {
clearInterval(this.setTime);
},
watch: {
rowData: {
handler(newV, oldV) {
this.instantiation();
this.rowDataChart();
this.tooltipShuffling();
},
deep: true,
},
},
};
</script>
- style基于scss
<style lang="scss" scoped>
.barcon {
position: relative;
width: 100%;
height: 100%;
}
.barcon-chart {
margin-top: 2px;
margin-bottom: 10px;
}
</style>