日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

qchart 坐标轴设置_实战PyQt5: 156-QChart图表之更换图表主题

發(fā)布時間:2025/3/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 qchart 坐标轴设置_实战PyQt5: 156-QChart图表之更换图表主题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

圖表主題

QChart定義了多種圖表主題,可以創(chuàng)建不同風(fēng)格的圖表顯示,在調(diào)整圖表主題風(fēng)格的時候,為了使整個應(yīng)用的風(fēng)格看起來更和諧一致,一般要使用應(yīng)用程序的背景調(diào)色板來調(diào)整應(yīng)用的顏色以適配圖表所選的主題。

QChat支持以下幾種風(fēng)格的圖表主題:

  • 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風(fēng)格(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())????????#如果餅圖切片標簽不適合屏幕,就會發(fā)生有趣的事情,因此我們忽略尺寸策略????????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)????????????????#設(shè)置缺省值????????self.ui.antialiasCheckBox.setChecked(True)????????????????#設(shè)置缺省主題為亮色主題????????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?=?[]????????????????#生成隨機數(shù)據(jù)????????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('網(wǎng)格坐標軸動畫',?QChart.GridAxisAnimations)????????self.ui.animatedComboBox.addItem('圖表序列動畫',?QChart.SeriesAnimations)????????self.ui.animatedComboBox.addItem('全部應(yīng)用動畫',?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)????????#設(shè)置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)?????????????????????#根據(jù)選擇的主題設(shè)置調(diào)色板????????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)????????????????????#?設(shè)置窗口標題????????self.setWindowTitle('實戰(zhàn)?Qt?for?Python:?圖表主題演示')??????????????#?設(shè)置窗口大小????????self.resize(960,?640)????????????????self.setCentralWidget(ThemeWidget())?????????????????if?__name__?==?'__main__':????app?=?QApplication(sys.argv)????window?=?DemoChartTheme()????window.show() sys.exit(app.exec())???

運行結(jié)果如下圖:

圖表主題演示

本文知識點

  • QChart有多種主題風(fēng)格可以切換。
  • 使用PyQt5的隨機數(shù)生成器生成圖表序列數(shù)據(jù)。
  • 使用QPalette切換應(yīng)用背景顏色。

前一篇: 實戰(zhàn)PyQt5: 155-QChart圖表之極坐標圖表

總結(jié)

以上是生活随笔為你收集整理的qchart 坐标轴设置_实战PyQt5: 156-QChart图表之更换图表主题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。