2 Attachment(s)
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).
Attachment 13659
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.
Attachment 13660
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)
Code:
//PlotPanel.cpp
PlotPanel
::PlotPanel(PlotStyle plotStyle, RulerType rulerType,
QWidget *parent
) : m_plot(0),
m_plotBackground(0),
m_plotGrid(0),
m_plotRulerPanel(0)
{
setFixedSize(StyleInfo::PLOT_PANEL_WIDTH, StyleInfo::PLOT_PANEL_HEIGHT);
m_plot = new Plot(plotStyle, this);
m_plot->move(PLOT_HORIZONTAL_BORDER, PLOT_TOP_BORDER);
m_plotRulerPanel = new PlotRulerPanel(rulerType, this);
m_plotRulerPanel->move(PLOT_HORIZONTAL_BORDER, 0);
m_plotRulerPanel->setPlot(m_plot);
m_plotBackground = new PlotBackground;
m_plotBackground->attach(m_plot);
m_plotGrid = new PlotGrid;
m_plotGrid->attach(m_plot);
}
The PlotPanel class is the one that initializes other plot classes that is used by the class that displays the graph.
Code:
//Plot.cpp
Plot
::Plot(PlotStyle plotStyle,
QWidget *parent
) : m_xBottomScaleDraw(0),
m_xTopScaleDraw(0),
m_yLeftScaleDraw(0),
m_yRightScaleDraw(0)
{
setFixedSize(StyleInfo::PLOT_WIDTH, StyleInfo::PLOT_HEIGHT);
plotLayout()->setAlignCanvasToScales(true);
plotLayout()->setSpacing(0);
for (int i=0; i<QwtPlot::axisCnt; i++)
{
if (scaleWidget)
{
scaleWidget->setSpacing(0);
scaleWidget->setMargin(0);
scaleWidget->setMinBorderDist(0, 0);
{
color = StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_ONE);
}
{
color = StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_TWO);
}
else
{
color = StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_THREE);
}
QPalette palette
= scaleWidget
->palette
();
palette.
setColor(QPalette::Text, color
);
palette.
setColor(QPalette::WindowText, color
);
scaleWidget->setPalette(palette);
}
QFont font
= StyleInfo
::font(StyleInfo
::FONT_SIZE_SMALL);
setAxisFont(i, font);
}
if (plotStyle == TIME_PLOT)
{
m_xBottomScaleDraw = new PlotTimeScaleDraw; // will be deleted by QwtPlot
}
else
{
m_xBottomScaleDraw = new PlotScaleDraw; // will be deleted by QwtPlot
}
m_xBottomScaleDraw->setSpacing(X_SPACING);
m_xBottomScaleDraw->setMinimumExtent(X_BOTTOM_EXTENT);
setAxisScaleDraw
(QwtPlot::xBottom, m_xBottomScaleDraw
);
m_xTopScaleDraw = new PlotScaleDraw; // will be deleted by QwtPlot
m_xTopScaleDraw->setSpacing(X_SPACING);
m_xTopScaleDraw->setMinimumExtent(X_TOP_EXTENT);
setAxisScaleDraw
(QwtPlot::xTop, m_xTopScaleDraw
);
m_yLeftScaleDraw = new PlotScaleDraw; // will be deleted by QwtPlot
m_yLeftScaleDraw->setSpacing(Y_SPACING);
m_yLeftScaleDraw->setMinimumExtent(Y_LEFT_EXTENT);
setAxisScaleDraw
(QwtPlot::yLeft, m_yLeftScaleDraw
);
m_yRightScaleDraw = new PlotScaleDraw; // will be deleted by QwtPlot
m_yRightScaleDraw->setSpacing(Y_SPACING);
m_yRightScaleDraw->setMinimumExtent(Y_RIGHT_EXTENT);
setAxisScaleDraw
(QwtPlot::yRight, m_yRightScaleDraw
);
for (int i=0; i<QwtPlot::axisCnt; i++)
{
axisScaleDraw
(i
)->setTickLength
(QwtScaleDiv::MinorTick, TICK_LENGTH
);
axisScaleDraw
(i
)->setTickLength
(QwtScaleDiv::MediumTick, TICK_LENGTH
);
axisScaleDraw
(i
)->setTickLength
(QwtScaleDiv::MajorTick, TICK_LENGTH
);
enableAxis(i);
}
setAxisMaxMajor
(QwtPlot::xBottom, MAX_MAJOR_TICKS
);
setAxisMaxMinor
(QwtPlot::xBottom, MAX_MINOR_TICKS
);
setAxisMaxMajor
(QwtPlot::xTop, MAX_MAJOR_TICKS
);
setAxisMaxMinor
(QwtPlot::xTop, MAX_MINOR_TICKS
);
setAxisMaxMajor
(QwtPlot::yLeft, MAX_MAJOR_TICKS
);
setAxisMaxMinor
(QwtPlot::yLeft, MAX_MINOR_TICKS
);
setAxisMaxMajor
(QwtPlot::yRight, MAX_MAJOR_TICKS
);
setAxisMaxMinor
(QwtPlot::yRight, MAX_MINOR_TICKS
);
}
Plot::~Plot()
{
}
void Plot::showAxis(Axis axis, bool show)
{
if (scaleDraw)
{
}
updateAxes();
}
int Plot::scaleWidth(Axis axis) const
{
if ((axis >
= 0) && (axis <
QwtPlot::axisCnt)) {
if (scaleWidget)
{
return scaleWidget->width();
}
}
return 0;
}
int Plot::scaleHeight(Axis axis) const
{
if ((axis >
= 0) && (axis <
QwtPlot::axisCnt)) {
if (scaleWidget)
{
return scaleWidget->height();
}
}
return 0;
}
Code:
PlotGrid::PlotGrid() :
{
}
{
Q_ASSERT(painter);
painter->save();
painter->setOpacity(0.2);
painter
->setPen
(QPen(StyleInfo
::skinColor(StyleInfo
::SKIN_COLOR_PLOT_THREE)));
drawLines(painter, rect, Qt::Vertical, xMap,
painter
->setPen
(QPen(StyleInfo
::skinColor(StyleInfo
::SKIN_COLOR_PLOT_ONE)));
drawLines(painter, rect, Qt::Horizontal, yMap,
painter->setOpacity(1.0);
// paint scale backbones
painter->setPen(StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_ONE));
painter->drawLine((int)rect.left(), (int)rect.top(), (int)rect.left(), (int)rect.bottom());
painter->setPen(StyleInfo::skinColor(StyleInfo::SKIN_COLOR_PLOT_THREE));
painter->drawLine((int)rect.left(), (int)rect.bottom(), (int)rect.right(), (int)rect.bottom());
painter->restore();
}
{
m_xScaleDiv = xScaleDiv;
m_yScaleDiv = yScaleDiv;
}
void PlotGrid::update(void)
{
// todo: is there a better way to force an update??
{
if (plotCanvas)
{
plotCanvas->invalidateBackingStore();
plotCanvas->update();
}
}
}
Qt
::Orientation orientation,
const QwtScaleMap &scaleMap,
const QList<double> &values) const
{
Q_ASSERT(painter);
const int x1 = (int)rect.left();
const int x2 = (int)rect.right();
const int y1 = (int)rect.top();
const int y2 = (int)rect.bottom();
for (uint i = 0; i < (uint)values.count(); i++)
{
const int value = qRound(scaleMap.transform(values[i]));
if (orientation == Qt::Horizontal)
{
if ((value >= y1) && (value <= y2))
{
painter->drawLine(x1, value, x2, value);
}
}
else
{
if ((value >= x1) && (value < x2)) // suppress last line on the right
{
painter->drawLine(value, y1, value, y2);
}
}
}
}
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
Re: Qwt x-axis is missing in the chart
You would need to provide a minimal example, that can be compiled and executed.
Uwe
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
Quote:
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
Thanks,
olredi
Re: Qwt x-axis is missing in the chart
Quote:
Originally Posted by
olredi
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
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.
Code:
Qt
::Orientation orientation,
const QwtScaleMap &scaleMap,
const QList<double> &values ) const
{
const double x1 = canvasRect.left();
const double x2 = canvasRect.right() - 1.0;
const double y1 = canvasRect.top();
const double y2 = canvasRect.bottom() - 1.0;
...
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)