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

View File

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

View File

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

View File

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

View File

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