PDA

View Full Version : qchart hide legend for part of series



marrierius
7th December 2016, 22:03
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.

d_stranz
8th December 2016, 16:34
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 (http://www.qcustomplot.com/documentation/index.html) as an alternative library. The documentation for its legend class seems to indicate that legend items can be made individually visible. See QCPLegend (http://www.qcustomplot.com/documentation/classQCPLegend.html) in the documentation.

corentin592
14th March 2017, 13:40
Actually, you can do something about your problem.

The thing is to select the marker inside the legend and set it to be invisible:



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.

abbaasi69
12th October 2020, 16:01
In Qt c++, you can hide the legend of series using:


QChat * myChart = new QChart();
myChart->addSeries(mySeries);
myChart->legend()->hide();

d_stranz
12th October 2020, 17:42
In Qt c++, you can hide the legend of series using:

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)?"

klf
9th September 2021, 14:11
One can get a list of the markers by

QChart * myChart = new QChart();
auto markersList = myChart->legend()->markers();

and then set the marker for the curve i to not be visible by

markersList[i]->setVisible(false);

Hope that helped.