当前位置:   article > 正文

PyQt 调用pyecharts_pyecharts pyqt

pyecharts pyqt

方法一

用Qt QWebEngineView 加载本地  html文件,可用于展示静态数据。

  1. from pyecharts import Geo
  2. from PyQt5.QtCore import QUrl
  3. from PyQt5.QtWidgets import QApplication,QWidget,QHBoxLayout,QFrame
  4. from PyQt5.QtWebEngineWidgets import QWebEngineView
  5. import sys
  6. class Stacked(QWidget):
  7. def __init__(self):
  8. super(Stacked, self).__init__()
  9. self.initData()
  10. self.initUI()
  11. self.mainLayout()
  12. def initUI(self):
  13. self.setGeometry(400,400,800,600)
  14. self.setWindowTitle(" ")
  15. def initData(self):
  16. list_city_name=["100,30-1","130,40-2"]
  17. list_num = [["value1","value2"],["value3"]]
  18. geo_cities_coords = {"100,30-1":[100,30],"130,40-2":[130,40]}
  19. geo = Geo("Geo 实例")
  20. geo.add("",list_city_name, list_num, geo_cities_coords=geo_cities_coords)
  21. geo.render() #生成html文件
  22. def mainLayout(self):
  23. self.mainhboxLayout = QHBoxLayout(self)
  24. self.frame = QFrame(self)
  25. self.mainhboxLayout.addWidget(self.frame)
  26. self.hboxLayout = QHBoxLayout(self.frame)
  27. self.myHtml = QWebEngineView()
  28. #打开本地html文件
  29. self.myHtml.load(QUrl("file:///render.html"))
  30. self.hboxLayout.addWidget(self.myHtml)
  31. self.setLayout(self.mainhboxLayout)
  32. if __name__ == '__main__':
  33. app = QApplication(sys.argv)
  34. ex = Stacked()
  35. ex.show()
  36. sys.exit(app.exec_())

 

方法二

