溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

javascript怎么實(shí)現(xiàn)動(dòng)態(tài)顯示顏色塊的報(bào)表效果

發(fā)布時(shí)間:2021-04-13 14:08:10 來源:億速云 閱讀:201 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)javascript怎么實(shí)現(xiàn)動(dòng)態(tài)顯示顏色塊的報(bào)表效果的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

利用html的顏色塊動(dòng)態(tài)展示數(shù)據(jù)

<style type="text/css">
 *{
 padding: 0;
 margin: 0;
 }
 .tubiao,.jihua,.shiji,.riqi{
 width: 100%;
 overflow: hidden;
 margin-top: 10px;
 }
 .left{
 width: 10%;
 float: left;
 text-align: center;
 height: 25px;
 line-height: 25px;
 }
 .right{
 width: 90%;
 float: right;
 height: 25px;
 }
 span {
 width: 5%;
 height: 100%;
 text-align: center;
 display: inline-block;
 }
 </style>
<body>
 <div class="tubiao">
 <div class="jihua">
 <div class="left">計(jì)劃</div>
 <!--計(jì)劃span存放的地方-->
 <div class="right plan"></div>
 </div>
 <div class="shiji">
 <div class="left">實(shí)際</div>
 <!--實(shí)際span存放的地方-->
 <div class="right act"></div>
 </div>
 <div class="riqi" id="day_id">
 <!--日期存放的地方-->
 <div class="right day"></div>
 </div>
 </div>
 <script type="text/javascript">
 var temp1="0-0.5-2-2-2-2-1-1";//計(jì)劃耗時(shí)(塊的單位寬度)
 var temp2="1-1-2-1-2-0-0-0";//實(shí)際耗時(shí)(塊的單位寬度) 
 var temp3="5/19-5/20-5/21-5/22-5/23-5/24-5/25-5/26-5/27-5/28";//綜合日期
 var temp=temp1+"~"+temp2+"~"+temp3; 
 var plan = document.getElementsByClassName("plan")[0];
 var act = document.getElementsByClassName("act")[0];
 var day = document.getElementsByClassName("day")[0];
 var num = 20;//創(chuàng)建多少個(gè)格
 load_first(temp);
 //分割數(shù)據(jù)和添加色塊操作
 function load_first(temp){
 var demo=temp.split("~");
 var d1=demo[0].split("-");//計(jì)劃耗時(shí)(塊的單位寬度)數(shù)組
 var d2=demo[1].split("-");////實(shí)際耗時(shí)(塊的單位寬度)數(shù)組
 var d3=demo[2].split("-");//綜合日期數(shù)組
 for(var i=0;i<d3.length;i++){
 time_span(d3[i]);
 }
 //alert("6:"+d1.length+"---"+"3:"+d2.length);
 //alert("d3.length:"+d3.length);
 for(var i=0;i<d1.length;i++){ 
 add_span(d1[i],d2[i],i);
 }
 document.getElementById("day_id").style.marginLeft="-30px"; 
 } 
 //新增顏色塊,a為計(jì)劃顏色塊寬度,b為實(shí)際顏色塊寬度
 function add_span(a,b,i){
 //創(chuàng)建span塊
 var span1 = document.createElement("span");
 var span2 = document.createElement("span");
 //定義隨機(jī)底色
 var spa = "rgba(" + rnd(0,255)+ "," + rnd(0,255)+ ","+ rnd(0,255)+ ","+ rnd(0.5,1) +")";//每一個(gè)顏色隨機(jī)出來
 if(i==0){
 span1.style.backgroundColor = "000000";
 //clientWidth是對(duì)象看到的寬度(不含邊線,即border)
 span1.style.width = (plan.clientWidth/num*a) + "px";//計(jì)劃的每一格的寬度
 //插入節(jié)點(diǎn)span1至plan
 plan.appendChild(span1);
 span2.style.backgroundColor = "000000";
 span2.style.width = (plan.clientWidth/num*b) + "px";//實(shí)際的每一格的寬度
 act.appendChild(span2);
 }else{
 //alert("a:"+a+"b:"+b+"i:"+i);
 if(a=="0"){
 span1.style.backgroundColor = "000000"; 
 span1.style.width = (plan.clientWidth/num*a) + "px";//計(jì)劃的每一格的寬度
 //插入節(jié)點(diǎn)span1至plan
 plan.appendChild(span1);
 }else{
 span1.style.backgroundColor = spa;
 //clientWidth是對(duì)象看到的寬度(不含邊線,即border)
 span1.style.width = (plan.clientWidth/num*a) + "px";//計(jì)劃的每一格的寬度
 //插入節(jié)點(diǎn)span1至plan
 plan.appendChild(span1);
 }
 if(b=="0"){
 span2.style.backgroundColor = "000000";
 span2.style.width = (plan.clientWidth/num*b) + "px";//實(shí)際的每一格的寬度
 act.appendChild(span2); 
 }else{
 span2.style.backgroundColor = spa;
 span2.style.width = (plan.clientWidth/num*b) + "px";//實(shí)際的每一格的寬度
 act.appendChild(span2);
 } 
 }
 } 
 //日期的數(shù)據(jù)插入
 function time_span(time){
 //創(chuàng)建span塊
 var span = document.createElement("span"); 
 span.style.width = (plan.clientWidth/num*1) + "px";//每一個(gè)span的寬度 
 span.innerHTML = "" + time;
 day.appendChild(span);
 }
 //隨機(jī)函數(shù)
 function rnd(min,max){
 return Math.round(Math.random()*(max - min)+min); 
 }
 function QueryData() {
 var displayStyle = "1";
 $.ajax({
 type: "post",
 url: "Test.aspx",
 dataType: "text",
 data: { "DispalyStyle": displayStyle },
 error: function (XMLHttpRequest, textStatus, errorThrown) {
 alert(errorThrown + XMLHttpRequest.responseText);
 },
 success: function (json) {
 try {
 load_first(json);
 }
 catch (e) { }
 }
 });
 }
 //QueryData();
 </script>
 </body>

感謝各位的閱讀!關(guān)于“javascript怎么實(shí)現(xiàn)動(dòng)態(tài)顯示顏色塊的報(bào)表效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI