-
Notifications
You must be signed in to change notification settings - Fork 1
/
product_pie_chart.py
158 lines (125 loc) · 4.77 KB
/
product_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 30 22:02:28 2022
@author: arman hossain
!python product_pie_chart.py
"""
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
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(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Top Affected Products For Each Vendor")
self.setGeometry(100,100, 1280,600)
if os.path.exists("./data/2002-2022-vendor_product_vs_count.pickle"):
self.read_data("./data/2002-2022-vendor_product_vs_count.pickle")
vbox = QVBoxLayout()
vbox.setContentsMargins(0,0,0,0)
self.setLayout(vbox)
self.show()
# self.read_data("../data/2002-2022-vendor-year-count.pickle")
# self.create_piechart()
def read_data(self,filename):
with open(filename, 'rb') as file:
obj = pickle.load(file)
self.data = 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:
{
prod1:
{
1999: count,
1999.5: count,
},
prod2:
{
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 clearLayout(self,layout):
if layout==None: return
while layout.count():
child = layout.takeAt(0)
if child.widget():
child.widget().deleteLater()
def create_piechart(self,product_name,maxi):
data_dict = self.data[product_name]
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 > maxi: 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("Top Affected Products For Each Vendor")
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
chartview = QChartView(chart)
chartview.setRenderHint(QPainter.Antialiasing)
self.clearLayout(self.layout())
self.layout().addWidget(chartview)
if __name__ == "__main__":
# top vendors ['qualcomm', 'cisco', 'microsoft', 'apple', 'linux', 'intel', 'mozilla', 'adobe', 'oracle', 'ibm', 'google', 'sun', 'juniper', 'f5', 'hp', 'redhat', 'huawei', 'opera', 'apache', 'netgear', 'php', 'debian', 'vmware', 'siemens', 'canonical', 'dell', 'lenovo', 'zohocorp', 'netapp', 'amd', 'moodle']
# for qualcom ['mdm9607', 'mdm9206', 'mdm9607_firmware']
App = QApplication(sys.argv)
window = Window()
window.create_piechart('cisco',10)
sys.exit(App.exec_())