Results 1 to 3 of 3

Thread: how to set long label beyond axis widget's extent?

  1. #1
    Join Date
    Feb 2012
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default how to set long label beyond axis widget's extent?

    Hi,

    I need to set rotated long labels on the x axis, but the left end aligns with the y axis. The scales is compressed as below.
    01.png

    Is there any way to make the label extend beyond the end of its axis, like this
    02.png

    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to set long label beyond axis widget's extent?

    What happens if you set:

    Qt Code:
    1. plot->plotLayout()->setAlignCanvasToScale( QwtPlot::yLeft, true );
    To copy to clipboard, switch view to plain text mode 
    Uwe

  3. #3
    Join Date
    Feb 2012
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to set long label beyond axis widget's extent?

    Thanks for your reply. It's not the problem of canvas alignment. After many tries, it already works. But I still don't understand;-)

    I want the labels to rotate to avoid overlap when resizing. The minimum working code is below:
    Qt Code:
    1. #include "qwt_plot.h"
    2.  
    3. class QwtPlotBarChart;
    4. class LabelScaleDraw;
    5.  
    6. class Widget : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit Widget(QWidget *parent = 0);
    12.  
    13. bool eventFilter(QObject *watched, QEvent *ev);
    14.  
    15. private:
    16. void setLabels(qreal step, const QList<QwtText> &labels, LabelScaleDraw &scaleDraw);
    17.  
    18. private slots:
    19. void setData();
    20.  
    21. private:
    22. QwtPlot *_plot;
    23. QwtPlotBarChart *_barChart;
    24. LabelScaleDraw *_scaleDraw;
    25. };
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. #include "widget.h"
    2.  
    3. #include "qwt_plot_barchart.h"
    4. #include "qwt_plot_layout.h"
    5. #include "qwt_scale_widget.h"
    6.  
    7. #include <QTimer>
    8. #include <QResizeEvent>
    9. #include <QVBoxLayout>
    10. #include <QPushButton>
    11. #include <QDebug>
    12.  
    13. class LabelScaleDraw: public QwtScaleDraw
    14. {
    15. public:
    16. LabelScaleDraw(){}
    17. QwtText label(double value) const
    18. {
    19. const int index = qRound(value);
    20. if (index % _sampleStep == 0)
    21. return _labelTexts.value(index);
    22. else
    23. return QwtText();
    24. }
    25. QList<QwtText> labelTexts() const
    26. {
    27. return _labelTexts;
    28. }
    29. void setLabelTexts(const QList<QwtText> &texts, int sampleStep = 1)
    30. {
    31. _labelTexts = texts;
    32. _sampleStep = sampleStep > 1 ? sampleStep : 1;
    33. invalidateCache();
    34. }
    35.  
    36. private:
    37. QList<QwtText> _labelTexts;
    38. int _sampleStep = 1;
    39. };
    40.  
    41. Widget::Widget(QWidget *parent) :
    42. QWidget(parent)
    43. {
    44. _plot = new QwtPlot();
    45. _plot->axisWidget(QwtPlot::xBottom)->installEventFilter(this);
    46. _plot->setAutoReplot(false);
    47. _plot->plotLayout()->setAlignCanvasToScales(true);
    48.  
    49. _scaleDraw = new LabelScaleDraw();
    50. _plot->setAxisScaleDraw(QwtPlot::xBottom, _scaleDraw);
    51.  
    52. _barChart = new QwtPlotBarChart();
    53. _barChart->attach(_plot);
    54.  
    55. QPushButton *button = new QPushButton("Push");
    56.  
    57. QVBoxLayout *vLayout = new QVBoxLayout();
    58. vLayout->addWidget(_plot);
    59. vLayout->addWidget(button);
    60. setLayout(vLayout);
    61.  
    62. QTimer::singleShot(300, this, SLOT(setData()));
    63. // setData();
    64. // connect(button, &QPushButton::clicked, this, &Widget::setData);
    65. }
    66.  
    67. bool Widget::eventFilter(QObject *watched, QEvent *ev)
    68. {
    69. if (watched == _plot->axisWidget(QwtPlot::xBottom) && ev->type() == QEvent::Resize)
    70. {
    71. QwtScaleMap xMap = _plot->canvasMap(QwtPlot::xBottom);
    72. qreal scaleMajorInterval = xMap.transform(1) - xMap.transform(0);
    73. setLabels(scaleMajorInterval, _scaleDraw->labelTexts(), *_scaleDraw);
    74. _plot->replot();
    75. }
    76. return QWidget::eventFilter(watched, ev);
    77. }
    78.  
    79. void Widget::setLabels(qreal step, const QList<QwtText> &labels, LabelScaleDraw &scaleDraw)
    80. {
    81. if (labels.isEmpty())
    82. return;
    83.  
    84. qDebug() << "------------" << step;
    85.  
    86. qreal maxLabelWidth = 0;
    87. for (const QwtText &text : labels)
    88. {
    89. QSizeF textSize = text.textSize();
    90. if (textSize.width() > maxLabelWidth)
    91. maxLabelWidth = textSize.width();
    92. }
    93.  
    94. if (maxLabelWidth > step * 1.5)
    95. {
    96. scaleDraw.setLabelRotation(-90);
    97. scaleDraw.setLabelAlignment(Qt::AlignLeft);
    98. }
    99. else if (maxLabelWidth < step * 1.5 && maxLabelWidth > step * 0.9)
    100. {
    101. scaleDraw.setLabelRotation(-45);
    102. scaleDraw.setLabelAlignment(Qt::AlignLeft);
    103. }
    104. else
    105. {
    106. scaleDraw.setLabelRotation(0);
    107. scaleDraw.setLabelAlignment(Qt::AlignHCenter);
    108. }
    109. scaleDraw.setLabelTexts(labels);
    110. }
    111.  
    112. void Widget::setData()
    113. {
    114. const int numSamples = 10;
    115.  
    116. QVector<double> series;
    117. QList<double> ticks;
    118. QList<QwtText> labels;
    119. for (int i = 0; i < numSamples; ++i)
    120. {
    121. series << 2 + i * 3;
    122. ticks << i;
    123. labels << QwtText("SteelBlue");
    124. }
    125. //setSample
    126. _barChart->setSamples(series);
    127.  
    128. //setScaleDiv
    129. QwtScaleDiv xScaleDiv(- 0.5, numSamples - 0.5);
    130. xScaleDiv.setTicks(QwtScaleDiv::MajorTick, ticks);
    131. _plot->setAxisScaleDiv(QwtPlot::xBottom, xScaleDiv);
    132.  
    133. //setLabels
    134. QwtScaleMap xMap = _plot->canvasMap(QwtPlot::xBottom);
    135. qreal scaleMajorInterval = xMap.transform(1) - xMap.transform(0);
    136. setLabels(scaleMajorInterval, labels, *_scaleDraw);
    137.  
    138. _plot->replot();
    139. }
    To copy to clipboard, switch view to plain text mode 
    What it doesn't work before is I set auto plot to true, without the explicit
    Qt Code:
    1. _plot->replot()
    To copy to clipboard, switch view to plain text mode 
    in the resize eventFilter; But I don't know why it wasn't work. Shouldn't it auto replot itself as the explicit one?
    Thanks.

Similar Threads

  1. Axis Tick and Label !
    By huyhoangfool in forum Qwt
    Replies: 5
    Last Post: 29th July 2014, 12:20
  2. Axis Title to axis label alignment
    By ROCKSTAR in forum Qwt
    Replies: 0
    Last Post: 5th February 2014, 12:47
  3. Stil axis label trouble
    By K4ELO in forum Qwt
    Replies: 1
    Last Post: 18th March 2013, 06:35
  4. QwtPlot x axis label
    By ldg20 in forum Qwt
    Replies: 5
    Last Post: 17th July 2012, 07:02
  5. QCheckBox with long text label
    By Ferdous in forum Newbie
    Replies: 5
    Last Post: 29th October 2011, 19:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.