一、代码
import matplotlib.pyplot as plt
import numpy as np
import json
ax=plt.gca()
[ax.spines[i].set_visible(False) for i in ["top","right"]]
data_type = ''
data_ts = ''
proc_id = ''
data_id = ''
target_state = ''
list1 = []
list2 = []
list3 = []
list4 = []
list5 = []
list_xticks = []
list_yticks = []
xticks_min = 0
xticks_max = 0
yticks_min = 0
yticks_max = 0
proc_id_nums = 5
data_height = 2
list_data_ts_temp = [0] * proc_id_nums
list_proc_nums = [0] * proc_id_nums
list_record_flag = [0] * proc_id_nums
list_position = [0] * proc_id_nums
def gatt(i,data_height,data_width,position):
"""甘特图
m机器集
t时间集
"""
plt.barh(i,height=data_height,width=data_width,left=position)
plt.text(position,i,'position: %s\nprocid: %s'%(position,i//5 - 1),color="white",size=8)
def proc(i):
global data_height
data_type = i.get("proc_id")
if data_type == "0":
m = 5
data_ts = i.get("ts")
target_state = i.get("target_state")
if target_state == "2":
list_record_flag[m//5 - 1] = 1
list1.append(int(data_ts))
list_position[m//5 - 1] = int(data_ts)
print("set record_flag to 1")
if (target_state == "3" or target_state == "0")and list_record_flag[m//5 - 1] == 1:
print("set record_flag to 0")
list_record_flag[m//5 - 1] = 0
list1.append(int(data_ts))
time = list1[list_proc_nums[m//5 - 1] + 1]-list1[list_proc_nums[m//5 - 1]]
gatt(m,data_height,time,list_position[m//5 - 1])
list_proc_nums[m//5 - 1] +=2
print("time=%d" % time)
print("position=%d" % list_position[m//5 - 1])
elif data_type == "1":
m = 10
data_ts = i.get("ts")
target_state = i.get("target_state")
if target_state == "2":
list_record_flag[m//5 - 1] = 1
list2.append(int(data_ts))
list_position[m//5 - 1] = int(data_ts)
print("set record_flag1 to 1")
if (target_state == "3" or target_state == "0")and list_record_flag[m//5 - 1] == 1:
print("set record_flag1 to 0")
list_record_flag[m//5 - 1] = 0
list2.append(int(data_ts))
time1 = list2[list_proc_nums[m//5 - 1]+1]-list2[list_proc_nums[m//5 - 1]]
gatt(m,data_height,time1,list_position[m//5 - 1])
list_proc_nums[m//5 - 1] +=2
print("time1=%d" % time1)
print("position1=%d" % list_position[m//5 - 1])
elif data_type == "2":
m = 15
data_ts = i.get("ts")
target_state = i.get("target_state")
if target_state == "2":
list_record_flag[m//5 - 1] = 1
list3.append(int(data_ts))
list_position[m//5 - 1] = int(data_ts)
print("set record_flag2 to 1")
if (target_state == "3" or target_state == "0") and list_record_flag[m//5 - 1] == 1:
print("set record_flag2 to 0")
list_record_flag[m//5 - 1] = 0
list3.append(int(data_ts))
time2 = list3[list_proc_nums[m//5 - 1]+1]-list3[list_proc_nums[m//5 - 1]]
gatt(m,data_height,time2,list_position[m//5 - 1])
list_proc_nums[m//5 - 1] +=2
print("time1=%d" % time2)
print(