Skip to content

Commit 6bdeb47

Browse files
committed
Added bar graph mode to plotwidget class and refactored the class naming for better clarity around it's functionality
1 parent ee53939 commit 6bdeb47

10 files changed

+128
-97
lines changed

tools/ld-analyse/blacksnranalysisdialog.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ BlackSnrAnalysisDialog::BlackSnrAnalysisDialog(QWidget *parent) :
2727
plot->updateTheme();
2828
ui->verticalLayout->addWidget(plot);
2929

30-
// Set up curves and marker
31-
blackCurve = plot->addCurve("Black SNR");
30+
// Set up series and marker
31+
blackSeries = plot->addSeries("Black SNR");
3232
// Theme-aware color: white in dark mode, black in light mode
3333
QColor dataColor = PlotWidget::isDarkTheme() ? Qt::white : Qt::black;
34-
blackCurve->setPen(QPen(dataColor, 2));
34+
blackSeries->setPen(QPen(dataColor, 2));
3535

36-
trendCurve = plot->addCurve("Trend line");
37-
trendCurve->setPen(QPen(Qt::red, 2));
36+
trendSeries = plot->addSeries("Trend line");
37+
trendSeries->setPen(QPen(Qt::red, 2));
3838

3939
plotMarker = plot->addMarker();
4040
plotMarker->setStyle(PlotMarker::VLine);
@@ -106,12 +106,12 @@ void BlackSnrAnalysisDialog::finishUpdate(qint32 _currentFrameNumber)
106106
plot->setAxisRange(Qt::Horizontal, 0, numberOfFrames);
107107
plot->setAxisRange(Qt::Vertical, 20, maxY);
108108

109-
// Set the black curve data
110-
blackCurve->setData(blackPoints);
109+
// Set the black series data
110+
blackSeries->setData(blackPoints);
111111

112112
// Generate and set the trend line
113113
generateTrendLine();
114-
trendCurve->setData(trendPoints);
114+
trendSeries->setData(trendPoints);
115115

116116
// Set the frame marker position
117117
plotMarker->setPosition(QPointF(static_cast<double>(_currentFrameNumber), (maxY + 20) / 2));

tools/ld-analyse/blacksnranalysisdialog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ private slots:
4343

4444
Ui::BlackSnrAnalysisDialog *ui;
4545
PlotWidget *plot;
46-
PlotCurve *blackCurve;
47-
PlotCurve *trendCurve;
46+
PlotSeries *blackSeries;
47+
PlotSeries *trendSeries;
4848
PlotMarker *plotMarker;
4949

5050
double maxY;

tools/ld-analyse/dropoutanalysisdialog.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ DropoutAnalysisDialog::DropoutAnalysisDialog(QWidget *parent) :
2727
plot->updateTheme();
2828
ui->verticalLayout->addWidget(plot);
2929

30-
// Set up curve and marker
31-
curve = plot->addCurve("Dropout Length");
32-
curve->setPen(QPen(Qt::red, 1));
30+
// Set up series and marker
31+
series = plot->addSeries("Dropout Length");
32+
series->setPen(QPen(Qt::red, 1));
33+
series->setStyle(PlotSeries::Bars);
3334

3435
plotMarker = plot->addMarker();
3536
plotMarker->setStyle(PlotMarker::VLine);
@@ -98,8 +99,8 @@ void DropoutAnalysisDialog::finishUpdate(qint32 _currentFrameNumber)
9899

99100
// Set the dropout curve data with theme-aware color
100101
QColor dataColor = PlotWidget::isDarkTheme() ? Qt::yellow : Qt::darkMagenta;
101-
curve->setPen(QPen(dataColor, 2));
102-
curve->setData(points);
102+
series->setPen(QPen(dataColor, 2));
103+
series->setData(points);
103104

104105
// Set the frame marker position
105106
plotMarker->setPosition(QPointF(static_cast<double>(_currentFrameNumber), yMax / 2));

tools/ld-analyse/dropoutanalysisdialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private slots:
4040

4141
Ui::DropoutAnalysisDialog *ui;
4242
PlotWidget *plot;
43-
PlotCurve *curve;
43+
PlotSeries *series;
4444
PlotMarker *plotMarker;
4545

4646
double maxY;

tools/ld-analyse/plotwidget.cpp

Lines changed: 72 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ PlotWidget::PlotWidget(QWidget *parent)
4040

4141
PlotWidget::~PlotWidget()
4242
{
43-
clearCurves();
43+
clearSeries();
4444
clearMarkers();
4545
}
4646

@@ -137,31 +137,31 @@ void PlotWidget::setGridPen(const QPen &pen)
137137
}
138138
}
139139

