QCustomPlot Discussion and Comments

Plot legend with multiple QCPAxisRect in one QCustomPlot?Return to overview

Hi guys,

in my application i create multiple QCPAxisRects in one QCustomPlot area and i add one or more graph to each axis rect. This works fine, but how can i create a legend for the graph(s) in each axis area?

When i try plotter->legend->setVisible(true);, the application crashs... Does anybody know how to do this?

Greets Taneeda


The application crashed because i called plotter->plotLayout()->clear(); at first. Without this, the legend is visible, but in one box inside the plotter area.

In the documentation i read:
"By default, every QCustomPlot has one legend (QCustomPlot::legend) which is placed in the inset layout of the main axis rect (QCPAxisRect::insetLayout)."

So, is it possible to create an own legend for every created axis rect? I had no success with this until now, so i stay trying. I welcome any ideas :)

Greets Taneeda

Yes, you can create individual legends for each axis rect. Note though that you have to take care of adding/removing the plottables to/from the new legends yourself.

Have a look at how the layout system works, because the legend is just a layout element in the insetLayout() of a QCPAxisRect. The documentation of QCPAxisRect and QCPLayoutInset might also be useful to you.

Sorry to bring this thread back to life.

My application is identical to Taneeda's.

I got as far as adding the legend to the QCPAxisRect (called xRect):

QCPLegend *legend1 = new QCPLegend();
QCPLayoutInset * xLayout = xRect->insetLayout();
xLayout->addElement((QCPLayoutElement*) legend1, Qt::AlignBottom|Qt::AlignRight);

Now I struggle with adding/removing the plottables to my legend (each QCPAxisRect has multiple QCPGraphs). Any pointers are greatly appreciated!

Does anybody has done this yet? At the Moment i just use a single legend box over the different axis rects...

This really seems to be a tricky one (possibly DerManu can help?)

I tried

legend1->addItem((QCPAbstractLegendItem*) xGraph);

but to no avail.

@DerManu: you suggested that it would be possible to append a legend to individual axis rects. Unfortunately, I cannot find a solution in the url you posted. Could you give us another pointer? Thanks a lot!

Was answered very recent and detailed with code, in here

Beautiful. Thanks a lot!!