1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include "mainwidget.h"
31#include <QtCharts/QChart>
32#include <QtCharts/QChartView>
33#include <QtWidgets/QPushButton>
34#include <QtWidgets/QLabel>
35#include <QtCore/QDebug>
36#include <QtCharts/QLegend>
37#include <QtWidgets/QFormLayout>
38#include <QtCharts/QLegendMarker>
39#include <QtCharts/QLineSeries>
40#include <QtCharts/QXYLegendMarker>
41#include <QtCore/QtMath>
42
43QT_CHARTS_USE_NAMESPACE
44
45MainWidget::MainWidget(QWidget *parent) :
46 QWidget(parent)
47{
48 // Create chart view with the chart
49 m_chart = new QChart();
50 m_chartView = new QChartView(m_chart, this);
51
52 // Create layout for grid and detached legend
53 m_mainLayout = new QGridLayout();
54 m_mainLayout->addWidget(m_chartView, row: 0, column: 1, rowSpan: 3, columnSpan: 1);
55 setLayout(m_mainLayout);
56
57 // Add few series
58 addSeries();
59 addSeries();
60 addSeries();
61 addSeries();
62
63 connectMarkers();
64
65 // Set the title and show legend
66 m_chart->setTitle("Legendmarker example (click on legend)");
67 m_chart->legend()->setVisible(true);
68 m_chart->legend()->setAlignment(Qt::AlignBottom);
69
70 m_chartView->setRenderHint(hint: QPainter::Antialiasing);
71}
72
73void MainWidget::addSeries()
74{
75 QLineSeries *series = new QLineSeries();
76 m_series.append(t: series);
77
78 series->setName(QString("line " + QString::number(m_series.count())));
79
80 // Make some sine wave for data
81 QList<QPointF> data;
82 int offset = m_chart->series().count();
83 for (int i = 0; i < 360; i++) {
84 qreal x = offset * 20 + i;
85 data.append(t: QPointF(i, qSin(v: qDegreesToRadians(degrees: x))));
86 }
87
88 series->append(points: data);
89 m_chart->addSeries(series);
90
91 if (m_series.count() == 1)
92 m_chart->createDefaultAxes();
93}
94
95void MainWidget::removeSeries()
96{
97 // Remove last series from chart
98 if (m_series.count() > 0) {
99 QLineSeries *series = m_series.last();
100 m_chart->removeSeries(series);
101 m_series.removeLast();
102 delete series;
103 }
104}
105
106void MainWidget::connectMarkers()
107{
108//![1]
109 // Connect all markers to handler
110 const auto markers = m_chart->legend()->markers();
111 for (QLegendMarker *marker : markers) {
112 // Disconnect possible existing connection to avoid multiple connections
113 QObject::disconnect(sender: marker, signal: &QLegendMarker::clicked,
114 receiver: this, slot: &MainWidget::handleMarkerClicked);
115 QObject::connect(sender: marker, signal: &QLegendMarker::clicked, receiver: this, slot: &MainWidget::handleMarkerClicked);
116 }
117//![1]
118}
119
120void MainWidget::disconnectMarkers()
121{
122//![2]
123 const auto markers = m_chart->legend()->markers();
124 for (QLegendMarker *marker : markers) {
125 QObject::disconnect(sender: marker, signal: &QLegendMarker::clicked,
126 receiver: this, slot: &MainWidget::handleMarkerClicked);
127 }
128//![2]
129}
130
131void MainWidget::handleMarkerClicked()
132{
133//![3]
134 QLegendMarker* marker = qobject_cast<QLegendMarker*> (object: sender());
135 Q_ASSERT(marker);
136//![3]
137
138//![4]
139 switch (marker->type())
140//![4]
141 {
142 case QLegendMarker::LegendMarkerTypeXY:
143 {
144//![5]
145 // Toggle visibility of series
146 marker->series()->setVisible(!marker->series()->isVisible());
147
148 // Turn legend marker back to visible, since hiding series also hides the marker
149 // and we don't want it to happen now.
150 marker->setVisible(true);
151//![5]
152
153//![6]
154 // Dim the marker, if series is not visible
155 qreal alpha = 1.0;
156
157 if (!marker->series()->isVisible())
158 alpha = 0.5;
159
160 QColor color;
161 QBrush brush = marker->labelBrush();
162 color = brush.color();
163 color.setAlphaF(alpha);
164 brush.setColor(color);
165 marker->setLabelBrush(brush);
166
167 brush = marker->brush();
168 color = brush.color();
169 color.setAlphaF(alpha);
170 brush.setColor(color);
171 marker->setBrush(brush);
172
173 QPen pen = marker->pen();
174 color = pen.color();
175 color.setAlphaF(alpha);
176 pen.setColor(color);
177 marker->setPen(pen);
178
179//![6]
180 break;
181 }
182 default:
183 {
184 qDebug() << "Unknown marker type";
185 break;
186 }
187 }
188}
189

source code of qtcharts/examples/charts/legendmarkers/mainwidget.cpp