feat:增加‘图例’的显示/隐藏配置

This commit is contained in:
duanshengchao 2025-08-07 11:48:08 +08:00
parent 892964c8ce
commit 3cf3c88b1e
10 changed files with 84 additions and 1 deletions

View File

@ -104,12 +104,14 @@ protected:
QString dataID;
QString synchronizeTagging; //同步配置数据时的标记new、update两种没有的就是要被删除
QString name; //用户legend图例展示用
Graph()
{
dataID = "";
//qGraph = nullptr;
synchronizeTagging = "noTagging";
name = "";
}
};
};

View File

@ -64,6 +64,8 @@ void dpConfigurationDialog::initialize()
connect(ui->btnReomve_type, SIGNAL(clicked()), this, SLOT(onBtnClicked_remove_type()));
connect(ui->btnReomve_source, SIGNAL(clicked()), this, SLOT(onBtnClicked_remove_source()));
//connect(ui->radioBtn_showLegend, &QRadioButton::toggled, this, &dpConfigurationDialog::onRadioBtnToggled);
connect(ui->typeSourceList, &QListView::clicked, this, &dpConfigurationDialog::onItemClicked_typeSource);
connect(ui->dataSourceList, &QListView::clicked, this, &dpConfigurationDialog::onItemClicked_dataSource);
@ -246,6 +248,7 @@ void dpConfigurationDialog::setPanel(DataPanel* pPanel)
// ui->typeSelectedList->setModel(m_pModel_typeSelected);
ui->serviceIP->setText(pPanel->m_cofigurationResults.dataServiceIP);
ui->servicePort->setText(QString::number(pPanel->m_cofigurationResults.dataServicePort));
ui->radioBtn_showLegend->setChecked(pPanel->m_cofigurationResults.showLegend);
DataPanelType panelType = pPanel->getType();
switch (panelType)
@ -335,6 +338,7 @@ void dpConfigurationDialog::onBtnClicked_confirm()
copyModelData(m_pModel_dataSelected, m_pDataPanel->m_cofigurationResults.m_pModel_dataSource);
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.arrangement = (AxisArrangementMode)ui->axisArrangement->currentIndex();
m_pDataPanel->m_cofigurationResults.axisCfgMap = m_axisCfgMap;
m_pDataPanel->configurationComplete();
@ -550,3 +554,8 @@ void dpConfigurationDialog::onBtnClicked_remove_source()
removeDataSelected(nCurrentRow);
}
}
void dpConfigurationDialog::onRadioBtnToggled(bool checked)
{
}

View File

@ -33,6 +33,7 @@ public slots:
void onBtnClicked_cancle();
void onBtnClicked_remove_type();
void onBtnClicked_remove_source();
void onRadioBtnToggled(bool checked);
void onItemClicked_typeSource(const QModelIndex&);
void onItemClicked_dataSource(const QModelIndex&);

View File

@ -40,6 +40,7 @@ struct configurationResults
QStandardItemModel* m_pModel_dataSource;
QString dataServiceIP;
int dataServicePort;
bool showLegend;
AxisArrangementMode arrangement;
QHash<RealTimeDataType, AxisCfgInfo> axisCfgMap; //可能有多个轴(数据类别)
@ -51,6 +52,7 @@ struct configurationResults
dataServiceIP = "127.0.0.1";
dataServicePort = 1987;
showLegend = false;
arrangement = AlternateSides;
}

View File

