I subclass QwtPlot and create function to set minValue, maxValue and scaleEngine of the axis. Also I draw axis inside plot canvas. Here is relevant part of the code:
void GeneralPlot::drawAxisInsideCanvas(){
scaleItemY->setBorderDistance(0);
scaleItemY->attach(this);
this
->enableAxis
(QwtPlot::yLeft,
false);
this->setAxisScale(this->yLeft,0,1);
this->setAxisMaxMinor(this->yLeft,0);
scaleItemX->setBorderDistance(0);
scaleItemX->attach(this);
this
->enableAxis
(QwtPlot::xBottom,
false);
this->setAxisScale(this->xBottom,0,100);
this->setAxisMaxMinor(this->xBottom,0);
this->updateAxes();
}
void GeneralPlot::setXAxis(double xmin, double xmax, bool linear){
bool xlinear = dynamic_cast<QwtLinearScaleEngine*>(axisScaleEngine(xBottom))!=NULL;
if(xlinear){
setXAxisScale(xmin, xmax);
setXAxisLinear(linear);
}else{
setXAxisLinear(linear);
setXAxisScale(xmin, xmax);
}
this->updateAxes();
}
void GeneralPlot::setXAxisScale(double xmin, double xmax){
setAxisScale(this->xBottom, xmin, xmax);
}
void GeneralPlot::setXAxisLinear(bool b){
bool xlinear = (dynamic_cast<QwtLinearScaleEngine*>(axisScaleEngine(xBottom))!=NULL);
if(xlinear&&!b){
setAxisScaleEngine(xBottom, new QwtLogScaleEngine());
}
else if(!xlinear&&b){
}
this->updateAxes();
}
void GeneralPlot::drawAxisInsideCanvas(){
QwtPlotScaleItem *scaleItemY =
new QwtPlotScaleItem(QwtScaleDraw::RightScale);
scaleItemY->setBorderDistance(0);
scaleItemY->attach(this);
this->enableAxis(QwtPlot::yLeft, false);
this->setAxisScale(this->yLeft,0,1);
this->setAxisMaxMinor(this->yLeft,0);
QwtPlotScaleItem *scaleItemX =
new QwtPlotScaleItem(QwtScaleDraw::TopScale);
scaleItemX->setBorderDistance(0);
scaleItemX->attach(this);
this->enableAxis(QwtPlot::xBottom, false);
this->setAxisScale(this->xBottom,0,100);
this->setAxisMaxMinor(this->xBottom,0);
this->updateAxes();
}
void GeneralPlot::setXAxis(double xmin, double xmax, bool linear){
bool xlinear = dynamic_cast<QwtLinearScaleEngine*>(axisScaleEngine(xBottom))!=NULL;
if(xlinear){
setXAxisScale(xmin, xmax);
setXAxisLinear(linear);
}else{
setXAxisLinear(linear);
setXAxisScale(xmin, xmax);
}
this->updateAxes();
}
void GeneralPlot::setXAxisScale(double xmin, double xmax){
setAxisScale(this->xBottom, xmin, xmax);
}
void GeneralPlot::setXAxisLinear(bool b){
bool xlinear = (dynamic_cast<QwtLinearScaleEngine*>(axisScaleEngine(xBottom))!=NULL);
if(xlinear&&!b){
setAxisScaleEngine(xBottom, new QwtLogScaleEngine());
}
else if(!xlinear&&b){
setAxisScaleEngine(xBottom, new QwtLinearScaleEngine());
}
this->updateAxes();
}
To copy to clipboard, switch view to plain text mode
GeneralPlot::drawAxisInsideCanvas() function is called in constructor. The problem is that sometimes axis is not updated, however plot canvas is updated. For example if I call :
plot->setXAxis(1, 100, false) //1 - 100 log scale
plot->replot()
plot->setXAxis(1, 100, false) //1 - 100 log scale
plot->replot()
To copy to clipboard, switch view to plain text mode
everything is fine, but if after that I call
plot->setXAxis(-100, 100, true) // linear scale
plot->replot()
plot->setXAxis(-100, 100, true) // linear scale
plot->replot()
To copy to clipboard, switch view to plain text mode
Axis is not updated, however plot with grid is already updated. If I resize plot window then axis updates. Everything works fine if I don't plot axis inside plot canvas (don't call drawAxisInsideCanvas() in constructor) then everything is fine. How shoul I make QwtPlotScaleItem to be updated after plot replot?
Bookmarks