按天数显示的柱状图

这篇博客详细介绍了如何使用Echarts库创建一个条形图,包括图表的网格设置、提示框定位、X轴与Y轴的配置,以及不同数据段采用不同颜色的动态渲染。数据来源于一个名为`materailStorageList`的列表,涵盖了多个时间区间和不同的数据系列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

var option = {

    legend: {
        bottom: "0",
        textStyle: { color: "#65ABE7", fontSize: fontSize }
    },

    "grid": {
        "top": "15%",
        "bottom": "25%",
        "right": "3%",
        "left": "10%"
    },
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'cross',
            label: {
                backgroundColor: '#6a7985'
            }
        },
        position: function (point, params, dom, rect, size) {
            // 鼠标坐标和提示框位置的参考坐标系是:以外层div的左上角那一点为原点,x轴向右,y轴向下
            // 提示框位置
            var x = 0; // x坐标位置
            var y = 0; // y坐标位置

            // 当前鼠标位置
            var pointX = point[0];
            var pointY = point[1];

            // 外层div大小
            // var viewWidth = size.viewSize[0];
            // var viewHeight = size.viewSize[1];

            // 提示框大小
            var boxWidth = size.contentSize[0];
            var boxHeight = size.contentSize[1];

            // boxWidth > pointX 说明鼠标左边放不下提示框
            if (boxWidth > pointX) {
                x = pointX + 10;
            } else { // 左边放的下
                x = pointX - boxWidth - 10;
            }

            // boxHeight > pointY 说明鼠标上边放不下提示框
            if (boxHeight > pointY) {
                y = 5;
            } else { // 上边放得下
                y = pointY - boxHeight;
            }

            return [x, y];

        },
    },
    xAxis: {

        data: timeName,
        axisLine: {
            show: false
        },
        axisTick: {
            show: false,
            lineStyle: {
                color: "#65ABE7",
                width: 1
            },
        },
        axisLabel: { //X轴标签
            show: true,
            interval: 0,
            rotate: 12,
            textStyle: {
                color: '#65ABE7', //字体颜色
                fontSize: fontSize //字体大小
            }
        }


    },
    yAxis: [

        {
            type: 'value',
            min: 0,
            //max: 100,
            //interval: 1,
            axisLabel: {
                formatter: '{value}',
                textStyle: {
                    color: '#65ABE7', //字体颜色
                    fontSize: fontSize //字体大小
                }

            },
            axisTick: {
                show: false
            },
            axisLine: {
                show: false
            },
            splitLine: { //Y轴分隔符
                show: true,
                lineStyle: {
                    color: '#65ABE7',
                }

            }
        },


    ],
    series: [

        {
            name: 'A01',
            type: 'bar',
            data: oneNum,
            itemStyle: {
                normal: {
                    //color: '#00d98b',
                    //这里是颜色
                    color: function (params) {
                        console.log("params", params);
                        if (params.name == "360天以上" || params.name == "181~360天") {
                            return "red";
                        } else if (params.name == "121~180天") {
                            return "yellow";
                        } else {
                            return "#00d98b";
                        }
                    },
                    label: {
                        show: false, //显示
                        formatter: "{c}",
                        position: 'top', //在上方
                        textStyle: { //文字样式
                            color: '#fff',
                            fontSize: fontSize
                        }
                    }
                }
            },

        },

        {
            name: 'D07',
            type: 'bar',
            data: twoNum,
            itemStyle: {
                normal: {
                    color: function (params) {
                        console.log("params", params);
                        if (params.name == "360天以上" || params.name == "181~360天") {
                            return "red";
                        } else if (params.name == "121~180天") {
                            return "yellow";
                        } else {
                            return "#00d98b";
                        }
                    },
                    label: {
                        show: false, //显示
                        formatter: "{c}",
                        position: 'top', //在上方
                        textStyle: { //文字样式
                            color: '#fff',
                            fontSize: fontSize
                        }
                    }
                }
            },

        },

        {
            name: 'W09',
            type: 'bar',
            data: threeNum,
            itemStyle: {
                normal: {
                    color: function (params) {
                        console.log("params", params);
                        if (params.name == "360天以上" || params.name == "181~360天") {
                            return "red";
                        } else if (params.name == "121~180天") {
                            return "yellow";
                        } else {
                            return "#00d98b";
                        }
                    },
                    label: {
                        show: false, //显示
                        formatter: "{c}",
                        position: 'top', //在上方
                        textStyle: { //文字样式
                            color: '#fff',
                            fontSize: fontSize
                        }
                    }
                }
            },

        },

        {
            name: 'W29',
            type: 'bar',
            data: fourNum,
            itemStyle: {
                normal: {
                    color: function (params) {
                        console.log("params", params);
                        if (params.name == "360天以上" || params.name == "181~360天") {
                            return "red";
                        } else if (params.name == "121~180天") {
                            return "yellow";
                        } else {
                            return "#00d98b";
                        }
                    },
                    label: {
                        show: false, //显示
                        formatter: "{c}",
                        position: 'top', //在上方
                        textStyle: { //文字样式
                            color: '#fff',
                            fontSize: fontSize
                        }
                    }
                }
            },

        },

        {
            name: 'A21',
            type: 'bar',
            data: fiveNum,
            itemStyle: {
                normal: {
                    color: function (params) {
                        console.log("params", params);
                        if (params.name == "360天以上" || params.name == "181~360天") {
                            return "red";
                        } else if (params.name == "121~180天") {
                            return "yellow";
                        } else {
                            return "#00d98b";
                        }
                    },
                    label: {
                        show: false, //显示
                        formatter: "{c}",
                        position: 'top', //在上方
                        textStyle: { //文字样式
                            color: '#fff',
                            fontSize: fontSize
                        }
                    }
                }
            },

        },

        {
            name: 'D27',
            type: 'bar',
            data: sixNum,
            itemStyle: {
                normal: {
                    color: function (params) {
                        console.log("params", params);
                        if (params.name == "360天以上" || params.name == "181~360天") {
                            return "red";
                        } else if (params.name == "121~180天") {
                            return "yellow";
                        } else {
                            return "#00d98b";
                        }
                    },
                    label: {
                        show: false, //显示
                        formatter: "{c}",
                        position: 'top', //在上方
                        textStyle: { //文字样式
                            color: '#fff',
                            fontSize: fontSize
                        }
                    }
                }
            },

        }

    ]
};
    var timeName=materailStorageList.map(x=>x.NAME);
    var oneNum=materailStorageList.map(x=>x.A01_NUM);
    var twoNum=materailStorageList.map(x=>x.D07_NUM);
	var threeNum=materailStorageList.map(x=>x.W09_NUM);
	var fourNum=materailStorageList.map(x=>x.W29_NUM);
	var fiveNum=materailStorageList.map(x=>x.A21_NUM);
	var sixNum=materailStorageList.map(x=>x.D27_NUM);
