qchart 坐标轴设置_实战PyQt5: 156-QChart图表之更换图表主题
生活随笔
收集整理的這篇文章主要介紹了
qchart 坐标轴设置_实战PyQt5: 156-QChart图表之更换图表主题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圖表主題
QChart定義了多種圖表主題,可以創建不同風格的圖表顯示,在調整圖表主題風格的時候,為了使整個應用的風格看起來更和諧一致,一般要使用應用程序的背景調色板來調整應用的顏色以適配圖表所選的主題。
QChat支持以下幾種風格的圖表主題:
- QChart.ChartThemeLightL: 淺色(Light);
- QChart.ChartThemeBlueCerulean: 天藍(Blue Cerulean);
- QChart.ChartThemeDark: 深色(Dark);
- QChart.ChartThemeBrownSand:棕色(Brown Sand);
- QChart.ChartThemeBlueNcs:藍色NCS(Blue NCS);
- QChart.ChartThemeHighContrast:高對比(High Contrast);
- QChart.ChartThemeBlueIcy:藍色Icy(Blue Icy);
- QChart.ChartThemeQt: Qt風格(Qt)
圖表主題示例
示例代碼不僅演示了主題切換,還包括動畫方式切換,圖例對齊方式,以及抗鋸齒等控制操作,添加了多種圖表序列以演示不同的效果。完整代碼如下:
import?sysfrom?PyQt5.QtCore?import?Qt,?QPointF,?QRandomGeneratorfrom?PyQt5.QtGui?import?QPalette,?QColor,?QPainterfrom?PyQt5.QtWidgets?import?(QApplication,?QMainWindow,?QWidget,?QSizePolicy)from?PyQt5.QtChart?import?(QChart,?QChartView,?QLineSeries,?QAreaSeries,?QStackedBarSeries,???????????????????????????QBarSet,?QPieSeries,?QPieSlice,?QSplineSeries,?QScatterSeries)from?Ui_themewidget?import?Ui_ThemeWidgetForm?class?Data():????def?__init__(self,?point,?label):???????self.point?=?point???????self.label?=?label?class?ThemeWidget(QWidget):????def?__init__(self,?parent?=?None):????????super(ThemeWidget,?self).__init__(parent)????????????????self.listCount?=?3????????self.valueMax?=?10????????self.valueCount?=7????????self.generateRandomData()????????self.charts?=?[]????????????????self.ui?=?Ui_ThemeWidgetForm()????????self.ui.setupUi(self)????????????????self.populateThemeBox()????????self.populateAnimationBox()????????self.populateLegendBox()????????????????#生成圖表????????chartView?=?QChartView(self.createAreaChart())????????self.ui.gridLayout.addWidget(chartView,?1,?0)????????self.charts.append(chartView)????????????????chartView?=?QChartView(self.createPieChart())????????#如果餅圖切片標簽不適合屏幕,就會發生有趣的事情,因此我們忽略尺寸策略????????chartView.setSizePolicy(QSizePolicy.Ignored,?QSizePolicy.Ignored)????????self.ui.gridLayout.addWidget(chartView,?1,?1)????????self.charts.append(chartView)????????????????chartView?=?QChartView(self.createLineChart())????????self.ui.gridLayout.addWidget(chartView,?1,?2)????????self.charts.append(chartView)????????????????chartView?=?QChartView(self.createBarChart())????????self.ui.gridLayout.addWidget(chartView,?2,?0)????????self.charts.append(chartView)????????????????chartView?=?QChartView(self.createSplineChart())????????self.ui.gridLayout.addWidget(chartView,?2,?1)????????self.charts.append(chartView)????????????????chartView?=?QChartView(self.createScatterChart())????????self.ui.gridLayout.addWidget(chartView,?2,?2)????????self.charts.append(chartView)????????????????#設置缺省值????????self.ui.antialiasCheckBox.setChecked(True)????????????????#設置缺省主題為亮色主題????????pal?=?app.palette()????????pal.setColor(QPalette.Window,?QColor(0xf0f0f0))????????pal.setColor(QPalette.WindowText,?QColor(0x404044))????????app.setPalette(pal)????????????????self.updateUI()?????def?__del__(self):????????del?self.ui????????def?generateRandomData(self):????????self.dataTable?=?[]????????????????#生成隨機數據????????for?i?in?range(self.listCount):????????????dataList?=?[]????????????yValue?=?0????????????for?j?in?range(self.valueCount):????????????????yValue?=?yValue?+?QRandomGenerator.global_().bounded(self.valueMax?/?self.valueCount)????????????????xValue?=?(j?+?QRandomGenerator.global_().generateDouble())?*?(self.valueMax?/?self.valueCount)????????????????label?=?'片?'?+?str(i)?+?':'?+?str(j)????????????????dataList.append(Data(QPointF(xValue,?yValue),?label))????????????self.dataTable.append(dataList)????????????def?populateThemeBox(self):????????#添加主題選項????????self.ui.themeComboBox.addItem('淺色',?QChart.ChartThemeLight)????????self.ui.themeComboBox.addItem('天藍',?QChart.ChartThemeBlueCerulean)????????self.ui.themeComboBox.addItem('深色',?QChart.ChartThemeDark)????????self.ui.themeComboBox.addItem('棕色',?QChart.ChartThemeBrownSand)????????self.ui.themeComboBox.addItem('藍色NCS',?QChart.ChartThemeBlueNcs)????????self.ui.themeComboBox.addItem('高對比',?QChart.ChartThemeHighContrast)????????self.ui.themeComboBox.addItem('藍色Icy',?QChart.ChartThemeBlueIcy)????????self.ui.themeComboBox.addItem('Qt',?QChart.ChartThemeQt)????????????def?populateAnimationBox(self):????????self.ui.animatedComboBox.addItem('無動畫',?QChart.NoAnimation)????????self.ui.animatedComboBox.addItem('網格坐標軸動畫',?QChart.GridAxisAnimations)????????self.ui.animatedComboBox.addItem('圖表序列動畫',?QChart.SeriesAnimations)????????self.ui.animatedComboBox.addItem('全部應用動畫',?QChart.AllAnimations)????????????def?populateLegendBox(self):????????#為圖例選擇框添加選項????????self.ui.legendComboBox.addItem('不顯示圖例',?0)????????self.ui.legendComboBox.addItem('圖例頂端對齊',?Qt.AlignTop)????????self.ui.legendComboBox.addItem('圖例底端對齊',?Qt.AlignBottom)????????self.ui.legendComboBox.addItem('圖例左對齊',?Qt.AlignLeft)????????self.ui.legendComboBox.addItem('圖例右對齊',?Qt.AlignRight)????????????def?createAreaChart(self):????????chart?=?QChart()????????chart.setTitle('面積圖')????????????????lowerSeries?=?None????????nameIndex?=?0????????for?i,dataList?in?enumerate(self.dataTable):????????????upperSeries?=?QLineSeries(chart)????????????for?j,?data?in?enumerate(dataList):????????????????if?not?lowerSeries?is?None:????????????????????points?=?lowerSeries.pointsVector()????????????????????upperSeries.append(QPointF(j,?points[i].y()?+?data.point.y()))????????????????else:????????????????????upperSeries.append(QPointF(j,?data.point.y()))????????????area?=?QAreaSeries(upperSeries,?lowerSeries)????????????area.setName('面積:'?+?str(nameIndex))????????????nameIndex?+=?1????????????chart.addSeries(area)????????????lowerSeries?=?upperSeries????????????????????chart.createDefaultAxes()????????axisX?=?chart.axes(Qt.Horizontal)[0]????????axisY?=?chart.axes(Qt.Vertical)[0]????????axisX.setRange(0,?self.valueCount?-?1)????????axisY.setRange(0,?self.valueMax)????????#設置y軸標簽格式????????axisY.setLabelFormat('%.1f?')????????????????return?chart????????def?createBarChart(self):????????chart?=?QChart()????????chart.setTitle('柱形圖')????????????????series?=?QStackedBarSeries(chart)????????for?i,dataList?in?enumerate(self.dataTable):????????????barSet?=?QBarSet('條?'?+?str(i))????????????for?data?in?dataList:????????????????barSet?<0?and?self.charts[0].chart().theme()?!=?theme:????????????for?chartView?in?self.charts:????????????????chartView.chart().setTheme(theme)?????????????????????#根據選擇的主題設置調色板????????pal?=?app.palette()????????if?theme?==?QChart.ChartThemeLight:????????????pal.setColor(QPalette.Window,?QColor(0xf0f0f0))????????????pal.setColor(QPalette.WindowText,?QColor(0x404044))????????elif?theme?==?QChart.ChartThemeDark:????????????pal.setColor(QPalette.Window,?QColor(0x121218))????????????pal.setColor(QPalette.WindowText,?QColor(0xd6d6d6))????????elif?theme?==?QChart.ChartThemeBlueCerulean:????????????pal.setColor(QPalette.Window,?QColor(0x40434a))????????????pal.setColor(QPalette.WindowText,?QColor(0xd6d6d6))?????????elif?theme?==?QChart.ChartThemeBrownSand:????????????pal.setColor(QPalette.Window,?QColor(0x9e8965))????????????pal.setColor(QPalette.WindowText,?QColor(0x404044))????????elif?theme?==?QChart.ChartThemeBlueNcs:????????????pal.setColor(QPalette.Window,?QColor(0x018bba))????????????pal.setColor(QPalette.WindowText,?QColor(0x404044))????????elif?theme?==?QChart.ChartThemeHighContrast:????????????pal.setColor(QPalette.Window,?QColor(0xffab03))????????????pal.setColor(QPalette.WindowText,?QColor(0x181818))????????elif?theme?==?QChart.ChartThemeBlueIcy:????????????pal.setColor(QPalette.Window,?QColor(0xcee7f0))????????????pal.setColor(QPalette.WindowText,?QColor(0x404044))????????else:????????????pal.setColor(QPalette.Window,?QColor(0xf0f0f0))????????????pal.setColor(QPalette.WindowText,?QColor(0x404044))????????app.setPalette(pal)????????????????#抗鋸齒????????checked?=?self.ui.antialiasCheckBox.isChecked()????????for?chartView?in?self.charts:????????????????chartView.setRenderHint(QPainter.Antialiasing,?checked)????????????????#動畫????????options?=?self.ui.animatedComboBox.itemData(self.ui.animatedComboBox.currentIndex())????????if?len(self.charts)>0?and?self.charts[0].chart().animationOptions()?!=?options:????????????for?chartView?in?self.charts:????????????????chartView.chart().setAnimationOptions(QChart.AnimationOptions(options))????????????????????????#圖例對齊方式????????alignment?=?self.ui.legendComboBox.itemData(self.ui.legendComboBox.currentIndex())????????if?not?alignment:????????????for?chartView?in?self.charts:????????????????chartView.chart().legend().hide()????????else:?????????????for?chartView?in?self.charts:????????????????chartView.chart().legend().setAlignment(Qt.Alignment(alignment))????????????????chartView.chart().legend().show()?????class?DemoChartTheme(QMainWindow):????def?__init__(self,?parent=None):????????super(DemoChartTheme,?self).__init__(parent)????????????????????#?設置窗口標題????????self.setWindowTitle('實戰?Qt?for?Python:?圖表主題演示')??????????????#?設置窗口大小????????self.resize(960,?640)????????????????self.setCentralWidget(ThemeWidget())?????????????????if?__name__?==?'__main__':????app?=?QApplication(sys.argv)????window?=?DemoChartTheme()????window.show() sys.exit(app.exec())???運行結果如下圖:
圖表主題演示
本文知識點
- QChart有多種主題風格可以切換。
- 使用PyQt5的隨機數生成器生成圖表序列數據。
- 使用QPalette切換應用背景顏色。
前一篇: 實戰PyQt5: 155-QChart圖表之極坐標圖表
總結
以上是生活随笔為你收集整理的qchart 坐标轴设置_实战PyQt5: 156-QChart图表之更换图表主题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: can总线rollingcounter_
- 下一篇: apache php 调优_Apache