@@ -40,7 +40,7 @@ PlotWidget::PlotWidget(QWidget *parent)
4040
4141PlotWidget::~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
167167PlotMarker* 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
327327void 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