Hi,
I have multiple axisRect on a window and I want to add legend to them. Each axisRect can have more than one graph on it.
I have written this code to do this:
void plotWidget::setLegend(QStringList list, QVector<int> Index, QVector<int> SubPlot) { QFont legendFont = font(); legendFont.setPointSize(9); QString leg; for (int i = 0; i < Index.size(); i++) { leg = list.at(Index[i]-1); sub_graphs_[i]->setName(leg); } ui->plotWindow->setAutoAddPlottableToLegend(false); for (int i = 0; i < SubPlot.size(); i++) { QCPLegend *leg = new QCPLegend(); leg->setFont(legendFont); leg->setLayer(QString::number(i)); m_legend.push_back(leg); int value = SubPlot.at(i) - 1; axis_rect_[value]->insetLayout()->addElement(leg, Qt::AlignTop|Qt::AlignRight); leg->addItem(new QCPPlottableLegendItem(leg, sub_graphs_[i])); } ui->plotWindow->replot(); }
In the first for bucle I add the name to each graph, and in the second I add a Legend to each axisrect. The problem is that the legend appears overwrite when, in the same axisrect, I have more than one graph.
Thank for your help!