Hello,
I have a QChart which contains 4 line series. I would like to set legend for only two of them. Any suggestions?
Many thanks.
Printable View
Hello,
I have a QChart which contains 4 line series. I would like to set legend for only two of them. Any suggestions?
Many thanks.
For something in a Qt library, I am disappointed in how poorly QtCharts are designed. It is almost impossible to customize them in any way - there are no virtual methods you can override to customize behavior or add new series types, and all of the implementation is hidden in private classes that are inaccessible and can't be modified without a complete rewrite.
Your problem is just another example of the "take it or leave it" design philosophy, and I am afraid you are stuck with it. The legend is owned by the chart, and the chart puts an entry on the legend for each series. You can't have a series be visible on the chart without it also being visible in the legend, and vice versa.
You might want to look at QCustomPlot as an alternative library. The documentation for its legend class seems to indicate that legend items can be made individually visible. See QCPLegend in the documentation.
Actually, you can do something about your problem.
The thing is to select the marker inside the legend and set it to be invisible:
Code:
serie_without_marker = new QLineSeries(); chart.addSeries(serie_without_marker); // we select the marker associated with serie_without_marker chart.legend().markers(serie_without_marker)[0].setVisible(false);
I haven't tested it in C++ but this method works with PyQt5.
In Qt c++, you can hide the legend of series using:
Code:
QChat * myChart = new QChart(); myChart->addSeries(mySeries); myChart->legend()->hide();
This does not answer the original question. The was question was not "How do I hide the legend?", it was "How do I hide -part- of the legend and show other parts (series)?"Quote:
In Qt c++, you can hide the legend of series using:
One can get a list of the markers by
Code:
QChart * myChart = new QChart(); auto markersList = myChart->legend()->markers();
and then set the marker for the curve i to not be visible by
Code:
markersList[i]->setVisible(false);
Hope that helped.