Results 1 to 4 of 4

Thread: Qwt x-axis is missing in the chart

  1. #1
    Join Date
    Apr 2021
    Posts
    7
    Thanks
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Qwt x-axis is missing in the chart

    Hi

    I had a problem with my graph that the major x-axis is not drawn in the graph but the y-axis line is drawn (see attached image).

    missingxaxis.jpg

    I updated my Qwt version 5.2.3 to 6.1.6. At first the Plot Grid is missing also with the x and y -axis which was already solved in my previous post that the draw method must be overriden.

    plotview.jpg

    Commenting out the line of code to attach the PlotGrid removes the plot grid as well as the main y-axis.

    m_plotGrid = new PlotGrid;
    //m_plotGrid->attach(m_plot);

    Below is the source codes that initializes the Graph elements (removed other non-related methods/implementations)

    Qt Code:
    1. //PlotPanel.cpp
    2.  
    3. PlotPanel::PlotPanel(PlotStyle plotStyle, RulerType rulerType, QWidget *parent) :
    4. QWidget(parent),
    5. m_plot(0),
    6. m_plotBackground(0),
    7. m_plotGrid(0),
    8. m_plotRulerPanel(0)
    9. {
    10. setFixedSize(StyleInfo::PLOT_PANEL_WIDTH, StyleInfo::PLOT_PANEL_HEIGHT);
    11.  
    12. m_plot = new Plot(plotStyle, this);
    13. m_plot->move(PLOT_HORIZONTAL_BORDER, PLOT_TOP_BORDER);
    14.  
    15. m_plotRulerPanel = new PlotRulerPanel(rulerType, this);
    16. m_plotRulerPanel->move(PLOT_HORIZONTAL_BORDER, 0);
    17. m_plotRulerPanel->setPlot(m_plot);
    18.  
    19. m_plotBackground = new PlotBackground;
    20. m_plotBackground->attach(m_plot);
    21.  
    22. m_plotGrid = new PlotGrid;
    23. m_plotGrid->attach(m_plot);
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 
    The PlotPanel class is the one that initializes other plot classes that is used by the class that displays the graph.

    Qt Code:
    1. //Plot.cpp
    2.  
    3. Plot::Plot(PlotStyle plotStyle, QWidget *parent) :
    4. QwtPlot(parent),
    5. m_xBottomScaleDraw(0),
    6. m_xTopScaleDraw(0),
    7. m_yLeftScaleDraw(0),
    8. m_yRightScaleDraw(0)
    9. {
    10. setFixedSize(StyleInfo::PLOT_WIDTH, StyleInfo::PLOT_HEIGHT);
    11.  
    12. ((QwtPlotCanvas*)canvas())->setFrameStyle(QFrame::Box | QFrame::Plain);
    13. ((QwtPlotCanvas*)canvas())->setLineWidth(0);
    14. ((QwtPlotCanvas*)canvas())->unsetCursor();
    15.  
    16. plotLayout()->setAlignCanvasToScales(true);
    17. plotLayout()->setSpacing(0);
    18.  
    19. for (int i=0; i<QwtPlot::axisCnt; i++)
    20. {
    21. QwtScaleWidget *scaleWidget = axisWidget(i);
    22. if (scaleWidget)
    23. {
    24. scaleWidget->setSpacing(0);
    25. scaleWidget->setMargin(0);
    26. scaleWidget->setMinBorderDist(0, 0);
    27.  
    28. QColor color;
    29. if (i == QwtPlot::yLeft)
    30. {
    31. color = StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_ONE);
    32. }
    33. else if (i == QwtPlot::yRight)
    34. {
    35. color = StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_TWO);
    36. }
    37. else
    38. {
    39. color = StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_THREE);
    40. }
    41.  
    42. QPalette palette = scaleWidget->palette();
    43. palette.setColor(QPalette::Text, color);
    44. palette.setColor(QPalette::WindowText, color);
    45. scaleWidget->setPalette(palette);
    46. }
    47. QFont font = StyleInfo::font(StyleInfo::FONT_SIZE_SMALL);
    48. setAxisFont(i, font);
    49. }
    50.  
    51. if (plotStyle == TIME_PLOT)
    52. {
    53. m_xBottomScaleDraw = new PlotTimeScaleDraw; // will be deleted by QwtPlot
    54. }
    55. else
    56. {
    57. m_xBottomScaleDraw = new PlotScaleDraw; // will be deleted by QwtPlot
    58. }
    59. m_xBottomScaleDraw->setSpacing(X_SPACING);
    60. m_xBottomScaleDraw->setMinimumExtent(X_BOTTOM_EXTENT);
    61. setAxisScaleDraw(QwtPlot::xBottom, m_xBottomScaleDraw);
    62.  
    63. m_xTopScaleDraw = new PlotScaleDraw; // will be deleted by QwtPlot
    64. m_xTopScaleDraw->setSpacing(X_SPACING);
    65. m_xTopScaleDraw->setMinimumExtent(X_TOP_EXTENT);
    66. setAxisScaleDraw(QwtPlot::xTop, m_xTopScaleDraw);
    67.  
    68. m_yLeftScaleDraw = new PlotScaleDraw; // will be deleted by QwtPlot
    69. m_yLeftScaleDraw->setSpacing(Y_SPACING);
    70. m_yLeftScaleDraw->setMinimumExtent(Y_LEFT_EXTENT);
    71. setAxisScaleDraw(QwtPlot::yLeft, m_yLeftScaleDraw);
    72.  
    73. m_yRightScaleDraw = new PlotScaleDraw; // will be deleted by QwtPlot
    74. m_yRightScaleDraw->setSpacing(Y_SPACING);
    75. m_yRightScaleDraw->setMinimumExtent(Y_RIGHT_EXTENT);
    76. setAxisScaleDraw(QwtPlot::yRight, m_yRightScaleDraw);
    77.  
    78. for (int i=0; i<QwtPlot::axisCnt; i++)
    79. {
    80. axisScaleDraw(i)->setTickLength(QwtScaleDiv::MinorTick, TICK_LENGTH);
    81. axisScaleDraw(i)->setTickLength(QwtScaleDiv::MediumTick, TICK_LENGTH);
    82. axisScaleDraw(i)->setTickLength(QwtScaleDiv::MajorTick, TICK_LENGTH);
    83.  
    84. enableAxis(i);
    85. }
    86.  
    87. setAxisMaxMajor(QwtPlot::xBottom, MAX_MAJOR_TICKS);
    88. setAxisMaxMinor(QwtPlot::xBottom, MAX_MINOR_TICKS);
    89.  
    90. setAxisMaxMajor(QwtPlot::xTop, MAX_MAJOR_TICKS);
    91. setAxisMaxMinor(QwtPlot::xTop, MAX_MINOR_TICKS);
    92.  
    93. setAxisMaxMajor(QwtPlot::yLeft, MAX_MAJOR_TICKS);
    94. setAxisMaxMinor(QwtPlot::yLeft, MAX_MINOR_TICKS);
    95.  
    96. setAxisMaxMajor(QwtPlot::yRight, MAX_MAJOR_TICKS);
    97. setAxisMaxMinor(QwtPlot::yRight, MAX_MINOR_TICKS);
    98.  
    99. showAxis(QwtPlot::xBottom, true);
    100. showAxis(QwtPlot::xTop, false);
    101. showAxis(QwtPlot::yLeft, true);
    102. showAxis(QwtPlot::yRight, false);
    103. }
    104.  
    105. Plot::~Plot()
    106. {
    107. }
    108.  
    109. void Plot::showAxis(Axis axis, bool show)
    110. {
    111. QwtScaleDraw *scaleDraw = axisScaleDraw(axis);
    112. if (scaleDraw)
    113. {
    114. scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
    115. scaleDraw->enableComponent(QwtScaleDraw::Labels, show);
    116. scaleDraw->enableComponent(QwtScaleDraw::Ticks, show);
    117. }
    118. updateAxes();
    119. }
    120.  
    121. int Plot::scaleWidth(Axis axis) const
    122. {
    123. if ((axis >= 0) && (axis < QwtPlot::axisCnt))
    124. {
    125. const QwtScaleWidget *scaleWidget = axisWidget(axis);
    126. if (scaleWidget)
    127. {
    128. return scaleWidget->width();
    129. }
    130. }
    131. return 0;
    132. }
    133.  
    134. int Plot::scaleHeight(Axis axis) const
    135. {
    136. if ((axis >= 0) && (axis < QwtPlot::axisCnt))
    137. {
    138. const QwtScaleWidget *scaleWidget = axisWidget(axis);
    139. if (scaleWidget)
    140. {
    141. return scaleWidget->height();
    142. }
    143. }
    144. return 0;
    145. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. PlotGrid::PlotGrid() :
    2. {
    3. }
    4.  
    5. void PlotGrid::draw(QPainter *painter, const QwtScaleMap &xMap,
    6. const QwtScaleMap &yMap, const QRectF &rect) const
    7. {
    8. Q_ASSERT(painter);
    9.  
    10. painter->save();
    11.  
    12. painter->setOpacity(0.2);
    13.  
    14. painter->setPen(QPen(StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_THREE)));
    15. drawLines(painter, rect, Qt::Vertical, xMap,
    16. m_xScaleDiv.ticks(QwtScaleDiv::MajorTick));
    17.  
    18. painter->setPen(QPen(StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_ONE)));
    19. drawLines(painter, rect, Qt::Horizontal, yMap,
    20. m_yScaleDiv.ticks(QwtScaleDiv::MajorTick));
    21.  
    22. painter->setOpacity(1.0);
    23.  
    24. // paint scale backbones
    25.  
    26. painter->setPen(StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_ONE));
    27. painter->drawLine((int)rect.left(), (int)rect.top(), (int)rect.left(), (int)rect.bottom());
    28.  
    29. painter->setPen(StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_THREE));
    30. painter->drawLine((int)rect.left(), (int)rect.bottom(), (int)rect.right(), (int)rect.bottom());
    31.  
    32. painter->restore();
    33. }
    34.  
    35. void PlotGrid::updateScaleDiv(const QwtScaleDiv &xScaleDiv, const QwtScaleDiv &yScaleDiv)
    36. {
    37. m_xScaleDiv = xScaleDiv;
    38. m_yScaleDiv = yScaleDiv;
    39. }
    40.  
    41. void PlotGrid::update(void)
    42. {
    43. // todo: is there a better way to force an update??
    44. QwtPlot *qwtPlot = plot();
    45. if (qwtPlot)
    46. {
    47. QwtPlotCanvas *plotCanvas = (QwtPlotCanvas*)qwtPlot->canvas();
    48. if (plotCanvas)
    49. {
    50. plotCanvas->invalidateBackingStore();
    51. plotCanvas->update();
    52. }
    53. }
    54. }
    55.  
    56. void PlotGrid::drawLines(QPainter *painter, const QRectF &rect,
    57. Qt::Orientation orientation, const QwtScaleMap &scaleMap,
    58. const QList<double> &values) const
    59. {
    60. Q_ASSERT(painter);
    61.  
    62. const int x1 = (int)rect.left();
    63. const int x2 = (int)rect.right();
    64. const int y1 = (int)rect.top();
    65. const int y2 = (int)rect.bottom();
    66.  
    67. for (uint i = 0; i < (uint)values.count(); i++)
    68. {
    69. const int value = qRound(scaleMap.transform(values[i]));
    70. if (orientation == Qt::Horizontal)
    71. {
    72. if ((value >= y1) && (value <= y2))
    73. {
    74. painter->drawLine(x1, value, x2, value);
    75. }
    76. }
    77. else
    78. {
    79. if ((value >= x1) && (value < x2)) // suppress last line on the right
    80. {
    81. painter->drawLine(value, y1, value, y2);
    82. }
    83. }
    84. }
    85. }
    To copy to clipboard, switch view to plain text mode 
    The PlotGrid class displays the Plot grids as well as the x and y-axis. As you can see with qwt 6.1.6, I need to cast the qwtscalemap transform method to integer using qRound as a fix and also the QRectF value to int but I am not sure if there is a problem why the major x-axis is missing with these conversions but the y-axis is drawn okay.

    Please let me know if the code I added is enough to determine why the x-axis is missing.

    Regards,

    olredi

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

    Default Re: Qwt x-axis is missing in the chart

    You would need to provide a minimal example, that can be compiled and executed.

    Uwe

  3. #3
    Join Date
    Apr 2021
    Posts
    7
    Thanks
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Qwt x-axis is missing in the chart

    I already found the issue why the x-axis is missing in the chart. Actually it was drawn but outside of the bounding Rect that is why it is hidden but not missing. The rect.bottom() value in my case is 199 and manually setting it to 198 for testing purposes will show the x-axis.

    Line 30-31 of PlotGrid class
    painter->setPen(StyleInfo::skinColor(StyleInfo::SKIN_COLOR _PLOT_THREE));
    painter->drawLine((int)rect.left(), (int)rect.bottom(), (int)rect.right(), (int)rect.bottom());
    I am still checking the root cause but I think it is code related implementation already (margins/font size adjustments).
    Enabling the backbone will show all the rect lines but I only need the x and y axis with
    Qt Code:
    1. scaleDraw->enableComponent(QwtScaleDraw::Backbone, true);
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    olredi

  4. #4
    Join Date
    Apr 2021
    Posts
    7
    Thanks
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Qwt x-axis is missing in the chart

    Quote Originally Posted by olredi View Post
    I already found the issue why the x-axis is missing in the chart. Actually it was drawn but outside of the bounding Rect that is why it is hidden but not missing. The rect.bottom() value in my case is 199 and manually setting it to 198 for testing purposes will show the x-axis.

    Line 30-31 of PlotGrid class


    I am still checking the root cause but I think it is code related implementation already (margins/font size adjustments).
    Enabling the backbone will show all the rect lines but I only need the x and y axis with
    Qt Code:
    1. scaleDraw->enableComponent(QwtScaleDraw::Backbone, true);
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    olredi
    Found the discrepancy of qwt_plot_grid.cpp class of version 5.2.3 (QRect) to version 6.1.6 (QRectF). In version 5.2.3 there is no subtraction done but in 6.1.6 it is necessary I guess? not really sure why QRectF right() and bottom() method was subtracted by 1.0.
    Qt Code:
    1. void QwtPlotGrid::drawLines( QPainter *painter, const QRectF &canvasRect,
    2. Qt::Orientation orientation, const QwtScaleMap &scaleMap,
    3. const QList<double> &values ) const
    4. {
    5. const double x1 = canvasRect.left();
    6. const double x2 = canvasRect.right() - 1.0;
    7. const double y1 = canvasRect.top();
    8. const double y2 = canvasRect.bottom() - 1.0;
    9. ...
    To copy to clipboard, switch view to plain text mode 
    I subtracted 1.0 to the rect.bottom() value and solved the issue with missing x-axis (my previous reply was justified that the value must be 198 to show the x-axis instead of 199)

Similar Threads

  1. Replies: 2
    Last Post: 7th May 2019, 15:26
  2. Replies: 0
    Last Post: 23rd January 2017, 11:55
  3. Replies: 1
    Last Post: 27th November 2012, 16:52
  4. Replies: 9
    Last Post: 3rd May 2011, 21:21
  5. Replies: 2
    Last Post: 29th October 2006, 07:54

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.