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
|
#############################################################################
##
## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
## Contact: Qt Software Information (qt-info@nokia.com)
##
## This file is part of the Graphics Dojo project on Qt Labs.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 or 3.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file. Please review the following information to ensure GNU
## General Public Licensing requirements will be met:
## http:#www.fsf.org/licensing/licenses/info/GPLv2.html and
## http:#www.gnu.org/copyleft/gpl.html.
##
## If you are unsure which license is appropriate for your use, please
## contact the sales department at qt-sales@nokia.com.
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
#############################################################################
import sys
from PyQt4.QtCore import QPointF, QSize, Qt
from PyQt4.QtGui import *
class BarChart(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.suffix = ""
def showEvent(self, event):
# find the maximum value and widest (pixel-wise) label and suffix
fm = QFontMetrics(self.font())
self.margin = 20
self.titleHeight = fm.height()
self.barHeight = 1.5 * fm.height()
self.barSpacing = 0.6 * fm.height()
self.maxValue = self.suffixWidth = self.labelWidth = 0
count = 0
for key, value in self.data.items():
self.labelWidth = max(self.labelWidth, fm.width(key))
self.maxValue = max(self.maxValue, value)
self.suffixWidth = max(self.suffixWidth, fm.width(str(value) + " " + self.suffix))
count += 1
self.startHue = 15
self.hueDelta = 360 / count
self.resize(QSize(640, self.titleHeight + 2 * self.margin + (self.barHeight + self.barSpacing) * count))
def paintEvent(self, event):
p = QPainter()
p.begin(self)
# background and title
p.fillRect(self.rect(), QBrush(QColor(255, 255, 255)))
p.drawText(0, 0, self.width(), self.margin + self.titleHeight, Qt.AlignCenter, self.windowTitle())
ofs = self.labelWidth + self.margin
ww = self.width() - self.suffixWidth - ofs - 2 * self.margin
hue = self.startHue
y = 0
p.translate(self.margin, self.titleHeight + 1.5 * self.margin)
for key, value in self.data.items():
# label on the left side
p.setPen(QColor(Qt.black))
p.drawText(0, y, self.labelWidth, self.barHeight, Qt.AlignVCenter + Qt.AlignRight, key)
# the colored bar
gradient = QLinearGradient(QPointF(ofs, y), QPointF(ofs, y + self.barHeight))
gradient.setColorAt(0, QColor.fromHsv(hue, 255, 240))
gradient.setColorAt(1, QColor.fromHsv(hue, 255, 92))
p.setBrush(QBrush(gradient))
p.setPen(QColor(96, 96, 96))
bw = value * ww / self.maxValue
p.drawRect(ofs, y, bw, self.barHeight)
# extra text at the end of the bar
text = str(value) + " " + self.suffix
p.setPen(QColor(Qt.black))
p.drawText(ofs + bw + self.margin/2, y, self.suffixWidth, self.barHeight, Qt.AlignVCenter + Qt.AlignLeft, text)
# for the next bar
y += (self.barHeight + self.barSpacing)
hue += self.hueDelta
if hue >= 360:
hue -= 360
p.end()
def wheelEvent(self, event):
self.startHue += event.delta() / 8 / 5
if self.startHue >= 360:
self.startHue -= 360
if self.startHue < 0:
self.startHue += 360
self.update()
event.ignore()
def mousePressEvent(self, event):
fname = QFileDialog.getSaveFileName(self, "Save", ".", "*.png", 0, 0)
if fname.length > 0:
img = QImage(self.size(), QImage.Format_ARGB32_Premultiplied)
self.render(img)
img.save(QFile(fname))
if __name__ == "__main__":
app = QApplication(sys.argv)
chart = BarChart()
chart.setWindowTitle("SquirrelFish Performance")
chart.suffix = "runs"
chart.data = {"WebKit 3.0" : 5.4,
"WebKit 3.1" : 18.8,
"SquirrelFish" : 29.9,
"SquirrelFish Extreme" : 63.6}
chart.show()
sys.exit(app.exec_())
|