140-
PlotCurve* PlotWidget::addCurve(const QString &title)
140+
PlotSeries* PlotWidget::addSeries(const QString &title)
141141
{
142-
PlotCurve *curve = new PlotCurve(this);
143-
curve->setTitle(title);
144-
m_curves.append(curve);
145-
m_scene->addItem(curve);
146-
return curve;
142+
PlotSeries *series = new PlotSeries(this);
143+
series->setTitle(title);
144+
m_series.append(series);
145+
m_scene->addItem(series);
146+
return series;
147147
}
148148

149-
void PlotWidget::removeCurve(PlotCurve *curve)
149+
void PlotWidget::removeSeries(PlotSeries *series)
150150
{
151-
if (curve && m_curves.contains(curve)) {
152-
m_curves.removeAll(curve);
153-
m_scene->removeItem(curve);
154-
delete curve;
151+
if (series && m_series.contains(series)) {
152+
m_series.removeAll(series);
153+
m_scene->removeItem(series);
154+
delete series;
155155
}
156156
}
157157

158-
void PlotWidget::clearCurves()
158+
void PlotWidget::clearSeries()
159159
{
160-
for (PlotCurve *curve : m_curves) {
161-
m_scene->removeItem(curve);
162-
delete curve;
160+
for (PlotSeries *series : m_series) {
161+
m_scene->removeItem(series);
162+
delete series;
163163
}
164-
m_curves.clear();
164+
m_series.clear();
165165
}
166166

167167
PlotMarker* PlotWidget::addMarker()
@@ -266,9 +266,9 @@ void PlotWidget::replot()
266266
QRectF sceneRect = QRectF(0, 0, m_view->width(), m_view->height());
267267
m_scene->setSceneRect(sceneRect);
268268

269-
// Update all curves
270-
for (PlotCurve *curve : m_curves) {
271-
curve->updatePath(m_plotRect, m_dataRect);
269+
// Update all series
270+
for (PlotSeries *series : m_series) {
271+
series->updatePath(m_plotRect, m_dataRect);
272272
}
273273

274274
// Update grid
@@ -283,7 +283,7 @@ void PlotWidget::replot()
283283

284284
// Update legend
285285
if (m_legend) {
286-
m_legend->updateLegend(m_curves, m_plotRect);
286+
m_legend->updateLegend(m_series, m_plotRect);
287287
}
288288

289289
// Update axis labels
@@ -326,13 +326,13 @@ void PlotWidget::updatePlotArea()
326326

327327
void PlotWidget::calculateDataRange()
328328
{
329-
if (m_curves.isEmpty()) return;
329+
if (m_series.isEmpty()) return;
330330

331331
bool firstPoint = true;
332332
double xMin = 0, xMax = 0, yMin = 0, yMax = 0;
333333

334-
for (PlotCurve *curve : m_curves) {
335-
const QVector<QPointF> &data = curve->data();
334+
for (PlotSeries *series : m_series) {
335+
const QVector<QPointF> &data = series->data();
336336
for (const QPointF &point : data) {
337337
if (firstPoint) {
338338
xMin = xMax = point.x();
@@ -373,35 +373,41 @@ QPointF PlotWidget::mapFromData(const QPointF &dataPos) const
373373
return QPointF(x, y);
374374
}
375375

376-
// PlotCurve implementation
377-
PlotCurve::PlotCurve(PlotWidget *parent)
376+
// PlotSeries implementation
377+
PlotSeries::PlotSeries(PlotWidget *parent)
378378
: QGraphicsPathItem()
379379
, m_plotWidget(parent)
380+
, m_style(Lines)
380381
{
381382
setPen(QPen(Qt::blue, 1.0));
382383
}
383384

384-
void PlotCurve::setTitle(const QString &title)
385+
void PlotSeries::setTitle(const QString &title)
385386
{
386387
m_title = title;
387388
}
388389

389-
void PlotCurve::setPen(const QPen &pen)
390+
void PlotSeries::setPen(const QPen &pen)
390391
{
391392
QGraphicsPathItem::setPen(pen);
392393
}
393394

394-
void PlotCurve::setBrush(const QBrush &brush)
395+
void PlotSeries::setBrush(const QBrush &brush)
395396
{
396397
QGraphicsPathItem::setBrush(brush);
397398
}
398399

399-
void PlotCurve::setData(const QVector<QPointF> &data)
400+
void PlotSeries::setStyle(PlotStyle style)
401+
{
402+
m_style = style;
403+
}
404+
405+
void PlotSeries::setData(const QVector<QPointF> &data)
400406
{
401407
m_data = data;
402408
}
403409

404-
void PlotCurve::setData(const QVector<double> &xData, const QVector<double> &yData)
410+
void PlotSeries::setData(const QVector<double> &xData, const QVector<double> &yData)
405411
{
406412
m_data.clear();
407413
int count = qMin(xData.size(), yData.size());
@@ -410,27 +416,41 @@ void PlotCurve::setData(const QVector<double> &xData, const QVector<double> &yDa
410416
}
411417
}
412418

413-
void PlotCurve::setVisible(bool visible)
419+
void PlotSeries::setVisible(bool visible)
414420
{
415421
QGraphicsPathItem::setVisible(visible);
416422
}
417423

418-
void PlotCurve::updatePath(const QRectF &plotRect, const QRectF &dataRect)
424+
void PlotSeries::updatePath(const QRectF &plotRect, const QRectF &dataRect)
419425
{
420426
if (m_data.isEmpty() || !m_plotWidget) return;
421427

422428
QPainterPath path;
423-
bool firstPoint = true;
424429

425-
for (const QPointF &dataPoint : m_data) {
426-
QPointF scenePoint = m_plotWidget->mapFromData(dataPoint);
427-
428-
if (firstPoint) {
429-
path.moveTo(scenePoint);
430-
firstPoint = false;
431-
} else {
430+
if (m_style == Bars) {
431+
// Draw vertical bars from x-axis (y=0) to each data point
432+
for (const QPointF &dataPoint : m_data) {
433+
QPointF scenePoint = m_plotWidget->mapFromData(dataPoint);
434+
QPointF basePoint = m_plotWidget->mapFromData(QPointF(dataPoint.x(), 0.0));
435+
436+
// Draw vertical line from base (y=0) to the data point
437+
path.moveTo(basePoint);
432438
path.lineTo(scenePoint);
433439
}
440+
} else {
441+
// Default Lines style: connect points with continuous line
442+
bool firstPoint = true;
443+
444+
for (const QPointF &dataPoint : m_data) {
445+
QPointF scenePoint = m_plotWidget->mapFromData(dataPoint);
446+
447+
if (firstPoint) {
448+
path.moveTo(scenePoint);
449+
firstPoint = false;
450+
} else {
451+
path.lineTo(scenePoint);
452+
}
453+
}
434454
}
435455

436456
setPath(path);
@@ -594,11 +614,11 @@ void PlotLegend::setEnabled(bool enabled)
594614
setVisible(enabled);
595615
}
596616

597-
void PlotLegend::updateLegend(const QList<PlotCurve*> &curves, const QRectF &plotRect)
617+
void PlotLegend::updateLegend(const QList<PlotSeries*> &series, const QRectF &plotRect)
598618
{
599-
m_curves = curves;
619+
m_series = series;
600620

601-
if (!m_enabled || curves.isEmpty()) {
621+
if (!m_enabled || series.isEmpty()) {
602622
m_boundingRect = QRectF();
603623
return;
604624
}
@@ -610,9 +630,9 @@ void PlotLegend::updateLegend(const QList<PlotCurve*> &curves, const QRectF &plo
610630
int maxWidth = 0;
611631
int totalHeight = 0;
612632

613-
for (PlotCurve *curve : curves) {
614-
if (!curve->title().isEmpty()) {
615-
int width = fm.horizontalAdvance(curve->title()) + 30; // 30 for line sample
633+
for (PlotSeries *s : series) {
634+
if (!s->title().isEmpty()) {
635+
int width = fm.horizontalAdvance(s->title()) + 30; // 30 for line sample
616636
maxWidth = qMax(maxWidth, width);
617637
totalHeight += fm.height() + 2;
618638
}
@@ -636,7 +656,7 @@ void PlotLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
636656
Q_UNUSED(option)
637657
Q_UNUSED(widget)
638658

639-
if (!m_enabled || m_curves.isEmpty()) return;
659+
if (!m_enabled || m_series.isEmpty()) return;
640660

641661
// Draw legend background
642662
painter->fillRect(m_boundingRect, QColor(255, 255, 255, 200));
@@ -649,16 +669,16 @@ void PlotLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
649669

650670
int y = m_boundingRect.top() + 5;
651671

652-
for (PlotCurve *curve : m_curves) {
653-
if (!curve->title().isEmpty()) {
672+
for (PlotSeries *series : m_series) {
673+
if (!series->title().isEmpty()) {
654674
// Draw line sample
655-
painter->setPen(curve->pen());
675+
painter->setPen(series->pen());
656676
painter->drawLine(QPointF(m_boundingRect.left() + 5, y + fm.height()/2),
657677
QPointF(m_boundingRect.left() + 25, y + fm.height()/2));
658678

659679
// Draw text
660680
painter->setPen(QPen(Qt::black));
661-
painter->drawText(QPointF(m_boundingRect.left() + 30, y + fm.ascent()), curve->title());
681+
painter->drawText(QPointF(m_boundingRect.left() + 30, y + fm.ascent()), series->title());
662682

663683
y += fm.height() + 2;
664684
}

0 commit comments

Comments
 (0)