先上效果图
做了两种维度不同的进度图,一个是月度,一个是精确到每个时辰
实现代码:js+css
html代码:
<div class="mainPanl">
<div class="panl_li">
<div class="machineTitle">XXX</div>
</div>
<div class="dataPanl">
<div id="machinePanl"></div>
<div id="gantPanl"></div>
<div style="clear: both;"></div>
</div>
</div>
js代码:
var dataJson = []
var startDay = 1;
var endDay = 3;
$(function () {
initGant();
$(window).resize(function (){
// initGant();
})
});
function initGant(){
//初始化表头
initTitle({startDay:1,endDay:3});
//初始化数据
dataJson = initData();
//初始化甘特图
initGrid();
}
//initTitle
function initTitle(params){
var dayLength = parseInt(params.endDay)-parseInt(params.startDay)+1;
var width = 603*dayLength;
var str = ""
for(var i = parseInt(params.startDay);i<=parseInt(params.endDay);i++){
if(i%2!=0){
str+='<div class="title_all bg_blue"><div class="title_day">'+i+'号</div><div class="title_hours">';
}else{
str+='<div class="title_all bg_pink"><div class="title_day">'+i+'号</div><div class="title_hours">';
}
var hourStr = "";
for(var j = 1;j<=24;j++){
hourStr+='<div class="title_hour">'+j+'</div>'
}
str+=hourStr;
str+='</div></div>'
}
$('.mainPanl').css('width',width+100+"px");
$('.panl_li').css('width',width+100+"px");
$('#gantPanl').css('width',width-10+"px");
$('.panl_li').append(str);
$('#gantPanl').append("<div class='linePnal'></div><div style='clear:both'></div>");
}
function initData(){
var dataJson = [
{
codeId:'E1-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'7.50R16KCA651/AT651/F27 AF',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'576',
ywcl:'576',
},
{
codeId:'E2-LR',
startDay:'01',
startHour:'01',
endDay:'02',
endHour:'15',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'7.50R16KLS200',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'350',
ywcl:'350',
},
{
codeId:'E2-LR',
startDay:'02',
startHour:'16',
endDay:'02',
endHour:'21',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'收尾换膜',
type:'0',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'245',
ywcl:'245',
},
{
codeId:'E2-LR',
startDay:'02',
startHour:'22',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'215/50R16 95V EV100',
type:'2',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'250',
ywcl:'250',
},
{
codeId:'E3-R',
startDay:'02',
startHour:'03',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'195/50R15 86H iGREE',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'270',
ywcl:'270',
},
{
codeId:'E3-L',
startDay:'02',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'215/65R15 100H CO',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'270',
ywcl:'270',
},
{
codeId:'E4-LR',
startDay:'01',
startHour:'07',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'165/50R15 73V',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'650',
ywcl:'650',
},
{
codeId:'E5-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'165/50R15 73V',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'650',
ywcl:'650',
},
{
codeId:'E6-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'165/50R15 73V',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'650',
ywcl:'650',
},
{
codeId:'E7-L',
startDay:'02',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'165/50R15 73V',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'650',
ywcl:'650',
},
{
codeId:'E7-R',
startDay:'01',
startHour:'00',
endDay:'01',
endHour:'00',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'165/50R15 73V',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'650',
ywcl:'650',
},
{
codeId:'F1-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'T125/70R18 99M T010 ',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'390',
ywcl:'390',
},
{
codeId:'F2-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'T125/70R18 99M T010 ',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'390',
ywcl:'390',
},
{
codeId:'F3-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'T125/70R18 99M T010 ',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'390',
ywcl:'390',
},
{
codeId:'F4-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'T125/70R18 99M T010 ',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'390',
ywcl:'390',
},
{
codeId:'F1-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'215/55R17 94W AM520',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'558',
ywcl:'558',
},
{
codeId:'F2-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'215/55R17 94W AM520',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'558',
ywcl:'558',
},
{
codeId:'F3-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'215/55R17 94W AM520',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'558',
ywcl:'558',
},
{
codeId:'F4-LR',
startDay:'01',
startHour:'01',
endDay:'03',
endHour:'24',
msg:'3405 cv 744F R524 LX-WCV Dec 31 TRT(14)',
innerMsg:'215/55R17 94W AM520',
type:'1',
cpdh:'TP183119000',
specdh:'1000001H',
jd:'TP183119000',
bc:'1000001H',
ydl:'558',
ywcl:'558',
},
]
return dataJson
}
function initGrid(){
//数据分组及排序
var data = getGroupData(dataJson);
$('.machineTitleItem').remove()
for(var i = 0;i<data.length;i++){
var stopTimes = 0;
var str = "";
var height = 44
if(data[i].data.length>0){
height = 45*data[i].data.length-1
}
str+='<div class="machineTitleItem" style="height:'+height+'px;line-height:'+height+'px">'+data[i].machineCode+'</div>';
$('#machinePanl').append(str)
var dataItem = data[i].data;
var strItem = "";
for(var j = 0;j<dataItem.length;j++){
var marginLeft = 0;
var width = 0;
var itemStartDay = parseInt(dataItem[j].startDay);
var itemEndDay = parseInt(dataItem[j].endDay);
var itemStartHour = parseInt(dataItem[j].startHour);
var itemEndHour = parseInt(dataItem[j].endHour);
width = (itemEndHour-itemStartHour+1)*25;
if(itemEndDay-itemStartDay==1){
width = (24-itemStartHour+itemEndHour+1)*25;
}
if(itemEndDay-itemStartDay>1){
width = (24*(itemEndDay-itemStartDay)-itemStartHour+itemEndHour+1)*25;
}
marginLeft = (itemStartDay-startDay)*25*24+(itemStartHour-1)*25;
var colorClass = "bg_item_blue";
if(dataItem[j].type=="2"){
colorClass = "bg_item_green";
}else if(dataItem[j].type=="3"){
colorClass = "bg_item_yellow";
}else if(dataItem[j].type=="4"){
colorClass = "bg_item_rin";
}
var tooltipClass = "lineItemBottom";
if((data.length-i)<=2&&(dataItem.length-j)<=4){
tooltipClass = "lineItemTop";
}
if(i==1&&j<4){
tooltipClass = "lineItemBottom";
}
if(24-itemStartHour<=12&&itemEndDay==endDay){
tooltipClass+=" lineItemLeft"
}
if(itemEndDay-itemStartDay==0&&itemEndHour-itemStartHour==0){
strItem+="<div class='lineItem'></div>"
}else if(dataItem[j].type=="0"){
//停工
colorClass = "bg_item_black";
stopTimes++;
strItem+="<div class='lineItem' style='margin-left:"+marginLeft+"px;'><div class='"+tooltipClass+" "+colorClass+"' style='width:"+width+"px'><span class='itemIndex'>停</span>"+dataItem[j].innerMsg+
"</span></div></div>"
}else{
strItem+="<div class='lineItem' style='margin-left:"+marginLeft+"px;'><div class='"+tooltipClass+" "+colorClass+"' style='width:"+width+"px'><span class='itemIndex'>"+(parseInt(j)+1-stopTimes)+"</span>"+dataItem[j].innerMsg+
"<span class='tooltiptext'><span class='tooltipLab'>规格明细:</span>"+dataItem[j].innerMsg+"<hr><span class='tooltipLab'>成品代号:</span><span class='tooltipVal'>"+dataItem[j].cpdh+"</span>"+
"<span class='tooltipLab'>SPEC代号:</span><span class='tooltipVal'>"+dataItem[j].specdh+"</span><br>"+
"<span class='tooltipLab'>阶段:</span><span class='tooltipVal'>"+dataItem[j].jd+"</span>"+
"<span class='tooltipLab'>版次:</span><span class='tooltipVal'>"+dataItem[j].bc+"</span><br>"+
"<span class='tooltipLab'>预定量:</span><span class='tooltipVal'>"+dataItem[j].ydl+"</span>"+
"<span class='tooltipLab'>已完成量:</span><span class='tooltipVal'>"+dataItem[j].ywcl+"</span>"+
"</span></div></div>"
}
}
$('.linePnal').append(strItem)
$('.linePnal').append("<div style='width:100%;position:absolute;height:1px;background-color:#666;margin-top: -1px;'></div>")
}
}
function getGroupData(data){
var markData = []
for(var i = 0;i<data.length;i++){
if(markData.length==0){
markData.push({
machineCode:data[i].codeId,
data:[data[i]]
})
}else{
for(var j=0;j<markData.length;j++){
if(data[i].codeId==markData[j].machineCode){
markData[j].data.push(data[i]);
break;
}else if(j==(markData.length-1)){
markData.push({
machineCode:data[i].codeId,
data:[]
})
}
}
}
}
return markData;
}
css代码:
body{
margin: 0;
font-size: 12px;
}
.mainPanl{
overflow-y: auto;
overflow-x: hidden;
height: calc(100vh - 150px) !important;
width: 1640px;
}
.machineTitle{
width: 98px;
height: 63px;
border: 1px solid;
text-align: center;
line-height: 63px;
background-color: #fff;
float: left;
}
.machineTitleDom{
width: 98px;
height: 63px;
border: 1px solid;
text-align: center;
line-height: 63px;
}
.machineTitleItem{
width: 98px;
height: 63px;
border: 1px solid;
border-top: 0;
text-align: center;
line-height: 63px;
}
#machinePanl{
float: left;
font-size: 14px;
width: 100px;
}
.panl_li{
width: 1640px;
position: absolute;
z-index: 99;
}
.bg_blue{
background-color: #ddebf7;
}
.bg_pink{
background-color: #fce4d6;
}
.title_hour{
width: 25px;
height: 30px;
float: left;
}
.title_all_items{
height: 63px;
float: left;
}
.title_all_dom{
height: 65px;
width: 100%;
}
.title_all{
text-align: center;
float: left;
border: 1px solid;
border-left: 0;
line-height: 30px;
width: 602px;
height: 63px;
font-size: 14px;
}
.title_day{
border-bottom: 1px solid;
font-size: 17px;
font-weight: bold;
}
.title_hours{
clear: both;
width: 600px;
height: 30px;
}
.linePnal{
float: left;
width: 100%;
position: relative;
}
.lineItem{
height: 45px;
}
.bg_item_blue{
background-color: #7b859f;
}
.bg_item_green{
background-color: #a5d4c2;
}
.bg_item_yellow{
background-color: #e9d276;
}
.bg_item_rin{
background-color: #89d1d8;
}
.bg_item_black{
background-color: #e6e7ec;
}
.lineItemBottom,.lineItemTop{
position: relative;
display: inline-block;
border-bottom: 1px dotted black; /* 悬停元素上显示点线 */
height: 30px;
line-height: 30px;
border-radius: 20px;
white-space: nowrap;
cursor: pointer;
margin-top: 5px;
}
/* Tooltip 文本 */
.lineItemBottom .tooltiptext {
visibility: hidden;
width: 450px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
/* 定位 */
position: absolute;
z-index: 1;
bottom: -135px;
left: 0%;
margin-left: 0px; /* 使用一半宽度 (120/2 = 60) 来居中提示工具 */
opacity: 0;
transition: opacity 1s;
font-size: 14px;
border: 1px solid;
}
/* Tooltip 文本 */
.lineItemTop .tooltiptext {
visibility: hidden;
width: 450px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
/* 定位 */
position: absolute;
z-index: 1;
bottom: 30px;
left: 0%;
margin-left: 0px; /* 使用一半宽度 (120/2 = 60) 来居中提示工具 */
opacity: 0;
transition: opacity 1s;
font-size: 14px;
border: 1px solid;
}
/* Tooltip 文本 */
.lineItemLeft .tooltiptext {
margin-left: -450px; /* 使用一半宽度 (120/2 = 60) 来居中提示工具 */
}
/* 鼠标移动上去后显示提示框 */
.lineItemBottom:hover .tooltiptext {
visibility: visible;
opacity: 0.8;
}
/* 鼠标移动上去后显示提示框 */
.lineItemTop:hover .tooltiptext {
visibility: visible;
opacity: 0.8;
}
/*tooltip样式*/
.tooltipLab{
width: 75px;
display: inline-block;
text-align: right;
}
.tooltipVal{
width: 120px;
display: inline-block;
text-align: left;
}
.itemIndex{
width: 20px;
height: 20px;
margin-top: 2px;
margin-left: 5px;
display: inline-block;
line-height: 20px;
border-radius: 50%;
text-align: center;
color: #fff;
background-color: #d15686;
margin-right: 5px;
}
/* *{
box-sizing: content-box;
} */
#gantPanl{
width: 1530px;
float: left;
}
.table-striped {
overflow-x: scroll;
position: relative;
overflow-y: auto;
height: calc(100vh - 120px) !important;
}
.dataPanl{
padding-top: 65px;
}
.dataPanl div{
box-sizing: content-box;
}
.panl_li div{
box-sizing: content-box;
}
hr{
margin-bottom: 0;
margin-top: 0;;
}
内置使用tooltip触摸查看详细信息,没什么难点,注意元素的计算也可以自己创建别的风格的进度图,这里只提供一个创建的方法思路