コード例 #1
0
    def __init__(self):
        QMainWindow.__init__(self)

        set0 = QtCharts.QBarSet("Jane")
        set1 = QtCharts.QBarSet("John")
        set2 = QtCharts.QBarSet("Axel")
        set3 = QtCharts.QBarSet("Mary")
        set4 = QtCharts.QBarSet("Samantha")

        set0.append([1, 2, 3, 4, 5, 6])
        set1.append([5, 0, 0, 4, 0, 7])
        set2.append([3, 5, 8, 13, 8, 5])
        set3.append([5, 6, 7, 3, 4, 5])
        set4.append([9, 7, 5, 3, 1, 2])

        series = QtCharts.QPercentBarSeries()
        series.append(set0)
        series.append(set1)
        series.append(set2)
        series.append(set3)
        series.append(set4)

        chart = QtCharts.QChart()
        chart.addSeries(series)
        chart.setTitle("Simple percentbarchart example")
        chart.setAnimationOptions(QtCharts.QChart.SeriesAnimations)

        categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
        axis = QtCharts.QBarCategoryAxis()
        axis.append(categories)
        chart.createDefaultAxes()
        chart.setAxisX(axis, series)

        chart.legend().setVisible(True)
        chart.legend().setAlignment(Qt.AlignBottom)

        chart_view = QtCharts.QChartView(chart)
        chart_view.setRenderHint(QPainter.Antialiasing)

        self.setCentralWidget(chart_view)
