-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzoomablechartwidget.cpp
248 lines (211 loc) · 8.46 KB
/
zoomablechartwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "zoomablechartwidget.h"
#include "ui_zoomablechartwidget.h"
#include <QtCharts/QLegendMarker>
#include <QDebug>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QT_CHARTS_USE_NAMESPACE
#endif
ZoomableChartWidget::ZoomableChartWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ZoomableChartWidget)
{
ui->setupUi(this);
QIcon panIcon;
panIcon.addFile(QStringLiteral(":/icons/pan.png"), QSize(), QIcon::Normal, QIcon::Off);
ui->comboBoxZoomMode->addItem(panIcon, QString(), ZoomableChartView::Pan);
QIcon rectZoomIcon;
rectZoomIcon.addFile(QStringLiteral(":/icons/rectangle_zoom.png"), QSize(), QIcon::Normal, QIcon::Off);
ui->comboBoxZoomMode->addItem(rectZoomIcon, QString(), ZoomableChartView::RectangleZoom);
QIcon vZoomIcon;
vZoomIcon.addFile(QStringLiteral(":/icons/vertical_zoom.png"), QSize(), QIcon::Normal, QIcon::Off);
ui->comboBoxZoomMode->addItem(vZoomIcon, QString(), ZoomableChartView::VerticalZoom);
QIcon hZoomIcon;
hZoomIcon.addFile(QStringLiteral(":/icons/horizontal_zoom.png"), QSize(), QIcon::Normal, QIcon::Off);
ui->comboBoxZoomMode->addItem(hZoomIcon, QString(), ZoomableChartView::HorizontalZoom);
ui->chartView->setZoomMode(ZoomableChartView::Pan);
m_chart = new ZoomableChart();
ui->chartView->setChart(m_chart);
connect(m_chart, &ZoomableChart::seriesAdded, this, &ZoomableChartWidget::seriesAdded);
connect(m_chart, &ZoomableChart::seriesRemoved, this, &ZoomableChartWidget::seriesRemoved);
}
ZoomableChartWidget::~ZoomableChartWidget()
{
delete m_chart;
delete ui;
}
void ZoomableChartWidget::on_comboBoxZoomMode_activated(int index)
{
ui->chartView->setZoomMode(static_cast<ZoomableChartView::ZoomMode>(ui->comboBoxZoomMode->itemData(index).toInt()));
}
void ZoomableChartWidget::legendMarkerClicked()
{
static QLegendMarker *m_lastClickedMarker = nullptr;
auto* marker = qobject_cast<QLegendMarker*>(sender());
Q_ASSERT(marker);
// Toggle visibility of series
setSeriesVisible(marker->series(), !marker->series()->isVisible());
if (m_lastClickedMarker
&& (m_lastClickedMarker->series()->isVisible() == marker->series()->isVisible())
&& (QApplication::keyboardModifiers() & Qt::ShiftModifier)) {
auto markers = m_chart->legend()->markers();
int startIndex = markers.indexOf(marker);
int lastIndex = markers.indexOf(m_lastClickedMarker);
if (startIndex != -1 && lastIndex != -1) {
if (startIndex > lastIndex) {
auto tmp = lastIndex;
lastIndex = startIndex;
startIndex = tmp;
}
for (int i = (startIndex + 1); i<lastIndex; i++) {
setSeriesVisible(markers[i]->series(), !markers[i]->series()->isVisible());
}
}
}
m_lastClickedMarker = marker;
connect(m_lastClickedMarker, &QLegendMarker::destroyed, this, [this]() {m_lastClickedMarker = nullptr;});
}
void ZoomableChartWidget::legendMarkerHovered(bool hover)
{
auto* marker = qobject_cast<QLegendMarker*>(sender());
Q_ASSERT(marker);
QFont font = marker->font();
font.setBold(hover);
marker->setFont(font);
if (marker->series()->type() == QAbstractSeries::SeriesTypeLine) {
auto series = qobject_cast<QLineSeries*>(marker->series());
auto pen = series->pen();
pen.setWidth(hover ? (pen.width() * 2) : (pen.width() / 2));
series->setPen(pen);
}
}
ZoomableChart *ZoomableChartWidget::chart() const
{
return m_chart;
}
ZoomableChartView *ZoomableChartWidget::chartView() const
{
return ui->chartView;
}
void ZoomableChartWidget::addToolWidget(QWidget *widget)
{
ui->horizontalLayoutToolbar->insertWidget(1, widget);
}
void ZoomableChartWidget::setSeriesVisible(QAbstractSeries *series, bool visible)
{
series->setVisible(visible);
const auto markers = m_chart->legend()->markers(series);
for (auto *marker : markers) {
// 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);
// Dim the marker, if series is not visible
qreal alpha = visible ? 1.0 : 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);
}
for (QAbstractAxis *axis : m_chart->axes(Qt::Vertical)) {
bool hideAxis = true;
for (auto *series : m_chart->series()) {
for (const auto *attachedAxis : series->attachedAxes()) {
if (series->isVisible() && attachedAxis == axis) {
hideAxis = false;
break;
}
}
if (!hideAxis)
break;
}
axis->setVisible(!hideAxis);
}
}
void ZoomableChartWidget::seriesAdded(QAbstractSeries *series)
{
// Connect all markers to handler
const auto markers = m_chart->legend()->markers(series);
for (auto marker : markers) {
QObject::connect(marker, &QLegendMarker::clicked,
this, &ZoomableChartWidget::legendMarkerClicked);
QObject::connect(marker, &QLegendMarker::hovered,
this, &ZoomableChartWidget::legendMarkerHovered);
}
}
void ZoomableChartWidget::seriesRemoved(QAbstractSeries *series)
{
// Connect all markers to handler
const auto markers = m_chart->legend()->markers(series);
for (auto marker : markers) {
QObject::disconnect(marker, &QLegendMarker::clicked,
this, &ZoomableChartWidget::legendMarkerClicked);
QObject::disconnect(marker, &QLegendMarker::hovered,
this, &ZoomableChartWidget::legendMarkerHovered);
}
}
void ZoomableChartWidget::on_toolButtonFitInView_clicked()
{
QHash<const QAbstractAxis*, qreal> xmin, xmax, ymin, ymax;
// loop on all axes (both vetival and horizontal)
for (const QAbstractAxis *axis : m_chart->axes()) {
// only QValueAxis supported at the moment
if (axis->type() != QAbstractAxis::AxisTypeValue)
continue;
// loop on all series attached to the axes and look for each min max point
for (auto *series : m_chart->series()) {
// at the moment only QLineSeries is supported for zoom to fit
if (series->type() != QAbstractSeries::SeriesTypeLine)
continue;
if (!xmin.contains(axis))
xmin[axis] = std::numeric_limits<qreal>::max();
if (!xmax.contains(axis))
xmax[axis] = -std::numeric_limits<qreal>::max();
if (!ymin.contains(axis))
ymin[axis] = std::numeric_limits<qreal>::max();
if (!ymax.contains(axis))
ymax[axis] = -std::numeric_limits<qreal>::max();
for (const auto *attachedAxis : series->attachedAxes()) {
if (series->isVisible() && attachedAxis == axis) {
auto lineSeries = static_cast<const QLineSeries*>(series);
for (const auto pt : lineSeries->points()) {
if (pt.x() < xmin[axis])
xmin[axis] = pt.x();
if (pt.x() > xmax[axis])
xmax[axis] = pt.x();
if (pt.y() < ymin[axis])
ymin[axis] = pt.y();
if (pt.y() > ymax[axis])
ymax[axis] = pt.y();
}
break;
}
}
}
}
for (QAbstractAxis *axis : m_chart->axes()) {
if (axis->type() != QAbstractAxis::AxisTypeValue)
continue;
if (axis->alignment() == Qt::Horizontal) {
if (xmin.contains(axis))
axis->setMin(xmin[axis]);
if (xmax.contains(axis))
axis->setMax(xmax[axis]);
} else if (axis->alignment() == Qt::Vertical) {
if (ymin.contains(axis))
axis->setMin(ymin[axis]);
if (ymax.contains(axis))
axis->setMax(ymax[axis]);
}
}
}