feat:完场直方图按类型进行打组展示

This commit is contained in:
duanshengchao 2025-09-02 16:28:32 +08:00
parent 605b8dd190
commit fae96a7b37
5 changed files with 51 additions and 34 deletions

View File

@ -7,7 +7,9 @@ dpBarsChart::dpBarsChart(QWidget* parent)
setAttribute(Qt::WA_TranslucentBackground,true);
m_pCustomPlot = new QCustomPlot(this);
m_barsGroup = new QCPBarsGroup(m_pCustomPlot);
/* m_pBarsGroup = new QCPBarsGroup(m_pCustomPlot);
m_pBarsGroup->setSpacingType(QCPBarsGroup::stAbsolute);
m_pBarsGroup->setSpacing(2); */
initQCP();
QBoxLayout* mainLayout = new QBoxLayout(QBoxLayout::LeftToRight);
@ -94,10 +96,12 @@ void dpBarsChart::viewHistoricalData(const QDateTime& dateTime)
void dpBarsChart::synchronizeConfigData(const configurationResults& cfg)
{
//先清除现有图形(直方图不像曲线一样有连续的过程展现,所以不采用动态更新,每次都清除、重新创建)
//m_pBarsGroup->clear();
m_pCustomPlot->clearPlottables();
m_bars.clear();
m_barsGroups.clear();
bool groupByType = false;
bool groupByType = cfg.groupByType;
//设定朝向
QCPAxis* keyAxis = nullptr;
@ -112,7 +116,7 @@ void dpBarsChart::synchronizeConfigData(const configurationResults& cfg)
keyAxis = m_pCustomPlot->yAxis;
valueAxis = m_pCustomPlot->xAxis;
}
if(!keyAxis && !valueAxis)
if(!keyAxis || !valueAxis)
return;
QVector<double> ticks;
@ -130,20 +134,6 @@ void dpBarsChart::synchronizeConfigData(const configurationResults& cfg)
QVariant colorData = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::DecorationRole);
RealTimeDataType dataType = (RealTimeDataType)cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_dataType).toInt();
/*QCPBarsGroup* barsGroup = nullptr;
if(!m_barsGroups.contains(dataType))
{
barsGroup = new QCPBarsGroup(m_pCustomPlot);
m_barsGroups.insert(dataType, barsGroup);
}
else
{
barsGroup = m_barsGroups.value(dataType);
}
if(!barsGroup)
continue;*/
Bars bars;
bars.name = itemText;
bars.dataID = dataID;
@ -156,7 +146,16 @@ void dpBarsChart::synchronizeConfigData(const configurationResults& cfg)
bars.qBars = qBars;
if(groupByType)
{
groupedBars[dataType].insert(dataID);
//每一个类型创建一个QCPBarsGroup对象
QCPBarsGroup* barsGroup = nullptr;
if(!m_barsGroups.contains(dataType))
{
barsGroup = new QCPBarsGroup(m_pCustomPlot);
m_barsGroups.insert(dataType, barsGroup);
}
}
else
{
ticks << ++tick;
@ -200,15 +199,17 @@ void dpBarsChart::synchronizeConfigData(const configurationResults& cfg)
}
}
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTicks(ticks, tickLabels);
keyAxis->setTicker(textTicker);
keyAxis->setRange(1, ticks.count());
//Axis
if(groupByType)
keyAxis->setTickLabels(true);
else
keyAxis->setTickLabels(false);
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTicks(ticks, tickLabels);
keyAxis->setTicker(textTicker);
keyAxis->setRange(0, ticks.count() + 1);
valueAxis->setTickLabels(true);
valueAxis->setRange(0, 11);
@ -222,13 +223,22 @@ void dpBarsChart::synchronizeConfigData(const configurationResults& cfg)
double randomVaule = 1 + QRandomGenerator::global()->generateDouble() * (8 - 1);
values << randomVaule;
it.value().qBars->setData(it.value().keys, values);
if(groupByType)
it.value().qBars->setBarsGroup(m_barsGroup);
Bars bar = it.value();
bar.qBars->setData(bar.keys, values);
if(groupByType && m_barsGroups.value(bar.dataType))
{
bar.qBars->setWidthType(QCPBars::wtPlotCoords);
//bar.qBars->setWidth(bar.qBars->width() / groupedBars.value(bar.dataType).count());
bar.qBars->setWidth(bar.qBars->width() * ticks.count() / m_bars.count());
bar.qBars->setBarsGroup(m_barsGroups.value(bar.dataType));
}
}
//Legend
m_pCustomPlot->legend->setVisible(cfg.showLegend);
if(cfg.showLegend && m_bars.count() > 0)
m_pCustomPlot->legend->setVisible(true);
else
m_pCustomPlot->legend->setVisible(false);
m_pCustomPlot->replot();
}

