How to achieve show/hide Qt Charts series by clicking legendmarker in Qt Quick
-
Hi.
I want to achieve below example in Qt Quick Project.
https://doc.qt.io/qt-6/qtcharts-legendmarkers-example.htmlThis example uses the QChartView class, but I'm using the QML ChartView Type.
I don't know how to detect the LegendMarker's Clicked signal from ChartView QML.
It seems that the LegendMarker cannot be accessed from the QML Legend Type.Thank you in advance.
-
Hi ynakane,
I was trying to achieve the same thing and was unable to do it using the QML Legend Type. However, I am relatively new to Qt Quick (coming from a widgets background) so may not have been doing things correctly.
The method I found that did work was creating a custom QML legend. I based this off the tutorial example:
https://doc.qt.io/qt-6/qtcharts-qmlcustomlegend-example.html
Change the purpose of the onSelected signal to instead change the series visibility. I also changed the signal so it passed the index instead of the series name. Then in you main QML code connect to the signal like this:
CustomLegend { id: customLegend width: parent.width height: 50 anchors.top: chartView.bottom anchors.horizontalCenter: parent.horizontalCenter onSelected: { chartView.series(seriesIndex).visible = !chartView.series(seriesIndex).visible } }
I don't know if this is the best method to use but it worked well for me.