Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime as dt
- import matplotlib.pyplot as plt
- import matplotlib.font_manager as font_manager
- import matplotlib.dates as dates
- from matplotlib.dates import WEEKLY, MONTHLY, DateFormatter, rrulewrapper, RRuleLocator
- import numpy as np
- from tkinter import *
- def create_date(date):
- d, m, y = [int(s) for s in date.split('.')]
- return dates.date2num(dt.datetime(y, m, d)) #делаем дату пригодной для matplotlib`а
- def checker(a,b):
- if a[0] < b[1] or a[1] > b[0]:
- return True
- return False
- def recreate_dict(date_of_task):
- date_of_task_rebuild = {}
- for i in date_of_task:
- for j in date_of_task:
- if i != j:
- for n in range(len(i)):
- for m in range(len(j)):
- if checker(date_of_task[i][n],date_of_task[j][m]):
- a = date_of_task[i][n]; b = date_of_task[j][m]
- if i not in date_of_task_rebuild: date_of_task_rebuild[i] = []
- if j not in date_of_task_rebuild: date_of_task_rebuild[j] = []
- if [a[0]] + [b[0]] + ["blue"] not in date_of_task_rebuild[i]: date_of_task_rebuild[i].append([a[0]] + [b[0]] + ["blue"])
- if [b[0]] + [a[1]] + ["red"] not in date_of_task_rebuild[i]: date_of_task_rebuild[i].append([b[0]] + [a[1]] + ["red"])
- if [b[0]] + [a[1]] + ["red"] not in date_of_task_rebuild[j]: date_of_task_rebuild[j].append([b[0]] + [a[1]] + ["red"])
- if [a[1]] + [b[1]] + ["blue"] not in date_of_task_rebuild[j]: date_of_task_rebuild[j].append([a[1]] + [b[1]] + ["blue"])
- else:
- a = date_of_task[i][n]; b = date_of_task[j][m]
- if i not in date_of_task_rebuild: date_of_task_rebuild[i] = []
- if j not in date_of_task_rebuild: date_of_task_rebuild[j] = []
- if [a[0]] + [a[1]] + ["blue"] not in date_of_task_rebuild[i]: date_of_task_rebuild[i].append([a[0]] + [a[1]] + ["blue"])
- if [b[0]] + [b[1]] + ["blue"] not in date_of_task_rebuild[j]: date_of_task_rebuild[j].append([b[0]] + [b[1]] + ["blue"])
- return date_of_task_rebuild
- def create_diagramm_gantt(event):
- name_characters, custom_dates = [],[]
- for people in characters: name_characters.append(people["Name"]); custom_dates.append([create_date(people["Begin_date"]),create_date(people["End_date"])]) #даты начала и конца отпусков
- date_of_task = {}
- for i, name in enumerate(name_characters):
- if name not in date_of_task: date_of_task[name] = []
- t = custom_dates[i] + ["blue"]
- date_of_task[name].append(t) #делаем словарь "ФИО":["начало","конец"] !ПЕРЕДЕЛАТЬ ДЛЯ НЕСКОЛЬКИХ ОТПУСКОВ НА СОТРУДНИКА!
- figure = plt.figure(figsize=(20,8))
- ax = figure.add_subplot(111)
- position = np.arange(0.5,len(date_of_task)*0.5+0.5,0.5)
- date_of_task_rebuild = recreate_dict(date_of_task)
- for i in range(len(date_of_task_rebuild)):
- for j in range(len(date_of_task_rebuild[name_characters[i]][0])):
- start_date,end_date,color = date_of_task_rebuild[name_characters[i]][j]
- ax.barh((i*0.5)+0.5,end_date - start_date, left=start_date, height=0.3, align='center',
- edgecolor=color, color=color, alpha = 0.8) #делаем колонки
- _, labelsy = plt.yticks(position,name_characters) #далее создание интерфейса диаграммы
- plt.setp(labelsy, fontsize = 14)
- ax.set_ylim(bottom = -0.1, top = len(date_of_task)*0.5+0.5)
- ax.grid(color = 'b', linestyle = ':')
- ax.xaxis_date()
- location = RRuleLocator(rrulewrapper(WEEKLY, interval=1))
- formatter = DateFormatter("%d-%b '%y")
- ax.xaxis.set_major_locator(location)
- ax.xaxis.set_major_formatter(formatter)
- plt.setp(ax.get_xticklabels(), rotation=30, fontsize=10)
- font = font_manager.FontProperties(size='small')
- ax.legend(loc=1,prop=font)
- ax.invert_yaxis()
- figure.autofmt_xdate()
- plt.show()
- def input_(event):
- char = [s[1:-1] for s in character.get().split("-")] #разбиваем строку "ФИО"-"начало"-"конец" на массив
- characters.append(dict(Name = char[0],Begin_date = char[1],End_date = char[2])) #добавляем
- if __name__ == "__main__":
- characters = [] #сотрудники
- root = Tk()
- root.title("Ввод отпусков")
- character, inputing, shows = [Entry(width=50),
- Button(text="Ввести данные сотрудника"),
- Button(text="Показать диаграмму")]
- character.pack(); inputing.pack(); shows.pack()
- inputing.bind('<Button-1>', input_); shows.bind('<Button-1>', create_diagramm_gantt)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement