Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.02 KB | None | 0 0
  1. import datetime as dt
  2. import matplotlib.pyplot as plt
  3. import matplotlib.font_manager as font_manager
  4. import matplotlib.dates as dates
  5. from matplotlib.dates import WEEKLY, MONTHLY, DateFormatter, rrulewrapper, RRuleLocator
  6. import numpy as np
  7. from tkinter import *
  8.  
  9. def create_date(date):
  10.     d, m, y = [int(s) for s in date.split('.')]
  11.     return dates.date2num(dt.datetime(y, m, d)) #делаем дату пригодной для matplotlib`а
  12.    
  13. def checker(a,b):
  14.     if a[0] < b[1] or a[1] > b[0]:
  15.         return True
  16.     return False
  17.  
  18. def recreate_dict(date_of_task):
  19.     date_of_task_rebuild = {}
  20.     for i in date_of_task:
  21.         for j in date_of_task:
  22.             if i != j:
  23.                 for n in range(len(i)):
  24.                     for m in range(len(j)):
  25.                         if checker(date_of_task[i][n],date_of_task[j][m]):
  26.                             a = date_of_task[i][n]; b = date_of_task[j][m]
  27.                             if i not in date_of_task_rebuild: date_of_task_rebuild[i] = []
  28.                             if j not in date_of_task_rebuild: date_of_task_rebuild[j] = []
  29.                             if [a[0]] + [b[0]] + ["blue"] not in date_of_task_rebuild[i]: date_of_task_rebuild[i].append([a[0]] + [b[0]] + ["blue"])
  30.                             if [b[0]] + [a[1]] + ["red"] not in date_of_task_rebuild[i]: date_of_task_rebuild[i].append([b[0]] + [a[1]] + ["red"])
  31.                             if [b[0]] + [a[1]] + ["red"] not in date_of_task_rebuild[j]: date_of_task_rebuild[j].append([b[0]] + [a[1]] + ["red"])      
  32.                             if [a[1]] + [b[1]] + ["blue"] not in date_of_task_rebuild[j]: date_of_task_rebuild[j].append([a[1]] + [b[1]] + ["blue"])
  33.                         else:
  34.                             a = date_of_task[i][n]; b = date_of_task[j][m]
  35.                             if i not in date_of_task_rebuild: date_of_task_rebuild[i] = []
  36.                             if j not in date_of_task_rebuild: date_of_task_rebuild[j] = []
  37.                             if [a[0]] + [a[1]] + ["blue"] not in date_of_task_rebuild[i]: date_of_task_rebuild[i].append([a[0]] + [a[1]] + ["blue"])
  38.                             if [b[0]] + [b[1]] + ["blue"] not in date_of_task_rebuild[j]: date_of_task_rebuild[j].append([b[0]] + [b[1]] + ["blue"])
  39.     return date_of_task_rebuild
  40.  
  41. def create_diagramm_gantt(event):
  42.     name_characters, custom_dates = [],[]
  43.     for people in characters: name_characters.append(people["Name"]); custom_dates.append([create_date(people["Begin_date"]),create_date(people["End_date"])]) #даты начала и конца отпусков
  44.     date_of_task = {}
  45.     for i, name in enumerate(name_characters):
  46.         if name not in date_of_task: date_of_task[name] = []
  47.         t = custom_dates[i] + ["blue"]
  48.         date_of_task[name].append(t) #делаем словарь "ФИО":["начало","конец"] !ПЕРЕДЕЛАТЬ ДЛЯ НЕСКОЛЬКИХ ОТПУСКОВ НА СОТРУДНИКА!
  49.     figure = plt.figure(figsize=(20,8))
  50.     ax = figure.add_subplot(111)
  51.     position = np.arange(0.5,len(date_of_task)*0.5+0.5,0.5)
  52.     date_of_task_rebuild = recreate_dict(date_of_task)
  53.     for i in range(len(date_of_task_rebuild)):
  54.         for j in range(len(date_of_task_rebuild[name_characters[i]][0])):
  55.             start_date,end_date,color = date_of_task_rebuild[name_characters[i]][j]
  56.             ax.barh((i*0.5)+0.5,end_date - start_date, left=start_date, height=0.3, align='center',
  57.                     edgecolor=color, color=color, alpha = 0.8) #делаем колонки
  58.     _, labelsy = plt.yticks(position,name_characters) #далее создание интерфейса диаграммы
  59.     plt.setp(labelsy, fontsize = 14)
  60.     ax.set_ylim(bottom = -0.1, top = len(date_of_task)*0.5+0.5)
  61.     ax.grid(color = 'b', linestyle = ':')
  62.     ax.xaxis_date()
  63.     location = RRuleLocator(rrulewrapper(WEEKLY, interval=1))
  64.     formatter = DateFormatter("%d-%b '%y")
  65.     ax.xaxis.set_major_locator(location)
  66.     ax.xaxis.set_major_formatter(formatter)
  67.     plt.setp(ax.get_xticklabels(), rotation=30, fontsize=10)
  68.     font = font_manager.FontProperties(size='small')
  69.     ax.legend(loc=1,prop=font)
  70.     ax.invert_yaxis()
  71.     figure.autofmt_xdate()
  72.     plt.show()
  73.    
  74. def input_(event):
  75.     char = [s[1:-1] for s in character.get().split("-")] #разбиваем строку "ФИО"-"начало"-"конец" на массив
  76.     characters.append(dict(Name = char[0],Begin_date = char[1],End_date = char[2])) #добавляем
  77.  
  78. if __name__ == "__main__":
  79.     characters = [] #сотрудники
  80.     root = Tk()
  81.     root.title("Ввод отпусков")
  82.  
  83.     character, inputing, shows = [Entry(width=50),
  84.                                   Button(text="Ввести данные сотрудника"),
  85.                                   Button(text="Показать диаграмму")]
  86.  
  87.     character.pack(); inputing.pack(); shows.pack()
  88.     inputing.bind('<Button-1>', input_); shows.bind('<Button-1>', create_diagramm_gantt)
  89.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement