diff --git a/dataPanel/dpBaseChart.h b/dataPanel/dpBaseChart.h index a622f31..8b6c9c4 100644 --- a/dataPanel/dpBaseChart.h +++ b/dataPanel/dpBaseChart.h @@ -94,6 +94,14 @@ protected: }*/ } }; + + struct Graph + { + QString dataID; + QColor color; + RealTimeDataType dataType; + QCPGraph* qGraph = nullptr; + }; }; #endif diff --git a/dataPanel/dpLineChart.cpp b/dataPanel/dpLineChart.cpp index db8a3d6..e2c21a7 100644 --- a/dataPanel/dpLineChart.cpp +++ b/dataPanel/dpLineChart.cpp @@ -11,6 +11,7 @@ dpLineChart::dpLineChart(QWidget* parent) m_timeRange = 60 * 1000; m_axisArrangementMode = AlternateSides; + m_updateData = false; QBoxLayout* mainLayout = new QBoxLayout(QBoxLayout::LeftToRight); mainLayout->setContentsMargins(0, 1, 0, 0); @@ -102,11 +103,23 @@ void dpLineChart::arrangeAxes() //更新位置,因为QCPAxis没有更新位置的结构,只能采用从plot中移除再添加的方式 if(axis.qAxis->axisType() != position) { + //因为要进行轴的移除再添加工作,所以其上相关的graph也要同步处理 + QVector affectedGraphs; + for(int j = 0; j < m_pCustomPlot->graphCount(); j++) + { + QCPGraph* graph = m_pCustomPlot->graph(j); + if(graph->valueAxis() == axis.qAxis) + affectedGraphs.append(graph); + } + bool bRemoved = m_pCustomPlot->axisRect()->removeAxis(axis.qAxis); if(bRemoved) //removeAxis执行成功,被删除的axis会被delete { QCPAxis* qcpAxis = m_pCustomPlot->axisRect()->addAxis(position); axis.setQCPAxis(qcpAxis, false); + + for(QCPGraph* graph : affectedGraphs) + graph->setValueAxis(qcpAxis); } } @@ -188,6 +201,9 @@ void dpLineChart::setDateTime(const QDateTime& dateTime) m_pCustomPlot->xAxis->setRange(timeValue, m_timeRange / 1000.0, Qt::AlignRight); m_pCustomPlot->replot(); m_curDateTime = dateTime; + + if(m_updateData) + {} } void dpLineChart::viewHistoricalData(const QDateTime& dateTime) @@ -197,7 +213,9 @@ void dpLineChart::viewHistoricalData(const QDateTime& dateTime) void dpLineChart::synchronizeConfigData(const configurationResults& cfg) { - //Y坐标轴的数量由数据类型决定 + m_updateData = false; //停止更新数据 + + //1.Y坐标轴-数量由数据类型决定 //将最新配置信息中的坐标轴相关数据存储在QHash中,有助于更好的判断当前坐标轴是否需要发生同步更新 QHash axisCfgMap; @@ -244,11 +262,35 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg) Axis& axis = m_axes[i]; if(!axisCfgMap.contains(axis._cfg.dataType)) { + //先删除轴上的Graph(注意先后顺序,QCustomPlot的removeGraph会析构graph,所以先做m_graphs的删除) + // for(auto it = m_graphs.begin(); it != m_graphs.end();) + // { + // if(it.value().qGraph && it.value().qGraph->valueAxis() == axis.qAxis) + // it = m_graphs.erase(it); //使用erase方法可以更新迭代器,从而避免迭代器失效 + // else + // ++it; + // } + QMutableHashIterator it(m_graphs); //使用QMutableHashIterator可以再遍历相关容器的同时安全的修改,例如删除元素 + while (it.hasNext()) + { + it.next(); + if (it.value().qGraph && it.value().qGraph->valueAxis() == axis.qAxis) + it.remove(); // 直接删除当前项,无需手动管理迭代器 + } + for(int j = 0; j < m_pCustomPlot->graphCount(); j++) + { + QCPGraph* graph = m_pCustomPlot->graph(j); + if(graph->valueAxis() == axis.qAxis) + { + m_pCustomPlot->removeGraph(graph); + } + } + //删除轴 m_pCustomPlot->axisRect()->removeAxis(axis.qAxis); m_axes.remove(i); } else - i++; + ++i; #endif } if(m_axes.isEmpty()) //若所有轴(数据类型)全部清楚,将默认左轴xAxis显示出来 @@ -321,6 +363,29 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg) //重新排列坐标轴 m_axisArrangementMode = cfg.arrangement; arrangeAxes(); + + //2.曲线-数据源决定 + QVector deleteGraphs; + 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 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 graphID = stationID + "-" + compoentID + "-" + pointID; + if(!m_graphs.contains(graphID)) + { + RealTimeDataType dataType = (RealTimeDataType)cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_dataType).toInt(); + Graph graph; + graph.dataID = graphID; + graph.dataType = dataType; + } + else + { + + } + } + + m_updateData = true; } void dpLineChart::onSignal_rangeChanged_xAxis(const QCPRange& range) diff --git a/dataPanel/dpLineChart.h b/dataPanel/dpLineChart.h index 9477c58..1a3bc74 100644 --- a/dataPanel/dpLineChart.h +++ b/dataPanel/dpLineChart.h @@ -33,15 +33,25 @@ public slots: void onSignal_rangeChanged_xAxis(const QCPRange&); private: + //处理关联图形的策略 + enum HandleGraphPolicy + { + Remove, //删除 + ReassignToOthrer //重新分配到其它坐标轴 + }; + void initQCP(); void arrangeAxes(); + //void handleAssociatedGraphs(); QCustomPlot* m_pCustomPlot; ChartStyle m_chartStyle; qint64 m_timeRange; QDateTime m_curDateTime; QVector m_axes; + QHash m_graphs; AxisArrangementMode m_axisArrangementMode; + bool m_updateData; }; #endif