View File

@ -53,9 +53,9 @@ private:
QCustomPlot* m_pCustomPlot;
ChartStyle m_chartStyle;
QCPBarsGroup* m_barsGroup;
//QCPBarsGroup* m_pBarsGroup;
QHash<QString, Bars> m_bars;
//QHash<RealTimeDataType, QCPBarsGroup*> m_barsGroups;
QHash<RealTimeDataType, QCPBarsGroup*> m_barsGroups;
};
#endif

View File

@ -251,6 +251,7 @@ void dpConfigurationDialog::setPanel(DataPanel* pPanel)
ui->serviceIP->setText(pPanel->m_cofigurationResults.dataServiceIP);
ui->servicePort->setText(QString::number(pPanel->m_cofigurationResults.dataServicePort));
ui->radioBtn_showLegend->setChecked(pPanel->m_cofigurationResults.showLegend);
ui->radioBtn_groupByType->setChecked(pPanel->m_cofigurationResults.groupByType);
DataPanelType panelType = pPanel->getType();
switch (panelType)
@ -362,6 +363,7 @@ void dpConfigurationDialog::onBtnClicked_confirm()
m_pDataPanel->m_cofigurationResults.dataServiceIP = ui->serviceIP->text();
m_pDataPanel->m_cofigurationResults.dataServicePort = ui->servicePort->text().toInt();
m_pDataPanel->m_cofigurationResults.showLegend = ui->radioBtn_showLegend->isChecked();
m_pDataPanel->m_cofigurationResults.groupByType = ui->radioBtn_groupByType->isChecked();
m_pDataPanel->m_cofigurationResults.axisArrangement = (AxisArrangementMode)ui->axisArrangement->currentIndex();
DataPanelType panelType = m_pDataPanel->getType();

View File

@ -47,6 +47,7 @@ struct configurationResults
QString dataServiceIP;
int dataServicePort;
bool showLegend;
bool groupByType;
AxisArrangementMode axisArrangement;
BarOrientation barOrientataion;
QHash<RealTimeDataType, AxisCfgInfo> axisCfgMap; //可能有多个轴(数据类别)
@ -60,6 +61,7 @@ struct configurationResults
dataServicePort = 1987;
showLegend = false;
groupByType = false;
axisArrangement = AlternateSides;
barOrientataion = Vertical;
}

View File

@ -337,7 +337,7 @@ QPushButton:pressed
<property name="geometry">
<rect>
<x>25</x>
<y>120</y>
<y>110</y>
<width>521</width>
<height>331</height>
</rect>
@ -624,7 +624,7 @@ background-color: rgb(24, 32, 38);
</rect>
</property>
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="axisCfg">
<property name="styleSheet">
@ -762,7 +762,7 @@ background-color:transparent;
<property name="geometry">
<rect>
<x>80</x>
<y>10</y>
<y>0</y>
<width>91</width>
<height>22</height>
</rect>
@ -782,7 +782,7 @@ background-color:transparent;
<property name="geometry">
<rect>
<x>0</x>
<y>10</y>
<y>0</y>
<width>71</width>
<height>21</height>
</rect>
@ -791,11 +791,11 @@ background-color:transparent;
<string>图形朝向:</string>
</property>
</widget>
<widget class="QRadioButton" name="groupByType">
<widget class="QRadioButton" name="radioBtn_groupByType">
<property name="geometry">
<rect>
<x>0</x>
<y>40</y>
<y>30</y>
<width>161</width>
<height>31</height>
</rect>
@ -811,13 +811,16 @@ background-color:transparent;
<rect>
<x>0</x>
<y>110</y>
<width>121</width>
<width>161</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>显示图例</string>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
</widget>
</widget>
</widget>