开发中,有做到这样的echarts渲染,x轴和y轴的数据都是通过请求所获得的,而y轴所渲染的柱状图后面都有一个统一的灰色背景的柱状图,这个柱状图的值不好设置,我的最初的思路是在渲染之前通过数组的max方法获取到y轴数据中最大的一个然后再设置到这个灰色背景柱状上。但是这样有个问题就是我不知道给这个值加多少合适,所以我又有了一个思路,能不能获取到y轴的最大值,例如图中的1000为y轴的最大值,这样就能实现动态赋值给灰色背景柱状了。看上去灰色背景柱状图是永远跟y轴保持一致的。
效果图:
1.HTML部分
2.JS--axios获取请求
3.js--渲染echarts图
cldkEchart(xData2, data1) {
var barWidth = 17;
var chartDom = document.getElementById("measureLand");
var myChart = echarts.init(chartDom);
var option;
option = {
tooltip: {
trigger: "item",
},
grid: {
width: "90%",
height: "60%",
top: 35,
},
xAxis: {
data: xData2,
axisTick: {
show: false,
},
axisLine: {
show: true,
},
axisLabel: {
interval: 0, //强制文字产生间隔
textStyle: {
color: "#fff",
fontSize: 16,
},
margin: 15,
rotate: "38", //旋转角度
},
// axisLabel: {
// interval: 0,
// textStyle: {
// color: "#fff",
// fontSize: 16,
// },
// margin: 26, //刻度标签与轴线之间的距离。
// },
},
yAxis: {
name: "块",
nameTextStyle: {
fontSize: 16,
color: "#ffffff",
},
splitLine: {
show: false,
},
axisTick: {
show: false,
},
axisLine: {
show: true,
},
axisLabel: {
textStyle: {
color: "#fff",
fontSize: 16,
},
},
},
series: [
{
// 上半截柱子
type: "bar",
barWidth: barWidth,
barGap: "-100%",
z: 0,
itemStyle: {
color: "#163F7A",
opacity: 0.3,
},
tooltip: {
trigger: "none",
},
emphasis: {
itemStyle: {
color: "#163F7A",
},
},
data: new Array(xData2.length).fill(Math.max(...data1) + 10),
},
{
//下半截柱子
name: "2020",
type: "bar",
barWidth: barWidth,
barGap: "-100%",
itemStyle: {
//lenged文本
opacity: 1,
color: function (params) {
return new echarts.graphic.LinearGradient(
0,
0,
0,
1,
[
{
offset: 0,
color: "rgb(25, 106, 221)", // 0% 处的颜色
},
{
offset: 1,
color: "rgb(32, 189, 244)", // 100% 处的颜色
},
],
false
);
},
},
data: data1,
},
{
// 替代柱状图 默认不显示颜色,是最下方柱图(邮件营销)的value值 - 20
type: "bar",
barWidth: barWidth,
barGap: "-100%",
stack: "广告",
itemStyle: {
color: "transparent",
},
data: data1,
},
{
//上半截柱子顶部圆片
name: "",
type: "pictorialBar",
symbolSize: [barWidth, 10],
symbolOffset: [0, -8],
z: 12,
symbolPosition: "end",
itemStyle: {
color: "#163F7A",
opacity: 1,
},
tooltip: {
trigger: "none",
},
emphasis: {
itemStyle: {
color: "#163F7A",
},
},
data: new Array(xData2.length).fill(Math.max(...data1) + 10),
},
{
//下半截柱子顶部圆片
name: "",
type: "pictorialBar",
symbolSize: [barWidth, 10],
symbolOffset: [0, -5],
z: 12,
itemStyle: {
opacity: 1,
color: "rgb(26, 158, 230)",
},
symbolPosition: "end",
data: data1,
},
{
//下半截柱子底部圆片
name: "",
type: "pictorialBar",
symbolSize: [barWidth, 10],
symbolOffset: [0, 8],
z: 12,
itemStyle: {
opacity: 1,
color: "rgb(23, 97, 222)",
},
data: new Array(xData2.length).fill(1),
},
],
};
option && myChart.setOption(option);
//设置柱状背后阴影值为y轴最大值
var yMaxValue = myChart.getModel().getComponent("yAxis").axis
.scale._extent;
option.series[0].data = new Array(xData2.length).fill(yMaxValue[1]);
option.series[3].data = new Array(xData2.length).fill(yMaxValue[1]);
option && myChart.setOption(option);
},