Диаграмма Ганта

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from datetime import datetime
from PIL import Image, ImageDraw
data = [[[1,2]],
[[2,3],[2,4]],
[[3,5],[3,6]],
[[2,5],[1,7]],
[[5,7],[5,4]],
[[5,6]],
[[14,6],[12,9]],
[[19,5],[22,3]]
]
width = 800.0
height = 600.0
img = Image.new('RGB',(width,height),(255,255,255))
draw = ImageDraw.Draw(img)
[discrete_x,discrete_y] = [0,0]
for i in data:
discrete_y += len(i)
if discrete_x <= i[0][0]:
if len(i) == 2:
discrete_x = max(i[0][0]+i[0][1],i[1][0]+i[1][1])
else:
discrete_x = i[0][0]+i[0][1]
else:
if len(i) == 2:
discrete_x = max(discrete_x-(discrete_x-i[0][0])+i[0][1],discrete_x-(discrete_x-i[1][0])+i[1][1])
else:
discrete_x = discrete_x-(discrete_x-i[0][0])+i[0][1]
for i in range(discrete_x):
draw.line([i*width/discrete_x,0,i*width/discrete_x,height],fill="Gray")
draw.text([i*width/discrete_x-15,height-15],str(i),fill="Gray")
draw.text([(i+1)*width/discrete_x-15,height-15],"t",fill="Gray")
j = 0
for i in data:
if len(i) == 1:
draw.rectangle([int(i[0][0]*width/discrete_x),int(height-j*height/discrete_y),int((i[0][0]+i[0][1])*width/discrete_x),int(height-(j+1)*height/discrete_y)],fill="Green")
j += 1
else:
draw.rectangle([int(i[0][0]*width/discrete_x),int(height-j*height/discrete_y),int((i[0][0]+i[0][1])*width/discrete_x),int(height-(j+1)*height/discrete_y)],fill="Green")
j += 1
draw.rectangle([int(i[1][0]*width/discrete_x),int(height-j*height/discrete_y),int((i[1][0]+i[1][1])*width/discrete_x),int(height-(j+1)*height/discrete_y)],fill="Red")
j += 1
img.save('gant.jpg','JPEG')