PDA

View Full Version : Sharing on QwtPlotPicker showing values of dual Y axes



davey
4th November 2010, 02:15
I have a plot with both Y axes enabled, but find the picker by default will only show value of either one.
After a while I have figured out a solution, though may not be elegant. :o


At the beginning I thought of having two QwtPlotPicker showing each axis but then it turns out their labels overlap and I could not control the label position.
So I came up with another thought: Modify what the picker displays on its label.

I first sub-class QwtPlotPicker.
I was thinking about overriding updateDisplay() then draw the label manually.
However after reading the source I know that I could override trackerText() which is a virtual function.

In the class header:

protected:
virtual QwtText trackerText(QPoint const &pos) const;


For the implementation of trackerText(), all I need is to return a text with both Y axes value but the problem is how.
A picker must know also the Y value of the other picker, so I passed a reference of the other picker in the constructor.


protected:
QwtPlotPicker *alt_picker;

In trackerText(), I get the Y value of alt_picker by calling its trackerText() function.
QwtPlotPicker::trackerText() is protected so I instead call QwtPicker::trackerText().
I actually re-organized the text content of both picker but here is a simple append solution.


QwtText const &alt_text = ((QwtPicker*)(alt_picker))->trackerText(alt_picker->trackerPosition());
QString alt_y(alt_text.text().split(",").at(1));
QString result(QwtPlotPicker::trackerText(pos).text() + ", " + alt_y);
return QwtText(result);

I do not know if there is a better way and I would love to know.
At least it works for me and it is not that cumbersome. :p

Uwe
4th November 2010, 07:00
For the implementation of trackerText(), all I need is to return a text with both Y axes value but the problem is how.

You can use the canvas maps:


virtual QwtText YourPicker::trackerText(const QwtDoublePoint &pos) const
{
QPointF p = transform(pos);

int axis = (yAxis() == QwtPlot::yRight) ? QwtPlot::yLeft : QwtPlot::yRight:
double y2 = plot()->canvasMap(axis).invTransform(p.y());

...
}

In your special situation, where you simple append the second y value to the text you could also do the following:


virtual QwtText YourPicker::trackerText(const QPoint &pos) const
{
int axis = (yAxis() == QwtPlot::yRight) ? QwtPlot::yLeft : QwtPlot::yRight:
double y2 = plot()->canvasMap(axis).transform(p.y());

QwtText text = QwtPlotPicker::trackerText(pos);
text += ...
return text:
}

Uwe