读取JSON并绘制对应的甘特图

文章目录

一、代码

# state 0  suspended // 停止记录
#       1  ready
#       2  running //开始记录
#       3  waiting  // 停止记录
#       5  ready and new 
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_yticks = [[] for j in range(2)]
# print(list_yticks)
list_data_ts_temp = [0] * proc_id_nums #初始化每个proc自己的ts存储变量
list_proc_nums = [0] * proc_id_nums  #初始化每个proc的序号
list_record_flag = [0] * proc_id_nums #初始化每个proc的长度记录标记号
list_position = [0] * proc_id_nums   #初始化每个proc的记录起始位置



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)

########################################draw the proc############################################
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&
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值