直接调用 javascript,可与数据进行交互

  1. from PyQt5.QtCore import QUrl, Qt
  2. from PyQt5.QtWebEngineWidgets import QWebEngineView
  3. from PyQt5.QtWidgets import QDialog, QApplication, QHBoxLayout, QWidget, QGridLayout, QLabel, QSpinBox, \
  4. QSpacerItem, QSizePolicy, QComboBox
  5. from random import randint
  6. from pyecharts import Bar, Pie, Line, Overlap
  7. from pyecharts_javascripthon.api import TRANSLATOR
  8. TITLE_TEXT = "" #主标题
  9. TITLE_SUBTEXT = "" #副标题
  10. ATTR = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  11. class Form(QWidget):
  12. def __init__(self):
  13. super(Form, self).__init__()
  14. self.view = None
  15. self.echarts = False
  16. self.initUi()
  17. self.load_url()
  18. def initUi(self):
  19. self.hl = QHBoxLayout(self)
  20. self.widget = QWidget()
  21. self.gl = QGridLayout(self.widget)
  22. # ATTR1
  23. label1 = QLabel(ATTR[0] + ':')
  24. self.gl.addWidget(label1, 1 - 1, 0, 1, 1)
  25. self.spinbox1 = QSpinBox()
  26. self.spinbox1.setSingleStep(100)
  27. self.spinbox1.setObjectName('spinbox')
  28. self.spinbox1.valueChanged.connect(self.set_options)
  29. self.spinbox1.setMaximum(1000)
  30. self.spinbox1.setValue(randint(0, 1000))
  31. self.gl.addWidget(self.spinbox1, 1 - 1, 1, 1, 1)
  32. # ATTR2
  33. label2 = QLabel(ATTR[1] + ':')
  34. self.gl.addWidget(label2, 2 - 1, 0, 1, 1)
  35. self.spinbox2 = QSpinBox()
  36. self.spinbox2.setSingleStep(100)
  37. self.spinbox2.setObjectName('spinbox')
  38. self.spinbox2.valueChanged.connect(self.set_options)
  39. self.spinbox2.setMaximum(1000)
  40. self.spinbox2.setValue(randint(0, 1000))
  41. self.gl.addWidget(self.spinbox2, 2 - 1, 1, 1, 1)
  42. # ATTR3
  43. label3 = QLabel(ATTR[2] + ':')
  44. self.gl.addWidget(label3, 3 - 1, 0, 1, 1)
  45. self.spinbox3 = QSpinBox()
  46. self.spinbox3.setSingleStep(100)
  47. self.spinbox3.setObjectName('spinbox')
  48. self.spinbox3.valueChanged.connect(self.set_options)
  49. self.spinbox3.setMaximum(1000)
  50. self.spinbox3.setValue(randint(0, 1000))
  51. self.gl.addWidget(self.spinbox3, 3 - 1, 1, 1, 1)
  52. # ATTR4
  53. label4 = QLabel(ATTR[3] + ':')
  54. self.gl.addWidget(label4, 4 - 1, 0, 1, 1)
  55. self.spinbox4 = QSpinBox()
  56. self.spinbox4.setSingleStep(100)
  57. self.spinbox4.setObjectName('spinbox')
  58. self.spinbox4.valueChanged.connect(self.set_options)
  59. self.spinbox4.setMaximum(1000)
  60. self.spinbox4.setValue(randint(0, 1000))
  61. self.gl.addWidget(self.spinbox4, 4 - 1, 1, 1, 1)
  62. # ATTR5
  63. label5 = QLabel(ATTR[4] + ':')
  64. self.gl.addWidget(label5, 5 - 1, 0, 1, 1)
  65. self.spinbox5 = QSpinBox()
  66. self.spinbox5.setSingleStep(100)
  67. self.spinbox5.setObjectName('spinbox')
  68. self.spinbox5.valueChanged.connect(self.set_options)
  69. self.spinbox5.setMaximum(1000)
  70. self.spinbox5.setValue(randint(0, 1000))
  71. self.gl.addWidget(self.spinbox5, 5 - 1, 1, 1, 1)
  72. # ATTR6
  73. label6 = QLabel(ATTR[5] + ':')
  74. self.gl.addWidget(label6, 6 - 1, 0, 1, 1)
  75. self.spinbox6 = QSpinBox()
  76. self.spinbox6.setSingleStep(100)
  77. self.spinbox6.setObjectName('spinbox')
  78. self.spinbox6.valueChanged.connect(self.set_options)
  79. self.spinbox6.setMaximum(1000)
  80. self.spinbox6.setValue(randint(0, 1000))
  81. self.gl.addWidget(self.spinbox6, 6 - 1, 1, 1, 1)
  82. self.hl.addWidget(self.widget)
  83. vs = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
  84. self.gl.addItem(vs, 6, 0, 1, 2)
  85. self.combobox_type = QComboBox()
  86. self.combobox_type.currentIndexChanged.connect(self.reload_canvas)
  87. self.combobox_type.addItems(['饼图', '柱状图', '折线图', '折线、柱状图'])
  88. self.gl.addWidget(self.combobox_type, 7, 0, 1, 2)
  89. self.combobox_theme = QComboBox()
  90. self.combobox_theme.currentTextChanged.connect(self.change_theme)
  91. self.combobox_theme.addItems(['light', 'dark'])
  92. self.gl.addWidget(self.combobox_theme, 8, 0, 1, 2)
  93. # 添加web view
  94. self.view = QWebEngineView()
  95. self.view.setContextMenuPolicy(Qt.NoContextMenu)
  96. self.hl.addWidget(self.view)
  97. def change_theme(self, theme):
  98. if not self.view:
  99. return
  100. options = self.get_options()
  101. if not options:
  102. return
  103. self.view.page().runJavaScript(
  104. f'''
  105. myChart.dispose();
  106. var myChart = echarts.init(document.getElementById('container'), '{theme}', {{renderer: 'canvas'}});
  107. myChart.clear();
  108. var option = eval({options});
  109. myChart.setOption(option);
  110. '''
  111. )
  112. def load_url(self):
  113. url = QUrl("file:///template.html")
  114. self.view.load(url)
  115. self.view.loadFinished.connect(self.set_options)
  116. def reload_canvas(self):
  117. if not self.view:
  118. return
  119. # 重载画布
  120. options = self.get_options()
  121. if not options:
  122. return
  123. self.view.page().runJavaScript(
  124. f'''
  125. myChart.clear();
  126. var option = eval({options});
  127. myChart.setOption(option);
  128. '''
  129. )
  130. def set_options(self):
  131. if not self.view:
  132. return
  133. if not self.echarts:
  134. # 初始化echarts
  135. self.view.page().runJavaScript(
  136. '''
  137. var myChart = echarts.init(document.getElementById('container'), 'light', {renderer: 'canvas'});
  138. '''
  139. )
  140. self.echarts = True
  141. options = self.get_options()
  142. if not options:
  143. return
  144. self.view.page().runJavaScript(
  145. f'''
  146. var option = eval({options});
  147. myChart.setOption(option);
  148. '''
  149. )
  150. def get_options(self):
  151. v1, v2, v3, v4, v5, v6 = self.spinbox1.value(), self.spinbox2.value(), self.spinbox3.value(), self.spinbox4.value(), \
  152. self.spinbox5.value(), self.spinbox6.value()
  153. v = [v1, v2, v3, v4, v5, v6]
  154. if self.combobox_type.currentIndex() == 0:
  155. # 饼图
  156. options = self.create_pie(v)
  157. elif self.combobox_type.currentIndex() == 1:
  158. # 柱状图
  159. options = self.create_bar(v)
  160. elif self.combobox_type.currentIndex() == 2:
  161. # 折线图
  162. options = self.create_line(v)
  163. elif self.combobox_type.currentIndex() == 3:
  164. # 折线、柱状图
  165. options = self.create_line_bar(v)
  166. else:
  167. return
  168. return options
  169. def create_pie(self, v):
  170. pie = Pie(TITLE_TEXT, TITLE_SUBTEXT)
  171. pie.add("商家", ATTR, v, is_label_show=True)
  172. snippet = TRANSLATOR.translate(pie.options)
  173. options = snippet.as_snippet()
  174. return options
  175. def create_bar(self, v):
  176. bar = Bar(TITLE_TEXT, TITLE_SUBTEXT)
  177. bar.add('商家1', ATTR, v, is_more_utils=True)
  178. bar.add('商家2', ATTR, v, is_more_utils=True)
  179. snippet = TRANSLATOR.translate(bar.options)
  180. options = snippet.as_snippet()
  181. return options
  182. def create_line(self, v):
  183. line = Line(TITLE_TEXT, TITLE_SUBTEXT)
  184. line.add("商家", ATTR, v, is_smooth=True, mark_line=["max", "average"])
  185. snippet = TRANSLATOR.translate(line.options)
  186. options = snippet.as_snippet()
  187. return options
  188. def create_line_bar(self, v):
  189. line = Line(TITLE_TEXT, TITLE_SUBTEXT)
  190. line.add("商家", ATTR, v, is_smooth=True, mark_line=["max", "average"])
  191. bar = Bar(TITLE_TEXT, TITLE_SUBTEXT)
  192. bar.add('商家', ATTR, v, is_more_utils=True)
  193. overlap = Overlap()
  194. overlap.add(line)
  195. overlap.add(bar)
  196. snippet = TRANSLATOR.translate(overlap.options)
  197. options = snippet.as_snippet()
  198. return options
  199. if __name__ == '__main__':
  200. import sys
  201. app = QApplication(sys.argv)
  202. app.setStyle('fusion')
  203. form = Form()
  204. form.show()
  205. sys.exit(app.exec_())

参考:https://blog.csdn.net/this_is_id/article/details/86688585

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/570819
推荐阅读
相关标签
  

闽ICP备14008679号