コード例 #2
0
ファイル: main.py プロジェクト: DzThor/website-analytics-app
    def createChart(self, title, days, type, db):

        with open('config.json', 'r') as f:
            config = json.load(f)
            names = config['SEARCH']

        if (type == "lines"):
            datesLookUp = self.DateSeriesMongoDB(
                self.calcultateFirstDate(days))
            datesAvailable = self.datesWithData(datesLookUp, db, title)

            series = []

            for name in names:
                newSeries = QtCharts.QLineSeries()
                newSeries.setName(name)
                #series.append(self.fillLineSeries(newSeries, datesLookUp, db, title))
                series.append(
                    self.fillLineSeries(newSeries, datesAvailable, db, title,
                                        name))

            chart = QtCharts.QChart()
            for serie in series:
                chart.addSeries(serie)

            chart.setTitle(title)

            #datesAxis = self.calculateDateSeries(self.calcultateFirstDate(days))
            print(datesAvailable)
            datesAxis = [dt.strftime("%m-%d") for dt in datesAvailable]
            print(datesAxis)
            chart.createDefaultAxes()
            axisX = QtCharts.QBarCategoryAxis()
            axisY = QtCharts.QValueAxis()
            axisX.append(datesAxis)
            axisX.setRange(datesAxis[0], datesAxis[len(datesAxis) - 1])

            for serie in series:
                chart.setAxisX(axisX, serie)
                chart.setAxisY(axisY, serie)

            chart.legend().setVisible(True)
            chart.legend().setAlignment(Qt.AlignBottom)
            chartView = QtCharts.QChartView(chart)
            chartView.setMinimumSize(700, 500)
            chartView.setRenderHint(QPainter.Antialiasing)

            return chartView

        elif (type == "bars"):

            datesLookUp = self.DateSeriesMongoDB(
                self.calcultateFirstDate(days))
            datesAvailable = self.datesWithData(datesLookUp, db, title)

            series = []

            for name in names:
                newSeries = QtCharts.QBarSet(name)
                #series.append(self.fillBarSeries(newSeries, datesLookUp, db, title))
                series.append(
                    self.fillBarSeries(newSeries, datesAvailable, db, title,
                                       name))

            chart = QtCharts.QChart()
            barSeries = QtCharts.QBarSeries()
            for serie in series:
                barSeries.append(serie)

            chart.setTitle(title)
            chart.addSeries(barSeries)

            #datesAxis = self.calculateDateSeries(self.calcultateFirstDate(days))
            datesAxis = [dt.strftime("%m-%d") for dt in datesAvailable]
            chart.createDefaultAxes()

            axisX = QtCharts.QBarCategoryAxis()
            axisX.append(datesAxis)
            chart.setAxisX(axisX, barSeries)
            axisX.setRange(datesAxis[0], datesAxis[len(datesAxis) - 1])

            axisY = QtCharts.QValueAxis()

            chart.legend().setVisible(True)
            chart.legend().setAlignment(Qt.AlignBottom)
            chartView = QtCharts.QChartView(chart)
            chartView.setMinimumSize(700, 500)
            chartView.setRenderHint(QPainter.Antialiasing)

            return chartView

        elif (type == "percentage"):

            datesLookUp = self.DateSeriesMongoDB(
                self.calcultateFirstDate(days))
            datesAvailable = self.datesWithData(datesLookUp, db, title)
            #datesAxis = self.calculateDateSeries(self.calcultateFirstDate(days))
            datesAxis = [dt.strftime("%m-%d") for dt in datesAvailable]

            series = []

            for name in names:
                newSeries = QtCharts.QBarSet(name)
                #series.append(self.fillBarSeries(newSeries, datesLookUp, db, title))
                series.append(
                    self.fillBarSeries(newSeries, datesAvailable, db, title,
                                       name))

            if (len(names) == 1):
                filler = QtCharts.QBarSet("")
                filler.append([100 for item in range(0, len(datesAxis))])
                series.append(filler)

            chart = QtCharts.QChart()
            percentBarSeries = QtCharts.QPercentBarSeries()
            for serie in series:
                percentBarSeries.append(serie)

            chart.setTitle(title)
            chart.addSeries(percentBarSeries)

            chart.createDefaultAxes()

            axisX = QtCharts.QBarCategoryAxis()
            axisX.append(datesAxis)
            chart.setAxisX(axisX, percentBarSeries)
            axisX.setRange(datesAxis[0], datesAxis[len(datesAxis) - 1])

            axisY = QtCharts.QValueAxis()

            chart.legend().setVisible(True)
            chart.legend().setAlignment(Qt.AlignBottom)
            chartView = QtCharts.QChartView(chart)
            chartView.setMinimumSize(700, 500)
            chartView.setRenderHint(QPainter.Antialiasing)

            return chartView
        elif (type == "schedule"):

            datesLookUp = self.DateSeriesMongoDB(
                self.calcultateFirstDate(days))
            datesAvailable = self.datesWithData(datesLookUp, db, title)
            series = []

            for name in names:
                newSeries = QtCharts.QBarSet(name)
                #series.append(self.fillScheduleSeries(newSeries, datesLookUp, db, title))
                series.append(
                    self.fillScheduleSeries(newSeries, datesAvailable, db,
                                            title, name))

            chart = QtCharts.QChart()
            barSeries = QtCharts.QBarSeries()
            for serie in series:
                barSeries.append(serie)

            chart.setTitle(title)
            #datesAxis = self.scheduleAxis(newSeries, datesLookUp, db, title)
            datesAxis = self.scheduleAxis(newSeries, datesAvailable, db, title)
            chart.addSeries(barSeries)

            chart.createDefaultAxes()

            axisX = QtCharts.QBarCategoryAxis()
            axisX.append(datesAxis)
            chart.setAxisX(axisX, barSeries)
            axisX.setRange(datesAxis[0], datesAxis[len(datesAxis) - 1])

            axisY = QtCharts.QValueAxis()

            chart.legend().setVisible(True)
            chart.legend().setAlignment(Qt.AlignBottom)
            chartView = QtCharts.QChartView(chart)
            chartView.setMinimumSize(700, 500)
            #chartView.setMaximumSize(1000,500)
            chartView.setRenderHint(QPainter.Antialiasing)

            return chartView
        else:
            raise "Wrong chart type"