@ -11,6 +11,7 @@ dpLineChart::dpLineChart(QWidget* parent)
m_timeRange = 60 * 1000;
m_axisArrangementMode = AlternateSides;
m_showLegend = false;
m_updateData = false;
QBoxLayout* mainLayout = new QBoxLayout(QBoxLayout::LeftToRight);
@ -80,6 +81,13 @@ void dpLineChart::initQCP()
//qDebug() << timeTicker->dateTimeFormat();
m_pCustomPlot->xAxis->setTicker(timeTicker);
//qDebug() << m_pCustomPlot->xAxis->range();
//Legend
m_pCustomPlot->legend->setBrush(QBrush(QColor(255,255,255,12))); //背景透明
m_pCustomPlot->legend->setBorderPen(QPen(QColor(255,255,255,0))); //边框透明
m_pCustomPlot->legend->setFont(m_chartStyle.labelFont);
m_pCustomPlot->legend->setTextColor( m_chartStyle.labelColor);
m_pCustomPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignRight|Qt::AlignTop);
}
void dpLineChart::arrangeAxes()
@ -131,6 +139,26 @@ void dpLineChart::arrangeAxes()
}
}
void dpLineChart::reLayoutLegend()
{
if(!m_showLegend)
return;
//图例放在最右侧纵坐标轴的右侧
//1.找到最右侧坐标轴
QCPAxis* rightmostAxis = m_pCustomPlot->yAxis2;
int offset = rightmostAxis->offset();
QList<QCPAxis*> axes = m_pCustomPlot->axisRect()->axes(QCPAxis::atRight);
for(QCPAxis* axis : axes)
{
if(axis->offset() > rightmostAxis->offset())
rightmostAxis = axis;
}
//2.设置图例位置
QCPLegend* legend = m_pCustomPlot->legend;
}
void dpLineChart::setTimeRange(TimeUnit unit)
{
switch(unit)
@ -406,9 +434,11 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
graph.dataID = graphID;
graph.dataType = dataType;
graph.synchronizeTagging = "new";
graph.name = cfg.m_pModel_dataSource->item(i, 0)->text();
QCPGraph* newGraph = m_pCustomPlot->addGraph(m_pCustomPlot->xAxis, valueAxis);
if(newGraph)
{
newGraph->setName(graph.name);
if(colorData.isValid())
{
QColor color = colorData.value<QColor>();
@ -449,9 +479,11 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
if( !m_pCustomPlot->removeGraph(g.qGraph) )
qWarning() << "Failed to remove gracloxne ph:" << key;
}
//qDebug() << "graph count: " << m_graphs.count() << ", " << m_pCustomPlot->graphCount();
//Legend
m_pCustomPlot->legend->setVisible(cfg.showLegend);
m_updateData = true;
}

View File

@ -42,6 +42,7 @@ private:
void initQCP();
void arrangeAxes();
void reLayoutLegend();
//void handleAssociatedGraphs();
QCustomPlot* m_pCustomPlot;
@ -51,6 +52,7 @@ private:
QVector<Axis> m_axes;
QHash<QString, Graph> m_graphs;
AxisArrangementMode m_axisArrangementMode;
bool m_showLegend;
bool m_updateData;
};

View File

@ -1,5 +1,7 @@
<RCC>
<qresource prefix="/">
<file>images/ico_switch_off.png</file>
<file>images/ico_switch_on.png</file>
<file>images/down-arrow.png</file>
<file>images/branch_closed.png</file>
<file>images/branch_open.png</file>

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -44,6 +44,26 @@ font: 12pt &quot;黑体&quot;;
color: rgb(250, 250, 250);
}
QRadioButton
{
spacing:8px;
font: 12pt &quot;黑体&quot;;
color: rgb(250, 250, 250);
}
QRadioButton::indicator
{
width:36px;
height:20px;
}
QRadioButton::indicator:unchecked
{
image: url(:/images/ico_switch_off.png);
}
QRadioButton::indicator:checked
{
image: url(:/images/ico_switch_on.png);
}
QLineEdit
{
font: 12pt &quot;黑体&quot;;
@ -730,6 +750,19 @@ background-color:transparent;
<string>Y轴排列</string>
</property>
</widget>
<widget class="QRadioButton" name="radioBtn_showLegend">
<property name="geometry">
<rect>
<x>0</x>
<y>100</y>
<width>121</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>显示图例</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="page_2"/>
</widget>