封装柱状图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>柱状图</title>
</head>
<body>
<div id="container"></div>
</body>
<script src="bar.js"></script>
<script>
    var obj = {
        id: "container",//必填
        width:500,//容器宽度
        height:300,//容器高度
        borderType:1,/*1:全包 2:半包  3:无边框*/
        borderColor:"red",
        title:"GDP分布",
        titleColor:"red",
        lineTipColor:"red",/*分割线的提示颜色*/
        lines:4,/*分割线数量*/
        lineColor:"red",
        barWidth:40,/*每个柱状图的宽度*/
        data: [    /*必填*/
            {
                label: "青岛",
                num: 270,
                color:"blue",
                labelColor:"blue"
            },
            {
                label: "烟台",
                num: 200,
                color:"red",
                labelColor:"red"
            },
            {
                label: "威海",
                num: 150,
                color:"orange",
                labelColor:"orange"
            }]
    };
    drawBar(obj);
</script>
</html>

引入的外部js是封装好的柱状图的js html文件中数据和id值是使用者必须要给的,其它的内容可以填写也可以不填(默认为封装js默认的样式),外部js在下面给出,

function drawBar(obj) {
    /*ctrl+f查找*/
    var id = obj.id;
    var width = obj.width || 500;//没有的话是默认500
    var height = obj.height || 300;
    var lineColor = obj.lineColor || "red";
    var lineTipColor = obj.lineTipColor || lineColor;
    var lines = obj.lines || 4;
    var title = obj.title || "";
    var titleColor = obj.titleColor || "red";
    var data = obj.data;
    var dataLength = data.length;
    var borderType = obj.borderType || 1;
    var borderColor = obj.borderColor || "red";
    var barWidth = obj.barWidth || 40;
    var container = document.getElementById(id);
    container.style.position = "relative";
    container.style.width = width + "px";
    container.style.height = height + "px";
    container.style.left = "50px";
    container.style.top = "50px";
    if (borderType === 1) {
        container.style.border = "1px solid " + borderColor;
    } else if (borderType === 2) {
        container.style.borderLeft = "1px solid " + borderColor;
        container.style.borderBottom = "1px solid " + borderColor;
    }
    var maxNum = 0;
    for (var i = 0; i < dataLength; i++) {
        if (data[i].num >= maxNum) {
            maxNum = data[i].num;
        }
    }
    maxNum = maxNum * 1.1;
    var perHeight = height / maxNum;
    var barSpace = (width - barWidth * dataLength) / (dataLength + 1);
    /*1px代表的数据大小*/
    /*柱状图*/
    for (var i = 0; i < dataLength; i++) {
        var label = document.createElement("div");
        label.innerText = data[i].label;
        label.style.cssText = "position:absolute;bottom:-21px;width:100%;text-align:center";
        label.style.color = data[i].labelColor;
        var subTitle = document.createElement("div");
        subTitle.innerText = data[i].num;
        subTitle.style.cssText = "width:100%;text-align:center;position:absolute;top:-21px";
        subTitle.style.color = data[i].labelColor;
        var div = document.createElement("div");
        div.className = "bar";
        div.style.cssText = "height:0px;position:absolute;bottom:0px;transition:all 3s";
        div.style.width = barWidth + "px";
        div.style.backgroundColor = data[i].color || "red";
        div.style.left = barSpace * (i + 1) + (barWidth * i) + "px";
        div.appendChild(label);
        div.appendChild(subTitle);
        container.appendChild(div);
    }
    /*画线*/
    var lineSpace = height / (lines + 1);
    for (var i = 0; i < lines; i++) {
        var line = document.createElement("div");
        line.style.cssText = "height:1px;width:100%;position:absolute;z-index:-1";
        line.style.backgroundColor = lineColor;
        line.style.bottom = lineSpace * (i + 1) + "px";
        container.appendChild(line);
    }
    /*画lineTip*/
    var tipNumSpace = maxNum / (lines + 1);
    for (var i = 0; i < lines + 2; i++) {
        var tip = document.createElement("div");
        tip.style.cssText = "position:absolute;left:-45px;width:40px;text-align:center;transform:rotate(60deg)";
        tip.style.bottom = (lineSpace * i - 8) + "px";
        tip.style.color = lineTipColor;
        tip.innerText = Math.round(tipNumSpace * i * 100) / 100;
        /*保留小数点后两位*/
        container.appendChild(tip);
    }
    /*添加过渡效果*/
    var bars = document.getElementsByClassName("bar");
    setTimeout(function () {
        for (var i = 0; i < dataLength; i++) {
            bars[i].style.height = data[i].num * perHeight + "px";
        }
    }, 500);

    var titleT = document.createElement("div");
    titleT.innerHTML = title;
    titleT.style.cssText = "position:absolute;right:0px;top:-30px";
    titleT.style.color = titleColor;
    container.appendChild(titleT);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值