feat:新增曲线的实时模拟数据展示

This commit is contained in:
duanshengchao 2025-08-06 10:23:33 +08:00
parent 338baafe74
commit 892964c8ce
2 changed files with 35 additions and 13 deletions

View File

@ -207,7 +207,7 @@ void dpConfigurationDialog::createDataSourceList()
tempItem->setData("point", Qt::UserRole + itemRole_tag); tempItem->setData("point", Qt::UserRole + itemRole_tag);
tempItem->setData(i, Qt::UserRole + itemRole_stationID); tempItem->setData(i, Qt::UserRole + itemRole_stationID);
tempItem->setData(0, Qt::UserRole + itemRole_componentID); tempItem->setData(0, Qt::UserRole + itemRole_componentID);
tempItem->setData(2, Qt::UserRole + itemRole_pointID); tempItem->setData(3, Qt::UserRole + itemRole_pointID);
tempItem->setData(RealTimeDataType::temperature, Qt::UserRole + itemRole_dataType); tempItem->setData(RealTimeDataType::temperature, Qt::UserRole + itemRole_dataType);
componentItem->appendRow(tempItem); componentItem->appendRow(tempItem);
} }

View File

@ -203,7 +203,15 @@ void dpLineChart::setDateTime(const QDateTime& dateTime)
m_curDateTime = dateTime; m_curDateTime = dateTime;
if(m_updateData) if(m_updateData)
{} {
//模拟数据展示
double min = 0, max = 5.0;
for(auto it = m_graphs.begin(); it != m_graphs.end(); ++it)
{
double randomFloat = min + QRandomGenerator::global()->generateDouble() * (max - min);
it.value().qGraph->addData(timeValue, randomFloat);
}
}
} }
void dpLineChart::viewHistoricalData(const QDateTime& dateTime) void dpLineChart::viewHistoricalData(const QDateTime& dateTime)
@ -263,23 +271,26 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
// else // else
// ++it; // ++it;
// } // }
QMutableHashIterator<QString, Graph> it(m_graphs); //使用QMutableHashIterator可以再遍历相关容器的同时安全的修改例如删除元素
while (it.hasNext())
{
it.next();
if (it.value().qGraph.isNull() || it.value().qGraph->valueAxis() == axis.qAxis)
it.remove(); // 直接删除当前项,无需手动管理迭代器
}
for(int j = 0; j < m_pCustomPlot->graphCount();) for(int j = 0; j < m_pCustomPlot->graphCount();)
{ {
QCPGraph* graph = m_pCustomPlot->graph(j); QCPGraph* graph = m_pCustomPlot->graph(j);
if(graph->valueAxis() == axis.qAxis) if(graph->valueAxis() == axis.qAxis)
{ {
m_pCustomPlot->removeGraph(graph); m_pCustomPlot->removeGraph(graph); //removeGraph时graph会被delete
} }
else else
++j; ++j;
} }
QMutableHashIterator<QString, Graph> it(m_graphs); //使用QMutableHashIterator可以再遍历相关容器的同时安全的修改例如删除元素
while (it.hasNext())
{
it.next();
if (it.value().qGraph.isNull()/*it.value().qGraph->valueAxis() == axis.qAxis*/) //利用QPointer特性保持数据同步
{
//m_pCustomPlot->removeGraph(it.value().qGraph);
it.remove(); // 直接删除当前项,无需手动管理迭代器
}
}
//删除轴 //删除轴
m_pCustomPlot->axisRect()->removeAxis(axis.qAxis); m_pCustomPlot->axisRect()->removeAxis(axis.qAxis);
m_axes.remove(i); m_axes.remove(i);
@ -373,13 +384,17 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
arrangeAxes(); arrangeAxes();
//2.曲线-数据源决定 //2.曲线-数据源决定
//qDebug() << "m_graphs before update: " << m_graphs.keys();
for(int i = 0; i < cfg.m_pModel_dataSource->rowCount(); i++) for(int i = 0; i < cfg.m_pModel_dataSource->rowCount(); i++)
{ {
QString stationID = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_stationID).toString(); QString stationID = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_stationID).toString();
QString compoentID = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_componentID).toString(); QString compoentID = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_componentID).toString();
QString pointID = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_pointID).toString(); QString pointID = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_pointID).toString();
QString graphID = stationID + "-" + compoentID + "-" + pointID; QString graphID = stationID + "-" + compoentID + "-" + pointID;
//qDebug() << "update:" << graphID;
QVariant colorData = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::DecorationRole);
if(!m_graphs.contains(graphID)) //新增数据 if(!m_graphs.contains(graphID)) //新增数据
{ {
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();
@ -394,7 +409,6 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
QCPGraph* newGraph = m_pCustomPlot->addGraph(m_pCustomPlot->xAxis, valueAxis); QCPGraph* newGraph = m_pCustomPlot->addGraph(m_pCustomPlot->xAxis, valueAxis);
if(newGraph) if(newGraph)
{ {
QVariant colorData = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::DecorationRole);
if(colorData.isValid()) if(colorData.isValid())
{ {
QColor color = colorData.value<QColor>(); QColor color = colorData.value<QColor>();
@ -409,9 +423,17 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
//m_graphs.value(graphID).synchronizeTagging = "update"; //m_graphs.value(graphID).synchronizeTagging = "update";
auto it = m_graphs.find(graphID); auto it = m_graphs.find(graphID);
if(it != m_graphs.end()) if(it != m_graphs.end())
{
if(colorData.isValid())
{
QColor color = colorData.value<QColor>();
it.value().qGraph->setPen(QPen(color));
}
it.value().synchronizeTagging = "update"; it.value().synchronizeTagging = "update";
}
} }
} }
//qDebug() << "m_graphs after update: " << m_graphs.keys();
//没有标记的就是需要删除的 //没有标记的就是需要删除的
QStringList keysToRemove; QStringList keysToRemove;
for(auto it = m_graphs.begin(); it != m_graphs.end(); ++it) for(auto it = m_graphs.begin(); it != m_graphs.end(); ++it)
@ -428,7 +450,7 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
qWarning() << "Failed to remove gracloxne ph:" << key; qWarning() << "Failed to remove gracloxne ph:" << key;
} }
qDebug() << "graph count: " << m_graphs.count() << ", " << m_pCustomPlot->graphCount(); //qDebug() << "graph count: " << m_graphs.count() << ", " << m_pCustomPlot->graphCount();
m_updateData = true; m_updateData = true;
} }