.histogram-kandungan li{float:left;height:100%;text-align:center;position:relative;}
.histogram-box{position:relative;display:inline-block;height:100%;width:20px;}
.histogram-content li a{position:absolute;bottom:0;right:0;display:block;width:20px;font-size:0;line-height:0;}
.histogram-content li .histogram-name{position:absolute;bottom:-20px;left:0px;white-space:nowrap;display:inline-block;width:100%;font-size:12px;overflow:hidden; }
.histogram-y{position:absolute;left:-60px;top:-10px;font:12px/1.8 verdana,arial;}
.histogram-y li{text-align:right;width:55px;padding-right:5px;color:#333;}
.histogram-bg-line li div,.histogram-y li{height:30px;/*Kawal ketinggian sel dan ketinggian peratusan, supaya peratusan dan garisan dijajarkan*/}
Bahagian Js:
$(function(){
var dataArr={
"data":[
{"id":1,"name":"Baidu","per":"10"},
{"id":2,"name":"Tencent","per":"20"},
{"id":3,"name":"Sina","per":"10"},
{"id":4,"name":"NetEase","per":"44"},
{"id":5,"name":"Sohu","per":"50"},
{"id":5,"name":"Little Goby","per":"69"},
{"id":5,"name":"人人网","per":"72"},
{"id":5,"name":"iQiyi","per":"88"},
{"id":5,"name":"Qihoo 360","per":"92"},
{"id":5,"name":"Alibaba","per":"15"},
{"id":5,"name":"Alibaba","per":"10"}
] ]
};
histogram baharu().init(dataArr.data);
});
histogram(){
var controls={};
var bgColor=Array baharu("#666666","#21AA7C","#2277BB","#dc7644","#BBAA22","#AA22AA","#338800","#1099EE","#ffcc33" ,"#ED3810");
This.init=function(data,y){
setControls();
buildHtml(data,y);
}
Fungsi setControls(){
controls.histogramContainer=$("#histogram-container");
controls.histogramBgLineUL=controls.histogramContainer.children("div.histogram-bg-line");
controls.histogramContentUL=controls.histogramContainer.children("div.histogram-content");
controls.histogramY=controls.histogramContainer.children("div.histogram-y");
}
Function buildHtml(data,y){
var len=data.length,perArr=new Array(),maxNum,maxTotal,yStr='';
var contentStr='',bgLineStr='',bgLineAll='';
var widthPer=Math.floor(100/len);
minWidth=len*21 60;
controls.histogramContainer.css("min-width",minWidth "px");
untuk(var a=0;a
perArr[a]=parseInt(data[a]['per']);
maxNum=String(perArr.max());
Jika(maxNum.length>2){
var x=parseInt(maxNum.substr(maxNum.length-2,1)) 1;
maxTotal=Math.floor(parseInt(maxNum/100))*100 x*10;
}lain{
maxTotal=Math.floor(parseInt(maxNum/10))*10 10;
//bahagian paksi-y
Jika(y=="%"){
yStr ='
100%80%60%40%0%';
}lain{
var avg=maxTotal/5;
untuk(i=5;i>=0;i--){
yStr ='
' purata*i '';
}
}
//柱状条部分
untuk(var i=0;i
var per=Math.floor(parseInt(data[i]['per'])/maxTotal*100);
var n=Math.floor(parseInt(per)/10);
contentStr ='
';
contentStr ='' data[i]['name'] '';
contentStr ='';
bgLineStr ='
';
}
//背景方格部分
untuk(var j=0;j<5;j ){
bgLineAll ='
';
}
bgLineAll='
' bgLineAll '
';
contentStr='
';
yStr='
';
controls.histogramContainer.html(bgLineAll contentStr yStr);
controls.histogramContainer.css("height",controls.histogramContainer.height() "px");//主要是解决IE6中的问题
}
}
Array.prototype.max = function(){//最大值
kembalikan Math.max.apply({},this)
}