好久不見哈,今天帶來一個(gè)不咋炫酷但是比較實(shí)用的甘特圖繪制,就畫一堆矩形嘛非常簡(jiǎn)單。
工具函數(shù)完整代碼
之所以這期工具函數(shù)放在最前面是因?yàn)楸容^短哈:
function ganttHdl=ganttChart(sT,dT,id,varargin)
% sT | 任務(wù)開始時(shí)間
% dT | 任務(wù)持續(xù)時(shí)間
% id | 任務(wù)所屬類型
% @author : slandarer
% 公眾號(hào) : slandarer隨筆
ax=gca;hold on;
ax.YTick=1:max(id);
ax.YLim=[0,max(id)+1];
sT=sT(:);dT=dT(:);id=id(:);
% 基本配色表
this.colorList=[118 160 173;89 124 139;212 185 130;
217 189 195;165 108 127;188 176 210]./255;
this.colorList=[this.colorList;rand(max(id),3).*.6+.4];
% 獲取其他屬性
this.String='';
arginList={'String','ColorList'};
for i=(length(varargin)-1):-2:1
tid=ismember(arginList,varargin{i});
if any(tid)
this.(arginList{tid})=varargin{i+1};
varargin(i:i+1)=[];
end
end
% 循環(huán)繪圖
for i=unique(id)'
t_sT=sT(id==i);
t_dT=dT(id==i);
[t_sT,t_ind]=sort(t_sT);
t_dT=t_dT(t_ind);
if ~isempty(this.String)
t_Str=this.String(id==i);
t_Str=t_Str(t_ind);
end
for j=1:length(t_sT)
ganttHdl.(['p',num2str(i)])(j)=rectangle('Position',[t_sT(j),i-.4,t_dT(j),.8],...
'LineWidth',.8,'EdgeColor',[.2,.2,.2],...
'FaceColor',this.colorList(i,:),'AlignVertexCenters','on',varargin{:});
end
for j=1:length(t_sT)
if ~isempty(this.String)
ganttHdl.(['t',num2str(i)])(j)=text(t_sT(j),i,t_Str{j});
else
ganttHdl.(['t',num2str(i)])(j)=text(t_sT(j),i,'');
end
end
end
end
使用方法
基本使用
設(shè)置任務(wù)開始時(shí)間,結(jié)束時(shí)間及任務(wù)編號(hào)后,調(diào)用工具函數(shù)繪圖即可:
startT=[0 3 5 12 16,1.5 6 8 15 18,3 10 11 16 19,2 4 11 13 15,1 3 7 16 18,5 11 14 16 20];
durationT=[3 2 5 4 4,4 1 7 2 5,6 1 4 3 2,1 6 2 2 9,2 3 4 2 5,5 3 1 2 8];
jobId=[1 1 1 1 1,2 2 2 2 2,3 3 3 3 3,4 4 4 4 4,5 5 5 5 5,6 6 6 6 6];
GTC=ganttChart(startT,durationT,jobId);
不咋好看的圓角
設(shè)置Curvature
為0-1之間的數(shù)值即可:
GTC=ganttChart(startT,durationT,jobId,'Curvature',.8);
修改Y軸標(biāo)簽
就改當(dāng)前坐標(biāo)區(qū)域的YTickLabel
屬性即可,例如:
ax=gca;
ax.YTickLabel={'Process1','Process2','Process3','Process4','Process5','Process6'};
添加每個(gè)任務(wù)上的文本信息
調(diào)用工具函數(shù)的時(shí)候設(shè)置String
屬性即可,例如:
startT=[0 3 5 12 16,1.5 6 8 15 18,3 10 11 16 19,2 4 11 13 15,1 3 7 16 18,5 11 14 16 20];
durationT=[3 2 5 4 4,4 1 7 2 5,6 1 4 3 2,1 6 2 2 9,2 3 4 2 5,5 3 1 2 8];
jobId=[1 1 1 1 1,2 2 2 2 2,3 3 3 3 3,4 4 4 4 4,5 5 5 5 5,6 6 6 6 6];
pName{length(jobId)}='';
for i=1:length(jobId)
pName(i)={num2str(i)};
end
GTC=ganttChart(startT,durationT,jobId,'String',pName);
當(dāng)然可以更復(fù)雜些,包含一下每個(gè)任務(wù)的信息:
pName{length(jobId)}='';
for i=1:length(jobId)
pName(i)={['[',num2str(startT(i)),',',num2str(startT(i)+durationT(i)),']']};
end
單獨(dú)修改任務(wù)標(biāo)簽信息
實(shí)際上工具函數(shù)的返回值長(zhǎng)這樣:
GTC =
包含以下字段的 struct:
p1: [1×5 Rectangle]
t1: [1×5 Text]
p2: [1×5 Rectangle]
t2: [1×5 Text]
p3: [1×5 Rectangle]
t3: [1×5 Text]
p4: [1×5 Rectangle]
t4: [1×5 Text]
p5: [1×5 Rectangle]
t5: [1×5 Text]
p6: [1×5 Rectangle]
t6: [1×5 Text]
因此我們可以對(duì)每個(gè)矩形塊或者每個(gè)文本單獨(dú)修飾,例如:
startT=[0 3 5 12 16,1.5 6 8 15 18,3 10 11 16 19,2 4 11 13 15,1 3 7 16 18,5 11 14 16 20];
durationT=[3 2 5 4 4,4 1 7 2 5,6 1 4 3 2,1 6 2 2 9,2 3 4 2 5,5 3 1 2 8];
jobId=[1 1 1 1 1,2 2 2 2 2,3 3 3 3 3,4 4 4 4 4,5 5 5 5 5,6 6 6 6 6];
GTC=ganttChart(startT,durationT,jobId);
GTC.t1(2).String='slandarer';
GTC.t1(2).Color=[1,0,0];
GTC.t1(2).FontSize=25;
修改顏色
寫個(gè)循環(huán)為每個(gè)矩形修改顏色,一個(gè)實(shí)例:
startT=[0 3 5 12 16,1.5 6 8 15 18,3 10 11 16 19,2 4 11 13 15,1 3 7 16 18,5 11 14 16 20];
durationT=[3 2 5 4 4,4 1 7 2 5,6 1 4 3 2,1 6 2 2 9,2 3 4 2 5,5 3 1 2 8];
jobId=[1 1 1 1 1,2 2 2 2 2,3 3 3 3 3,4 4 4 4 4,5 5 5 5 5,6 6 6 6 6];
GTC=ganttChart(startT,durationT,jobId);
colorList=[204,154,129;222,191,170;185,184,163;165,165,139;107,112,92]./255;
for i=1:max(jobId)
tHdl=GTC.(['p',num2str(i)]);
for j=1:length(tHdl)
set(tHdl(j),'FaceColor',colorList(j,:))
end
end
此處使用阿昆的科研日常
No.11配色
另一個(gè)實(shí)例:
startT=[0 3 5 12 16,1.5 6 8 15 18,3 10 11 16 19,2 4 11 13 15,1 3 7 16 18,5 11 14 16 20];
durationT=[3 2 5 4 4,4 1 7 2 5,6 1 4 3 2,1 6 2 2 9,2 3 4 2 5,5 3 1 2 8];
jobId=[1 1 1 1 1,2 2 2 2 2,3 3 3 3 3,4 4 4 4 4,5 5 5 5 5,6 6 6 6 6];
GTC=ganttChart(startT,durationT,jobId);
colorList=[165 108 127;165 108 127;89 124 139;
89 124 139;212 185 130;212 185 130]./255;
for i=1:max(jobId)
tHdl=GTC.(['p',num2str(i)]);
for j=1:length(tHdl)
set(tHdl(j),'FaceColor',colorList(i,:))
end
end
ax=gca;
ax.YTickLabel={'S-1-1','S-1-2','S-2-1','S-2-2','S-3-1','S-3-2'};
實(shí)際上默認(rèn)配色只有前六組是固定的,更多組是隨機(jī)的,可以多畫幾組試試看:文章來源:http://www.zghlxwxcb.cn/news/detail-491461.html
startT=[0 3 5 12 16,1.5 6 8 15 18,3 10 11 16 19,2 4 11 13 15,1 3 7 16 18,5 11 14 16 20,0 3 5 12 16,1.5 6 8 15 18];
durationT=[3 2 5 4 4,4 1 7 2 5,6 1 4 3 2,1 6 2 2 9,2 3 4 2 5,5 3 1 2 8,3 2 5 4 4,4 1 7 2 5];
jobId=[1 1 1 1 1,2 2 2 2 2,3 3 3 3 3,4 4 4 4 4,5 5 5 5 5,6 6 6 6 6,7 7 7 7 7,8 8 8 8 8];
GTC=ganttChart(startT,durationT,jobId);
文章來源地址http://www.zghlxwxcb.cn/news/detail-491461.html
到了這里,關(guān)于MATLAB | 如何使用MATLAB繪制甘特圖(gantt chart)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!