summaryrefslogtreecommitdiffstats
path: root/examples/charts/legendmarkers
diff options
context:
space:
mode:
Diffstat (limited to 'examples/charts/legendmarkers')
-rw-r--r--examples/charts/legendmarkers/legendmarkers.pro10
-rw-r--r--examples/charts/legendmarkers/main.cpp35
-rw-r--r--examples/charts/legendmarkers/mainwidget.cpp177
-rw-r--r--examples/charts/legendmarkers/mainwidget.h62
4 files changed, 284 insertions, 0 deletions
diff --git a/examples/charts/legendmarkers/legendmarkers.pro b/examples/charts/legendmarkers/legendmarkers.pro
new file mode 100644
index 00000000..f79bdba4
--- /dev/null
+++ b/examples/charts/legendmarkers/legendmarkers.pro
@@ -0,0 +1,10 @@
+!include( ../examples.pri ) {
+ error( "Couldn't find the examples.pri file!" )
+}
+
+TARGET = legendmarkers
+SOURCES += main.cpp \
+ mainwidget.cpp
+
+HEADERS += \
+ mainwidget.h
diff --git a/examples/charts/legendmarkers/main.cpp b/examples/charts/legendmarkers/main.cpp
new file mode 100644
index 00000000..f627e496
--- /dev/null
+++ b/examples/charts/legendmarkers/main.cpp
@@ -0,0 +1,35 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://qt.digia.com
+**
+** This file is part of the Qt Enterprise Charts Add-on.
+**
+** $QT_BEGIN_LICENSE$
+** Licensees holding valid Qt Enterprise licenses may use this file in
+** accordance with the Qt Enterprise License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://qt.digia.com
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "mainwidget.h"
+
+#include <QApplication>
+#include <QMainWindow>
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+
+ MainWidget w;
+ w.resize(720, 480);
+ w.show();
+
+ return a.exec();
+}
diff --git a/examples/charts/legendmarkers/mainwidget.cpp b/examples/charts/legendmarkers/mainwidget.cpp
new file mode 100644
index 00000000..f71e7a61
--- /dev/null
+++ b/examples/charts/legendmarkers/mainwidget.cpp
@@ -0,0 +1,177 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://qt.digia.com
+**
+** This file is part of the Qt Enterprise Charts Add-on.
+**
+** $QT_BEGIN_LICENSE$
+** Licensees holding valid Qt Enterprise licenses may use this file in
+** accordance with the Qt Enterprise License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://qt.digia.com
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "mainwidget.h"
+#include <QChart>
+#include <QChartView>
+#include <QPushButton>
+#include <QLabel>
+#include <QDebug>
+#include <QLegend>
+#include <QFormLayout>
+#include <QLegendMarker>
+#include <QLineSeries>
+#include <QXYLegendMarker>
+#include <qmath.h>
+
+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->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->setTitle("Legendmarker example (click on legend)");
+ m_chart->legend()->setVisible(true);
+ m_chart->legend()->setAlignment(Qt::AlignBottom);
+
+ m_chartView->setRenderHint(QPainter::Antialiasing);
+}
+
+void MainWidget::addSeries()
+{
+ QLineSeries *series = new QLineSeries();
+ m_series.append(series);
+
+ series->setName(QString("line " + QString::number(m_series.count())));
+
+ // Make some sine wave for data
+ QList<QPointF> data;
+ int offset = m_chart->series().count();
+ for (int i = 0; i < 360; i++) {
+ qreal x = offset * 20 + i;
+ data.append(QPointF(i, qSin(2.0 * 3.141592 * x / 360.0)));
+ }
+
+ series->append(data);
+ m_chart->addSeries(series);
+
+ if (m_series.count() == 1) {
+ m_chart->createDefaultAxes();
+ }
+}
+
+void MainWidget::removeSeries()
+{
+ // Remove last series from chart
+ if (m_series.count() > 0) {
+ QLineSeries *series = m_series.last();
+ m_chart->removeSeries(series);
+ m_series.removeLast();
+ delete series;
+ }
+}
+
+void MainWidget::connectMarkers()
+{
+//![1]
+ // Connect all markers to handler
+ foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
+ // Disconnect possible existing connection to avoid multiple connections
+ QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
+ QObject::connect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
+ }
+//![1]
+}
+
+void MainWidget::disconnectMarkers()
+{
+//![2]
+ foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
+ QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
+ }
+//![2]
+}
+
+void MainWidget::handleMarkerClicked()
+{
+//![3]
+ QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
+ Q_ASSERT(marker);
+//![3]
+
+//![4]
+ switch (marker->type())
+//![4]
+ {
+ case QLegendMarker::LegendMarkerTypeXY:
+ {
+//![5]
+ // Toggle visibility of series
+ marker->series()->setVisible(!marker->series()->isVisible());
+
+ // Turn legend marker back to visible, since hiding series also hides the marker
+ // and we don't want it to happen now.
+ marker->setVisible(true);
+//![5]
+
+//![6]
+ // Dim the marker, if series is not visible
+ qreal alpha = 1.0;
+
+ if (!marker->series()->isVisible()) {
+ alpha = 0.5;
+ }
+
+ QColor color;
+ QBrush brush = marker->labelBrush();
+ color = brush.color();
+ color.setAlphaF(alpha);
+ brush.setColor(color);
+ marker->setLabelBrush(brush);
+
+ brush = marker->brush();
+ color = brush.color();
+ color.setAlphaF(alpha);
+ brush.setColor(color);
+ marker->setBrush(brush);
+
+ QPen pen = marker->pen();
+ color = pen.color();
+ color.setAlphaF(alpha);
+ pen.setColor(color);
+ marker->setPen(pen);
+
+//![6]
+ break;
+ }
+ default:
+ {
+ qDebug() << "Unknown marker type";
+ break;
+ }
+ }
+}
diff --git a/examples/charts/legendmarkers/mainwidget.h b/examples/charts/legendmarkers/mainwidget.h
new file mode 100644
index 00000000..350808a2
--- /dev/null
+++ b/examples/charts/legendmarkers/mainwidget.h
@@ -0,0 +1,62 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://qt.digia.com
+**
+** This file is part of the Qt Enterprise Charts Add-on.
+**
+** $QT_BEGIN_LICENSE$
+** Licensees holding valid Qt Enterprise licenses may use this file in
+** accordance with the Qt Enterprise License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://qt.digia.com
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef MAINWIDGET_H
+#define MAINWIDGET_H
+
+#include "qchartglobal.h"
+#include "qchart.h"
+#include "qchartview.h"
+#include <QWidget>
+#include <QGraphicsWidget>
+#include <QGridLayout>
+#include <QGraphicsGridLayout>
+#include <QDoubleSpinBox>
+#include <QGroupBox>
+#include <QLineSeries>
+
+QT_CHARTS_USE_NAMESPACE
+
+class MainWidget : public QWidget
+{
+ Q_OBJECT
+public:
+ explicit MainWidget(QWidget *parent = 0);
+
+public slots:
+ void addSeries();
+ void removeSeries();
+ void connectMarkers();
+ void disconnectMarkers();
+
+ void handleMarkerClicked();
+
+private:
+
+ QChart *m_chart;
+ QList<QLineSeries *> m_series;
+
+ QChartView *m_chartView;
+ QGridLayout *m_mainLayout;
+ QGridLayout *m_fontLayout;
+
+};
+
+#endif // MAINWIDGET_H