LegendMarkers Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"mainwidget.h"
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLabel>
#include <QtCore/QDebug>
#include <QtCharts/QLegend>
#include <QtWidgets/QFormLayout>
#include <QtCharts/QLegendMarker>
#include <QtCharts/QLineSeries>
#include <QtCharts/QXYLegendMarker>
#include <QtCore/QtMath>
QT_CHARTS_USE_NAMESPACE
MainWidget::
MainWidget(QWidget *
parent) :
QWidget(parent)
{
// Create chart view with the chart
m_chart =
new
QChart();
m_chartView =
new
QChartView(m_chart, this
);
// Create layout for grid and detached legend
m_mainLayout =
new
QGridLayout();
m_mainLayout-&
gt;addWidget(m_chartView, 0
, 1
, 3
, 1
);
setLayout(m_mainLayout);
// Add few series
addSeries();
addSeries();
addSeries();
addSeries();
connectMarkers();
// Set the title and show legend
m_chart-&
gt;setTitle("Legendmarker example (click on legend)"
);
m_chart-&
gt;legend()-&
gt;setVisible(true
);
m_chart-&
gt;legend()-&
gt;setAlignment(Qt::
AlignBottom);
m_chartView-&
gt;setRenderHint(QPainter::
Antialiasing);
}
void
MainWidget::
addSeries()
{
QLineSeries *
series =
new
QLineSeries();
m_series.append(series);
series-&
gt;setName(QString("line "
+
QString::
number(m_series.count())));
// Make some sine wave for data
QList&
lt;QPointF&
gt; data;
int
offset =
m_chart-&
gt;series().count();
for
(int
i =
0
; i &
lt; 360
; i++
) {
qreal x =
offset *
20
+
i;
data.append(QPointF(i, qSin(qDegreesToRadians(x))));
}
series-&
gt;append(data);
m_chart-&
gt;addSeries(series);
if
(m_series.count() ==
1
)
m_chart-&
gt;createDefaultAxes();
}
void
MainWidget::
removeSeries()
{
// Remove last series from chart
if
(m_series.count() &
gt; 0
) {
QLineSeries *
series =
m_series.last();
m_chart-&
gt;removeSeries(series);
m_series.removeLast();
delete
series;
}
}
void
MainWidget::
connectMarkers()
{
// Connect all markers to handler
const
auto
markers =
m_chart-&
gt;legend()-&
gt;markers();
for
(QLegendMarker *
marker : markers) {
// Disconnect possible existing connection to avoid multiple connections
QObject::
disconnect(marker, &
amp;QLegendMarker::
clicked,
this
, &
amp;MainWidget::
handleMarkerClicked);
QObject::
connect(marker, &
amp;QLegendMarker::
clicked, this
, &
amp;MainWidget::
handleMarkerClicked);
}
}
void
MainWidget::
disconnectMarkers()
{
const
auto
markers =
m_chart-&
gt;legend()-&
gt;markers();
for
(QLegendMarker *
marker : markers) {
QObject::
disconnect(marker, &
amp;QLegendMarker::
clicked,
this
, &
amp;MainWidget::
handleMarkerClicked);
}
}
void
MainWidget::
handleMarkerClicked()
{
QLegendMarker*
marker =
qobject_cast&
lt;QLegendMarker*&
gt; (sender());
Q_ASSERT(marker);
switch
(marker-&
gt;type())
{
case
QLegendMarker::
LegendMarkerTypeXY:
{
// Toggle visibility of series
marker-&
gt;series()-&
gt;setVisible(!
marker-&
gt;series()-&
gt;isVisible());
// Turn legend marker back to visible, since hiding series also hides the marker
// and we don't want it to happen now.
marker-&
gt;setVisible(true
);
// Dim the marker, if series is not visible
qreal alpha =
1.0
;
if
(!
marker-&
gt;series()-&
gt;isVisible())
alpha =
0.5
;
QColor color;
QBrush brush =
marker-&
gt;labelBrush();
color =
brush.color();
color.setAlphaF(alpha);
brush.setColor(color);
marker-&
gt;setLabelBrush(brush);
brush =
marker-&
gt;brush();
color =
brush.color();
color.setAlphaF(alpha);
brush.setColor(color);
marker-&
gt;setBrush(brush);
QPen pen =
marker-&
gt;pen();
color =
pen.color();
color.setAlphaF(alpha);
pen.setColor(color);
marker-&
gt;setPen(pen);
break
;
}
default
:
{
qDebug() &
lt;&
lt; "Unknown marker type"
;
break
;
}
}
}