Echarts
柱状图
设置柱状图位置
{
"grid": {
"bottom": 25
}
}
设置柱状图 y 轴不显示小数
{
"yAxis": {
"type": "value",
"min": 0,
"minInterval": 1
}
}
设置柱状图顶部是圆角:
const option = {
series: [
{
data: data,
type: 'bar',
barWidth: '10',
itemStyle: {
color: '#3F8CFF',
borderRadius: [20, 20, 0, 0] // 设置圆角
}
}
]
};
横向柱状图,类似进度条
const initPie = (pieData: any) => {
if (!pieRef.current) return;
const pieInstance = echarts.init(pieRef.current);
let charts = {
name: pieData.map((e: any) => e.name),
valueData: pieData.map((e: any) => e.value)
};
let pData = charts.valueData;
let color = ['#3F8CFF', '#FFD023', '#43D1A2', '#A5D63F', '#00BAAD'];
let lineY = [];
let lineT = [];
for (let i = 0; i < charts.name.length; i++) {
let x = i;
if (x > 1) {
x = 2;
}
let data = {
name: charts.name[i],
color: color[x],
value: pData[i],
barGap: '-100%',
itemStyle: {
normal: {
show: true,
color: color[x],
barBorderRadius: 10
},
emphasis: {
shadowBlur: 15,
shadowColor: 'rgba(0, 0, 0, 0.1)'
}
}
};
let data1 = {
value: pData[2],
itemStyle: {
color: '#F5F5F5',
barBorderRadius: 10
}
};
lineY.push(data);
lineT.push(data1);
}
const option = {
title: {
show: false
},
grid: {
borderWidth: 0,
top: '5%',
left: '5%',
right: '10%',
bottom: '0'
},
color: color,
yAxis: [
{
type: 'category',
inverse: true,
axisTick: {
show: false
},
axisLine: {
show: false
},
axisLabel: {
show: false,
inside: false
},
data: charts.name
},
{
type: 'category',
inverse: true,
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
show: true,
inside: false,
verticalAlign: 'bottom',
lineHeight: '1',
textStyle: {
color: '#b3ccf8',
fontSize: '14'
}
},
splitArea: {
show: false
},
splitLine: {
show: false
},
data: charts.valueData
}
],
xAxis: {
type: 'value',
axisTick: {
show: false
},
axisLine: {
show: false
},
splitLine: {
show: false
},
axisLabel: {
show: false
}
},
series: [
{
name: 'total',
type: 'bar',
zlevel: 1,
barGap: '-100%',
barWidth: '10px',
data: lineT,
legendHoverLink: false
},
{
name: 'bar',
type: 'bar',
zlevel: 2,
barWidth: '10px',
data: lineY,
label: {
normal: {
color: '#b3ccf8',
show: true,
position: [0, '-18px'],
textStyle: {
fontSize: 16
},
formatter: function (a: any) {
return `{color4|${a.name}}`;
},
rich: {
color4: {
color: '#111827'
}
}
}
}
}
]
};
pieInstance.setOption(option);
return pieInstance;
};
折线图
强制显示所有 x 轴标签
const option = {
xAxis: {
type: 'category',
name: '单位:万元',
axisLabel: {
interval: 0,
rotate: 45,
textStyle: {
color: '#8A8A8A'
},
formatter: function (value: any) {
if (value.length > 5) {
return `${value.slice(0, 5)}...`;
}
return value;
}
},
data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
}
};
- interval 设置成 0 强制显示所有标签。如果设置为 1,表示『隔一个标签显示一个标签』,如果值为 2,表示隔两个标签显示一个标签,以此类推。
- 如果需要设置文字超出显示省略号,可以设置 formatter
- 倾斜角度可以根据数据项来设置,比如数据长度超过 5 就倾斜:
rotate: data.length > 5 ? 30 : 0