I have a chart that has multiple graphs on it that can be filtered with user input. When the user chooses to hide the graph, the graph legend item remains in the legend. I would like this to be hidden when the graph is not visible and shown when the graph is. I want this behavior because I want to be able to set up the legend once, and I want the order of the legend to remain the same when items are hidden and shown again.
I was hoping for some behavior like QCustomPlot::rescaleAxis where I could tell the legend not to draw invisible items. I did not find that.
I was able to implement the functionality that I want by changing QCPPlottableLegendItem::minimumSizeHint to look at the visibility of the plottable that it contains.
QSize QCPPlottableLegendItem::minimumSizeHint() const { if (!mPlottable) return QSize(); if (mPlottable->layer() && !mPlottable->layer()->visible()) return QSize(); // New code if (!mPlottable->visible()) return QSize(); // New code QSize result(0, 0); QRect textRect; QFontMetrics fontMetrics(getFont()); QSize iconSize = mParentLegend->iconSize(); textRect = fontMetrics.boundingRect(0, 0, 0, iconSize.height(), Qt::TextDontClip, mPlottable->name()); result.setWidth(iconSize.width() + mParentLegend->iconTextPadding() + textRect.width() + mMargins.left() + mMargins.right()); result.setHeight(qMax(textRect.height(), iconSize.height()) + mMargins.top() + mMargins.bottom()); return result; }
I had to check the visibility of the layer as well as the plottable because the plottable shows as visible even if it's layer is not. I was hoping to override the visible method to add this behavior to the plottable, but visible is not a virtual method.
Is this functionality that could be incorporated back into the code branch?
I am using layers to show/hide groups of graphs because I need the Z ordering and it is really easy partition the graphs when the chart is created and then not worry about what is in each partition. Great functionality.