PDA

View Full Version : how to customize the legend postion,so that i can bring the legends inside the plot?



mammaiap
3rd October 2011, 06:07
Hi Friends,

i need to create the custom legend ... so that i can bring the legends inside the plot canvas....

that means i wanted to change the Qwt Legend Position as per my wish...but By default Qwt provide the legend postions( LeftLegend,
RightLegend,
BottomLegend,
TopLegend,

ExternalLegend)......


i am creating the legend by :

insertLegend(new QwtLegend(), QwtPlot::RightLegend);

defaultcurve->setLegendAttribute(QwtPlotCurve::LegendShowLine,tr ue);
defaultcurve->setLegendAttribute(QwtPlotCurve::LegendShowSymbol, true);



please go through the "gnuplot.png & qwtplot.png" image files...

my client want exactly the same legend location as it in the "gnuplot.png"...

so to achieve this what i need to do?... as well as i wanted to increase the length of the legend line

i need to overload the qwt legend class????? or qwt legend item class??

please provide some code example... that will be highly helpful...

Thanks,
Muthu

Uwe
3rd October 2011, 08:54
Don't crosspost !

Uwe

mammaiap
4th October 2011, 06:16
Hi Uwe,

sorry for cross posting...

and i have found the solution...

my solution is:

i create the custom legend and set the geometry inside the plot canvas..


QwtLegend *customLegend = new QwtLegend(this);
insertLegend(customLegend, QwtPlot::ExternalLegend);
customLegend->setGeometry(QRect(70,30,120,100));
customLegend->setMaximumWidth(120);
customLegend->setMinimumWidth(120);
customLegend->setMaximumHeight(100);
customLegend->setMinimumHeight(100);

this works perfectly.... and while save the plot into png file format ... i overload the QwtPlotRenderer functions

void renderCanvas( const QwtPlot *plot,QPainter *painter, const QRectF &canvasRect,const QwtScaleMap* maps ) const;

and

void renderLegend(const QwtPlot *plot, QPainter *painter, const QRectF &rect) const;

now it saves the *.png file with legends inside the plot canvas....this is exactly what i wanted to achieve...

i am attaching the newly created qwtplot_new.png.....

Thanks and Regards,
Muthulingam

Uwe
4th October 2011, 06:57
QwtLegend *customLegend = new QwtLegend(this);
insertLegend(customLegend, QwtPlot::ExternalLegend);
customLegend->setGeometry(QRect(70,30,120,100));


Assuming, that the position of the legend is supposed to be relative to the canvas and "this" is the plot widget itself:

Install an event filter for the plot canvas and adjust position of your legend for each QEvent::Move event. Otherwise the legend will jump f.e when you zoom in and the tick labels of the scales need more or less space.

Also use the the sizeHint of the legend for finding its geometry. Otherwise your layout might break when running in an environment f.e. with different fonts.





customLegend->setMaximumWidth(120);
customLegend->setMinimumWidth(120);
customLegend->setMaximumHeight(100);
customLegend->setMinimumHeight(100);

These lines are completely pointless, when you resize the legend with setGeometry yourself.

Uwe

jwieland
25th October 2012, 19:56
Hi Muthulingam,

I am trying to do exactly what you did and having problem inserting items (lines with icon) into legend. Could you show me how to achieve that?

Thank you

--

Never mind. I have figured it out.
Thank you so much for your wonderful post.

Uwe
26th October 2012, 08:46
Note that in Qwt from SVN trunk you have the option to use QwtPlotLegendItem instead of ( or additionally to ) the legend.

Uwe