//伪数据
materailStorageList:[
        {NAME:"1~7天(正产)",A01_NUM:474,D07_NUM:611,W09_NUM:577,W29_NUM:563,A21_NUM:258,D27_NUM:458},
        {NAME:"8~15天",A01_NUM:656,D07_NUM:511,W09_NUM:477,W29_NUM:548,A21_NUM:582,D27_NUM:752},
        {NAME:"16~21天",A01_NUM:567,D07_NUM:411,W09_NUM:377,W29_NUM:785,A21_NUM:465,D27_NUM:564},
        {NAME:"22~30天",A01_NUM:500,D07_NUM:311,W09_NUM:677,W29_NUM:256,A21_NUM:854,D27_NUM:285},
        {NAME:"31~60天",A01_NUM:400,D07_NUM:677,W09_NUM:345,W29_NUM:485,A21_NUM:235,D27_NUM:853},
        {NAME:"61~90天",A01_NUM:300,D07_NUM:777,W09_NUM:567,W29_NUM:256,A21_NUM:876,D27_NUM:253},
        {NAME:"91~120天",A01_NUM:100,D07_NUM:300,W09_NUM:432,W29_NUM:845,A21_NUM:582,D27_NUM:458},
        {NAME:"121~180天",A01_NUM:121,D07_NUM:455,W09_NUM:784,W29_NUM:256,A21_NUM:458,D27_NUM:762},
        {NAME:"181~360天",A01_NUM:647,D07_NUM:666,W09_NUM:257,W29_NUM:385,A21_NUM:358,D27_NUM:965},
        {NAME:"360天以上",A01_NUM:878,D07_NUM:366,W09_NUM:874,W29_NUM:415,A21_NUM:956,D27_NUM:584}
    ]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值