$(関数(){
var dataArr={
"データ":[
{"id":1,"name":"Baidu","per":"10"},
{"id":2,"name":"Tencent","per":"20"},
{"id":3,"name":"シーナ","per":"10"},
{"id":4,"name":"NetEase","per":"44"},
{"id":5,"name":"ソフ","per":"50"},
{"id":5,"name":"コハゼ","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"}
]]
};
新しいヒストグラム().init(dataArr.data);
});
関数ヒストグラム(){
var コントロール ={};
var bgColor=new Array("#666666","#21AA7C","#2277BB","#dc7644","#BBAA22","#AA22AA","#338800","#1099EE","#ffcc33" ,"#ED3810");
This.init=function(data,y){
setControls();
buildHtml(data,y);
}
関数 setControls(){
コントロール.histogramContainer=$("#ヒストグラムコンテナ");
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");
}
関数 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");
for(var a=0;a
perArr[a]=parseInt(data[a]['per']);
maxNum=String(perArr.max());
If(maxNum.length>2){
var x=parseInt(maxNum.substr(maxNum.length-2,1)) 1;
maxTotal=Math.floor(parseInt(maxNum/100))*100 x*10;
}その他{
maxTotal=Math.floor(parseInt(maxNum/10))*10 10;
//y軸部分
If(y=="%"){
yStr ='
100%
80%
60%
40%
;20%
0%
';
}その他{
var avg=maxTotal/5;
for(i=5;i>=0;i--){
yStr = '
' avg*i '';
}
}
//柱状条部分
for(var i=0;i
var per=Math.floor(parseInt(data[i]['per'])/maxTotal*100);
var n=Math.floor(parseInt(per)/10);
contentStr ='
';
contentStr =''データ[i]['名前'] '';
contentStr = '';
bgLineStr ='
';
}
//背景方格部分
for(var j=0;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(){//最大值
return Math.max.apply({},this)
}