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.
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.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Actually, you can do something about your problem.
The thing is to select the marker inside the legend and set it to be invisible:
Qt 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);To copy to clipboard, switch view to plain text mode
I haven't tested it in C++ but this method works with PyQt5.
maitai (16th March 2023)
In Qt c++, you can hide the legend of series using:
Qt Code:
QChat * myChart = new QChart(); myChart->addSeries(mySeries); myChart->legend()->hide();To copy to clipboard, switch view to plain text mode
Last edited by abbaasi69; 12th October 2020 at 16:02. Reason: missing [code] tags
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)?"In Qt c++, you can hide the legend of series using:
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
One can get a list of the markers by
Qt Code:
QChart * myChart = new QChart(); auto markersList = myChart->legend()->markers();To copy to clipboard, switch view to plain text mode
and then set the marker for the curve i to not be visible by
Qt Code:
markersList[i]->setVisible(false);To copy to clipboard, switch view to plain text mode
Hope that helped.
Bookmarks