-
Notifications
You must be signed in to change notification settings - Fork 1
/
veondor_pie_chart.py
137 lines (106 loc) · 3.62 KB
/
veondor_pie_chart.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 30 22:02:00 2022
@author: arman hossain
!python veondor_pie_chart.py
"""
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from PyQt5.QtChart import QChart, QChartView, QPieSeries, QPieSlice
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt
import pickle
import os
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "Top Affected Vendors"
self.setWindowTitle(self.title)
# self.setGeometry(100,100, 1280,600)
self.setMinimumSize(200, 360)
if os.path.exists('./data/2002-2022-vendor-year-count.pickle'):
data = self.read_pickle('./data/2002-2022-vendor-year-count.pickle')
self.create_piechart(data)
self.show()
def read_pickle(self,filename):
with open(filename, 'rb') as file:
obj = pickle.load(file)
# filename = "2002-2022-vendor-year-count.pickle"
return obj
def total_veulnerability_by_year(self,dict_):
'''
intended for this dictionary
{
1999: count,
1999.5: count,
}
'''
total = 0
for key in dict_:
total+=dict_[key]
return total
def total_veulnerability(self,dict_):
'''
intended for this dictionary
{
microsoft:
{
1999: count,
1999.5: count,
}
juniper:
{
1999: count,
1999.5: count,
}
}
'''
total = 0
for key in dict_:
for item in dict_[key]:
total+=dict_[key][item]
return total
def create_piechart(self,data_dict):
cnt = 0
total_individual = 0
series = QPieSeries()
total = self.total_veulnerability(data_dict)
keys = list(data_dict.keys())
while total_individual < total/2:
key = keys[cnt]
# if cnt > 15: break
individual = self.total_veulnerability_by_year(data_dict[key])
series.append(key+" "+str(round(individual/total*100,2))+"%", individual)
total_individual+=individual
cnt+=1
# for key in data_dict:
# if cnt == first_n: break
# cnt+=1
# individual = self.total_veulnerability_by_year(data_dict[key])
# series.append(key, individual)
# total_individual+=individual
series.append(str(len(data_dict.keys())-cnt+1)+" others "+str(round((total-total_individual)/total*100,2))+"%", total-total_individual)
for i in range(cnt+1):
#adding slice
slice = QPieSlice()
slice = series.slices()[i]
# slice.setExploded(True)
slice.setLabelVisible(True)
# slice.setPen(QPen(Qt.darkGreen, 2))
# slice.setBrush(Qt.green)
chart = QChart()
chart.legend().hide()
chart.addSeries(series)
chart.createDefaultAxes()
chart.setAnimationOptions(QChart.SeriesAnimations)
chart.setTitle(self.title)
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
chartview = QChartView(chart)
chartview.setRenderHint(QPainter.Antialiasing)
self.setCentralWidget(chartview)
if __name__ == "__main__":
App = QApplication(sys.argv)
window = Window()
# window.create_piechart(data)
sys.exit(App.exec_())