def draw(self): # 修改 Chart 大小为外外层 graphicsView 大小 width = self.main_ui.graphicsView.width() - 5 height = self.main_ui.graphicsView.height() - 5 self.resize(width, height) # 创建5个柱子 set0 = QBarSet('下载') set1 = QBarSet('上传') for s in [set0, set1]: for i in range(6): s << random() * 10 # 创建柱状条 series = QHorizontalBarSeries() series.append(set0) series.append(set1) # 添加Series self.removeAllSeries() self.addSeries(series) # 分类 categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'] # 分类x轴 axis = QBarCategoryAxis() axis.append(categories) # 创建默认轴线 self.createDefaultAxes() # 替换默认y轴 self.setAxisY(axis, series) # 显示图例 self.legend().setVisible(True) self.legend().setAlignment(Qt.AlignBottom)
def XDictSet(data_dict, chart_type='bar', key_order=None): known_chart_types = ['bar', 'hbar', 'box', 'candlestick'] if chart_type in known_chart_types: if key_order == None: key_order = data_dict.keys() def dictloop(data, ko, QSeries, QSet): for key in ko: set = QSet(key) for item in data[key]: try: set << float(item) except (ValueError, TypeError): set << 0.0 QSeries.append(set) return QSeries if chart_type == 'bar': series = dictloop(data_dict, key_order, QBarSeries(), QBarSet) if chart_type == 'hbar': series = dictloop(data_dict, key_order, QHorizontalBarSeries(), QBarSet) elif chart_type == 'box': series = dictloop(data_dict, key_order, QBoxPlotSeries(), QBoxSet) elif chart_type == 'candlestick': series = dictloop(data_dict, key_order, QCandlestickSeries(), QCandlestickSet) else: raise ValueError('chart_type is unknown') pass return series
def create_graph(self): self.emotion_set = QBarSet('Confidence Level') self.emotion_set.append([ self.face_anger_digust, self.face_happy, self.face_neutral, self.face_sadness, self.face_surprise_fear ]) series = QHorizontalBarSeries() series.append(self.emotion_set) chart = QChart() chart.addSeries(series) chart.setTitle('ReLuu FaceReader') chart.setAnimationOptions(QChart.SeriesAnimations) months = ('Angery and Disgusted', 'Happy', 'Neutral', 'Sadness', 'Fear and Surprise') axisY = QBarCategoryAxis() axisY.append(months) chart.addAxis(axisY, Qt.AlignLeft) series.attachAxis(axisY) axisX = QValueAxis() axisX.setMax(1.0) chart.addAxis(axisX, Qt.AlignBottom) series.attachAxis(axisX) axisX.applyNiceNumbers() chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(chartView)
def getSeries(self): # 创建5个柱子 set0 = QBarSet('Jane') set1 = QBarSet('John') set2 = QBarSet('Axel') set3 = QBarSet('Mary') set4 = QBarSet('Samantha') # 添加数据 set0 << 1 << 2 << 3 << 4 << 5 << 6 set1 << 5 << 0 << 0 << 4 << 0 << 7 set2 << 3 << 5 << 8 << 13 << 8 << 5 set3 << 5 << 6 << 7 << 3 << 4 << 5 set4 << 9 << 7 << 5 << 3 << 1 << 2 # 创建柱状条 series = QHorizontalBarSeries() series.append(set0) series.append(set1) series.append(set2) series.append(set3) series.append(set4) return series
def draw(self): # 通过 UI 单选框的状态判断选项 if self.main_ui.radio_ip.isChecked(): plotCategory = 'IP' else: plotCategory = 'protocol' if self.main_ui.radio_packet.isChecked(): plotUnit = 'packet' else: plotUnit = 'length' # 根据选项调整 sql 查询语法 sql = ''' SELECT {0}, upload_{1} AS upload, download_{1} AS download FROM network_monitor.{0}_upload_download ORDER BY all_{1} DESC LIMIT {2};''' sql = sql.format(plotCategory, plotUnit, PLOT_NUMBER) sql_values = run_sql_with_result(sql) # 查询结果是 list 里面套 dict try: sql_values.reverse() # 如果 except 说明 sql_values 为空 except: self.removeAllSeries() # 删除掉已经绘制的图像 return if plotUnit == 'length': # 如果按包大小绘图,为了美观,需要调整单位和用于绘图的数据 max_value = 0 for i in sql_values: for j in ['download', 'upload']: max_value = max(max_value, i[j]) (unit, base) = speed2str(max_value) for i in sql_values: for j in ['download', 'upload']: i[j] /= base unit_str = ',单位:' + unit + 'B' else: unit_str = ',单位:包' # 每组创建两个柱状条 set0 = QBarSet('下载' + unit_str) set1 = QBarSet('上传' + unit_str) for i in range(len(sql_values)): # PyQt 的传入数据的语法,估计是跟 C++ 的 Qt 学的 set0 << sql_values[i]['download'] set1 << sql_values[i]['upload'] # 创建横向柱状条 series = QHorizontalBarSeries() series.append(set0) series.append(set1) # 添加Series self.removeAllSeries() self.addSeries(series) # 分类 categories = [x[plotCategory] for x in sql_values] # 分类 x 轴,注意 x 轴在横向柱状图中是竖着的轴 axis = QBarCategoryAxis() axis.append(categories) # 创建默认轴线 self.createDefaultAxes() # 替换默认y轴 self.setAxisY(axis, series) # 显示图例 self.legend().setVisible(True) self.legend().setAlignment(Qt.AlignBottom)
def run_statistics(self): text = self.comp_type_crt_text x_vals = [] y_vals = [] if text[:5] == "Inter": data_sets = [] for check_box in self.check_boxes: if check_box.isChecked(): data_sets.append(self.check_boxes[check_box]) metric = self.select_metric_inter.currentText() metric_alias = self.dict_metric_inter[metric] res = ResultsManager.get_results_inter(metric_alias, data_sets) # self.graphWidget.plot(data_sets, res) x_vals = data_sets y_vals = res elif text[:5] == "Intra": ds_name = self.intra_ds_current metric = self.select_metric_intra.currentText() metric_alias = self.dict_metric_intra[metric] names, res = ResultsManager.get_results_intra(metric_alias, ds_name) # self.graphWidget.plot(names, res) x_vals = names y_vals = res #horizontal bar chart_view = None if self.select_chart.currentIndex() == 0: chart = QChart(flags=Qt.WindowFlags()) series = QHorizontalBarSeries() for i in range(len(x_vals)): name = x_vals[i] set0 = QBarSet(name) set0.append(y_vals[i]) series.append(set0) chart.addSeries(series) if text[:5] == "Inter": chart.setTitle(f"Comparison by {self.select_metric_inter.currentText()}") else: chart.setTitle(f"Comparison by {self.select_metric_intra.currentText()}") chart.setAnimationOptions(QChart.SeriesAnimations) chart_view = QChartView(chart) self.graph_layout.replaceWidget(self.crt_chart, chart_view) self.crt_chart = chart_view axisX = QValueAxis() chart.addAxis(axisX, Qt.AlignBottom) series.attachAxis(axisX) axisX.applyNiceNumbers() chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chart_view.setRenderHint(QPainter.Antialiasing) chart_view.setBackgroundBrush(QColor(0, 0, 0, 255)) chart.setBackgroundBrush(QColor(255, 255, 0, 255)) # vertical bar elif self.select_chart.currentIndex() == 1: chart = QChart(flags=Qt.WindowFlags()) series = QBarSeries() for i in range(len(x_vals)): name = x_vals[i] set0 = QBarSet(name) set0.append(y_vals[i]) series.append(set0) chart.addSeries(series) if text[:5] == "Inter": chart.setTitle(f"Comparison by {self.select_metric_inter.currentText()}") else: chart.setTitle(f"Comparison by {self.select_metric_intra.currentText()}") chart.setAnimationOptions(QChart.SeriesAnimations) chart_view = QChartView(chart) self.graph_layout.replaceWidget(self.crt_chart, chart_view) self.crt_chart = chart_view axisY = QValueAxis() chart.addAxis(axisY, Qt.AlignLeft) series.attachAxis(axisY) axisY.applyNiceNumbers() chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chart_view.setRenderHint(QPainter.Antialiasing) chart_view.setBackgroundBrush(QColor(0, 0, 0, 255)) chart.setBackgroundBrush(QColor(255, 255, 0, 255)) # pie chart elif self.select_chart.currentIndex() == 2: chart = QChart(flags=Qt.WindowFlags()) series = QPieSeries() for i in range(len(x_vals)): series.append(x_vals[i], y_vals[i]) chart.addSeries(series) if text[:5] == "Inter": chart.setTitle(f"Comparison by {self.select_metric_inter.currentText()}") else: chart.setTitle(f"Comparison by {self.select_metric_intra.currentText()}") chart.setAnimationOptions(QChart.SeriesAnimations) chart.legend().setAlignment(Qt.AlignBottom) # chart.legend().setFont(QFont("Arial", 12)) chart_view = QChartView(chart) self.graph_layout.replaceWidget(self.crt_chart, chart_view) self.crt_chart = chart_view chart_view.setRenderHint(QPainter.Antialiasing) chart_view.setBackgroundBrush(QColor(0, 0, 0, 255)) chart.setBackgroundBrush(QColor(255, 255, 0, 255)) chart_view.setRubberBand(QChartView.HorizontalRubberBand) chart_view.setRubberBand(QChartView.VerticalRubberBand)
def analysisUI(self, smwui): btn_refresh = QPushButton('Refresh') btn_refresh.clicked.connect(smwui.update_chart) smwui.cb_targetAxis1 = QCheckBox('X : Horizontal action') smwui.cb_targetAxis1.setChecked(True) smwui.cb_targetAxis1.stateChanged.connect(smwui.update_chart) smwui.cb_targetAxis2 = QCheckBox('Y : Vertical action') smwui.cb_targetAxis2.setChecked(True) smwui.cb_targetAxis2.stateChanged.connect(smwui.update_chart) smwui.cb_targetAxis3 = QCheckBox('Z : Depth action') smwui.cb_targetAxis3.setChecked(True) smwui.cb_targetAxis3.stateChanged.connect(smwui.update_chart) smwui.cb_targetValue1 = QCheckBox('Head') smwui.cb_targetValue1.setChecked(True) smwui.cb_targetValue2 = QCheckBox('ShoulderRight') smwui.cb_targetValue3 = QCheckBox('ElbowRight') smwui.cb_targetValue4 = QCheckBox('HandRight') smwui.cb_targetValue4.setChecked(True) smwui.cb_targetValue5 = QCheckBox('SpineBase') smwui.cb_targetValue6 = QCheckBox('HandLeft') smwui.cb_targetValue6.setChecked(True) smwui.cb_targetValue7 = QCheckBox('ElbowLeft') smwui.cb_targetValue8 = QCheckBox('ShoulderLeft') smwui.cb_targetValue9 = QCheckBox('Neck') smwui.cb_targetValue1.stateChanged.connect(smwui.update_chart) smwui.cb_targetValue2.stateChanged.connect(smwui.update_chart) smwui.cb_targetValue3.stateChanged.connect(smwui.update_chart) smwui.cb_targetValue4.stateChanged.connect(smwui.update_chart) smwui.cb_targetValue5.stateChanged.connect(smwui.update_chart) smwui.cb_targetValue6.stateChanged.connect(smwui.update_chart) smwui.cb_targetValue7.stateChanged.connect(smwui.update_chart) smwui.cb_targetValue8.stateChanged.connect(smwui.update_chart) smwui.cb_targetValue9.stateChanged.connect(smwui.update_chart) ### chart set1 = QBarSet("X") set2 = QBarSet("Y") set3 = QBarSet("Z") set1.setColor(QColor(0, 0, 200, 150)) set2.setColor(QColor(0, 200, 0, 150)) set3.setColor(QColor(200, 50, 0, 150)) series = QHorizontalBarSeries() series.append(set1) series.append(set2) series.append(set3) categories = [] categories.append('Parts') axisX = QValueAxis() axisX.applyNiceNumbers() axisY = QBarCategoryAxis() axisY.append(categories) chart = QChart() chart.addSeries(series) chart.setTitle("Timeseries Distance") chart.setAnimationOptions(QChart.SeriesAnimations) chart.setAxisX(axisX, series) chart.setAxisY(axisY) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignTop) smwui.chartView = QChartView(chart) smwui.chartView.setRenderHint(QPainter.Antialiasing) ### draw parts smwui.viewParts = QQuickWidget() smwui.viewParts.setSource(QUrl("./qml/qmlptviewer/main.qml")) smwui.viewParts.rootContext().setContextProperty( "HandRight", "0,0,0,0") smwui.viewParts.rootContext().setContextProperty("HandLeft", "0,0,0,0") smwui.viewParts.rootContext().setContextProperty( "ElbowRight", "0,0,0,0") smwui.viewParts.rootContext().setContextProperty( "ElbowLeft", "0,0,0,0") smwui.viewParts.rootContext().setContextProperty("Head", "0,0,0,0") scrollArea_viewParts = QScrollArea() scrollArea_viewParts.setMinimumWidth(420) scrollArea_viewParts.setWidget(smwui.viewParts) scrollArea_viewParts.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) ### |1| vbox1 = QVBoxLayout() vbox1.addWidget(smwui.cb_targetAxis1) vbox1.addWidget(smwui.cb_targetAxis2) vbox1.addWidget(smwui.cb_targetAxis3) gb_targetAxis = QGroupBox() gb_targetAxis.setTitle('Axis') gb_targetAxis.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Maximum) gb_targetAxis.setLayout(vbox1) ### |2| vbox2 = QVBoxLayout() vbox2.addWidget(smwui.cb_targetValue1) vbox2.addWidget(smwui.cb_targetValue2) vbox2.addWidget(smwui.cb_targetValue3) vbox2.addWidget(smwui.cb_targetValue4) vbox2.addWidget(smwui.cb_targetValue5) vbox2.addWidget(smwui.cb_targetValue6) vbox2.addWidget(smwui.cb_targetValue7) vbox2.addWidget(smwui.cb_targetValue8) vbox2.addWidget(smwui.cb_targetValue9) gb_targetValue = QGroupBox() gb_targetValue.setTitle('Parts') gb_targetValue.setLayout(vbox2) vbox3 = QVBoxLayout() vbox3.addWidget(btn_refresh) vbox3.addWidget(gb_targetAxis) vbox3.addWidget(gb_targetValue) ### -1- hbox = QHBoxLayout() hbox.addLayout(vbox3) hbox.addWidget(smwui.chartView) hbox.addWidget(scrollArea_viewParts) self.gb_analysis.setTitle("Analysis") self.gb_analysis.setLayout(hbox)
def update_chart(self): if not self.cb_targetAxis1.checkState( ) and not self.cb_targetAxis2.checkState( ) and not self.cb_targetAxis3.checkState(): return -1 ## draw chart set1 = QBarSet("X") set2 = QBarSet("Y") set3 = QBarSet("Z") set1.setColor(QColor(0, 0, 200, 180)) set2.setColor(QColor(0, 200, 0, 180)) set3.setColor(QColor(200, 100, 0, 180)) self.categories = [] self.set1_val = np.array([]) self.set2_val = np.array([]) self.set3_val = np.array([]) for i in range(len(self.parts_dict)): flag_add = False if self.parts_dict[self.cb_targetValue1.text( )] == i and self.cb_targetValue1.checkState(): flag_add = True self.categories.append(self.cb_targetValue1.text()) elif self.parts_dict[self.cb_targetValue2.text( )] == i and self.cb_targetValue2.checkState(): flag_add = True self.categories.append(self.cb_targetValue2.text()) elif self.parts_dict[self.cb_targetValue3.text( )] == i and self.cb_targetValue3.checkState(): flag_add = True self.categories.append(self.cb_targetValue3.text()) elif self.parts_dict[self.cb_targetValue4.text( )] == i and self.cb_targetValue4.checkState(): flag_add = True self.categories.append(self.cb_targetValue4.text()) elif self.parts_dict[self.cb_targetValue5.text( )] == i and self.cb_targetValue5.checkState(): flag_add = True self.categories.append(self.cb_targetValue5.text()) elif self.parts_dict[self.cb_targetValue6.text( )] == i and self.cb_targetValue6.checkState(): flag_add = True self.categories.append(self.cb_targetValue6.text()) elif self.parts_dict[self.cb_targetValue7.text( )] == i and self.cb_targetValue7.checkState(): flag_add = True self.categories.append(self.cb_targetValue7.text()) elif self.parts_dict[self.cb_targetValue8.text( )] == i and self.cb_targetValue8.checkState(): flag_add = True self.categories.append(self.cb_targetValue8.text()) elif self.parts_dict[self.cb_targetValue9.text( )] == i and self.cb_targetValue9.checkState(): flag_add = True self.categories.append(self.cb_targetValue9.text()) if flag_add: set1 << self.ctd.distanceList[0 + 3 * i] set2 << self.ctd.distanceList[1 + 3 * i] set3 << self.ctd.distanceList[2 + 3 * i] self.set1_val = np.append(self.set1_val, [self.ctd.distanceList[0 + 3 * i]]) self.set2_val = np.append(self.set2_val, [self.ctd.distanceList[1 + 3 * i]]) self.set3_val = np.append(self.set3_val, [self.ctd.distanceList[2 + 3 * i]]) series = QHorizontalBarSeries() if self.cb_targetAxis1.checkState(): series.append(set1) else: self.set1_val = np.zeros(len(self.categories)) if self.cb_targetAxis2.checkState(): series.append(set2) else: self.set2_val = np.zeros(len(self.categories)) if self.cb_targetAxis3.checkState(): series.append(set3) else: self.set3_val = np.zeros(len(self.categories)) axisX = QValueAxis() axisX.applyNiceNumbers() axisY = QBarCategoryAxis() axisY.append(self.categories) chart = QChart() chart.setTitle("Timeseries Distance") chart.setAnimationOptions(QChart.SeriesAnimations) chart.addSeries(series) chart.setAxisX(axisX, series) chart.setAxisY(axisY) self.chartView.setChart(chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.update_parts()
app = QApplication(sys.argv) set0 = QBarSet("Jane") set1 = QBarSet("John") set2 = QBarSet("Axel") set3 = QBarSet("Mary") set4 = QBarSet("Samantha") set0 << 1 << 2 << 3 << 4 << 5 << 6 set1 << 5 << 0 << 0 << 4 << 0 << 7 set2 << 3 << 5 << 8 << 13 << 8 << 5 set3 << 5 << 6 << 7 << 3 << 4 << 5 set4 << 9 << 7 << 5 << 3 << 1 << 2 series = QHorizontalBarSeries() series.append(set0) series.append(set1) series.append(set2) series.append(set3) series.append(set4) chart = QChart() chart.addSeries(series) chart.setTitle("Simple horizontal barchart example") chart.setAnimationOptions(QChart.SeriesAnimations) categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] axis = QBarCategoryAxis() axis.append(categories) chart.createDefaultAxes()
def __init__(self): super().__init__() self.resize(800, 600) set0 = QBarSet('X0') set1 = QBarSet('X1') set2 = QBarSet('X2') set3 = QBarSet('X3') set4 = QBarSet('X4') 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 = QHorizontalBarSeries() series.append(set0) series.append(set1) series.append(set2) series.append(set3) series.append(set4) chart = QChart() chart.addSeries(series) chart.setTitle('Horizontal Bar Chart Demo') chart.setAnimationOptions(QChart.SeriesAnimations) months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June') axisY = QBarCategoryAxis() axisY.append(months) chart.addAxis(axisY, Qt.AlignLeft) series.attachAxis(axisY) axisX = QValueAxis() chart.addAxis(axisX, Qt.AlignBottom) series.attachAxis(axisX) axisX.applyNiceNumbers() chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(chartView)
def display_compare(self, result, group): # No data extracted if len(result) == 0: chart = QChart() chart.setTitle('NO DATA') self.setChart(chart) return t, result_array, new_keys = extract_multidim_results(result) chart = QChart() chart.setTitle(t.title()) chart.setAnimationOptions(QChart.SeriesAnimations) chart.setTitleFont(self.title_font) chart.setPlotAreaBackgroundPen(QPen(QColor('red'))) # Case with single value if len(new_keys) == 0: value = result[0][0] chart.setTitle(t.title() + ':\t %d' % value) elements = 1 # # series = QHorizontalBarSeries() # axis = QBarCategoryAxis() # bar_set = QBarSet('') # axis.append("%s" % value) # bar_set.append(value) # series.append(bar_set) # chart.addSeries(series) # chart.setAxisY(axis, series) # elements = 1 else: x_range = np.max(result_array) series = QHorizontalBarSeries() series.setLabelsFormat("<font color=\"black\"><b>@value<\\b><\\font>") series.setLabelsVisible(True) series.setLabelsPosition(QAbstractBarSeries.LabelsOutsideEnd) axis = QBarCategoryAxis() font = QFont() font.setPixelSize(1) chart.setFont(font) # 1D Data if len(result_array.shape) == 1: bar_set = QBarSet('') # We want to sort it such that rows with the most studies are on the top itr = sorted([(res, name) for (res, name) in zip(result_array, new_keys[0])]) for res, name in itr: # Take only the first part of the name if it has multiple parts name = str(name).split(',')[0] bar_set.append(res) axis.append(name) series.append(bar_set) elements = result_array.shape[0] chart.legend().hide() # 2D Data elif len(result_array.shape) == 2: # Sort countries/cities such that the one with the most studies is on top itr = sorted([(res, name) for (res, name) in zip(result_array, new_keys[0])], key=lambda c: c[0].sum()) bar_sets = [QBarSet(str(k)) for k in new_keys[1]] # Array with values for the 2nd category and name of the 1st dimension for res, name in itr: name = str(name).split(',')[0] for i, (r, name2) in enumerate(zip(res, new_keys[1])): bar_sets[i].append(r) axis.append(name) for bar_set in bar_sets: series.append(bar_set) elements = result_array.shape[0] * result_array.shape[1] # TODO: Implement visualization for 3D data else: return chart.addSeries(series) chart.createDefaultAxes() chart.setAxisY(axis, series) chart.axisX().setRange(0, 1.1 * x_range) chart.axisX().applyNiceNumbers() chart.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.MinimumExpanding) chart.setMinimumHeight(30 * elements) self.setChart(chart)