QCustomPlot Discussion and Comments

Position of legend outside graphReturn to overview

Hey guys,

is there any chance to set the position of the legend outside of the graph?

I looked in the documentation, but i didn't find a way to do this.

I hope someone can help me ;)

I found a solution to place the legend outside the graph.
At first I increase the right margin to 150 px, than i place the
legend manual to the top right corner.

  customPlot->axisRect()->setAutoMargins(QCP::msLeft | QCP::msTop | QCP::msBottom);
  customPlot->axisRect()->setMargins(QMargins(0,0,150,0));
  customPlot->axisRect()->insetLayout()->setInsetPlacement(0, QCPLayoutInset::ipFree);
  customPlot->axisRect()->insetLayout()->setInsetRect(0, QRectF(1.1,0,0.1,0.1));

The correct values depends on your legend an the positions may be corrected if the window size will change, because the postion is relativ to the window size and the margin is a fixed value. In my test the legend will move out if I increase the window size.
I hope any other will find be a better solution.

The layout system allows placing the legend (which itself is just another layout element) next to the axis rect.
For example, try this as a starting point:

customPlot->plotLayout()->addElement(0, 1, customPlot->legend);

You'll see that now the legend takes up the entire right portion, stretching to fill the area. If you want the legend to be more compact, you could add a sub-layout and insert the legend above a (dummy) layout element which takes up most of the space, compacting the legend to its minimum size. Consider this:

  QCPLayoutGrid *subLayout = new QCPLayoutGrid;
  QCPLayoutElement *dummyElement = new QCPLayoutElement;
  customPlot->plotLayout()->addElement(0, 1, subLayout); // add sub-layout in the cell to the right of the main axis rect
  subLayout->addElement(0, 0, customPlot->legend); // add legend
  subLayout->addElement(1, 0, dummyElement); // add dummy element below legend
  subLayout->setRowStretchFactor(0, 0.01); // make legend cell (in row 0) take up as little vertical space as possible

If I try the solution of DerManu, the legend still use the half width of the widget.
I add the following line to maximize the legend to 150px.

subLayout->setMaximumSize(150, QWIDGETSIZE_MAX);
The height was set in this case to the maximum.

Is there an solution to set the width to the minimum to show the whole legend, or how could I read the nessesarry width of the legend?

It works in the same way as I controlled the vertical stretch factor, only this time on the top layout (the one managing the axis rect vs the subLayout) via the horizontal stretch factor for the columns:
customPlot->plotLayout()->setColumnStretchFactor(1, 0.01);
This will make the second column (the one with the subLayout and thus the legend) shrink as far as possible. Well, actually it tells the system to keep the right column at a width that's 1% of the left column, but effectively that means it's always at its minimum size.

I've tried all the suggestions above and while I see a column of space next to the plot, I don't see the legend text.

I've also tried adding this with no luck:

    qcpIbrPressure->autoAddPlottableToLegend();
    customPlot->graph(0)->setName("Test");  // graph(0) contains my data line

Is there something in the .ui file that I need to do to make it specify the text and display the legend?

ben, try this:

customPlot->legend->setVisible(true);

My version of DerManu's solution is

	QCPLayoutGrid *subLayout = new QCPLayoutGrid;
	ui->customPlot->plotLayout()->addElement(0, 1, subLayout);
	subLayout->addElement(0, 0, new QCPLayoutElement);
	subLayout->addElement(1, 0, ui->customPlot->legend);
	subLayout->addElement(2, 0, new QCPLayoutElement);
	ui->customPlot->plotLayout()->setColumnStretchFactor(1, 0.001);

It produces a small legend aligned to the right and vertically centered.

P.S. I do know that the thread is ancient but it's one of the first Google results.

StSav012's solution is great