The Qt framework doesn’t contain any useful charting widgets. There are a few python charting modules out there, most notably PyQwt. As a PyQt developer I didn’t find this module very useful. What I really wanted was a charting widget that used QWidget as its base class. At the time of this post I couldn’t find anything so I wrote my own based on the google charts module.
What I love about this snippet is that it mimics the C++ template functionality in Python. The QChart function below abstracts the PyQtChart class definition and any pygooglechart object can be instantiated as a QWidget.
__author__ = "Christopher Piekarski" __email__ = "@c_piekarski" import time, os, sys from PyQt4 import QtCore, QtGui from pygooglechart import * def QChart(parent, type, **kwargs): class PyQtChart(type, QtGui.QWidget): def __init__(self, parent, **kwargs): QtGui.QWidget.__init__(self, parent, **kwargs) type.__init__(self, kwargs["size"].width(), kwargs["size"].height()) self.pix = QtGui.QPixmap() def download(self): file = "./%f.png" % time.time() type.download(self, file) self.pix.load(file) def paintEvent(self, event): p = QtGui.QPainter(self) p.drawPixmap(0,0,self.pix) super(PyQtChart, self).paintEvent(event) return PyQtChart(parent, **kwargs) class MainWindow(QtGui.QMainWindow): def __init__(self, **kwargs): super(QtGui.QMainWindow, self).__init__(**kwargs) t = QChart(self, PieChart3D, size=QtCore.QSize(250,100)) t.add_data([10,20]) t.set_pie_labels(['Hello', 'World']) t.download() if __name__ == '__main__': app = QtGui.QApplication(sys.argv) app.setApplicationName("PyQt Charting Demo") app.setQuitOnLastWindowClosed(True) scaledSize = QtCore.QSize(500,500) window = MainWindow(size=scaledSize) window.setWindowTitle("PyQt Charting Demo") window.show() sys.exit(app.exec_())
Pingback: Python Generators | Christopher Piekarski