-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomChartView.cpp
More file actions
86 lines (74 loc) · 3.02 KB
/
CustomChartView.cpp
File metadata and controls
86 lines (74 loc) · 3.02 KB
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
#include "stdafx.h"
#include "CustomChartView.h"
void CustomChartView::wheelEvent(QWheelEvent* event)
{
QChartView::wheelEvent(event);
// Ïîëó÷àåì òåêóùèå îñè ãðàôèêà
QValueAxis* axisX = qobject_cast<QValueAxis*>(chart()->axes(Qt::Horizontal).first());
QValueAxis* axisY = qobject_cast<QValueAxis*>(chart()->axes(Qt::Vertical).first());
if (axisX && axisY) {
// Èçìåíåíèå äèàïàçîíà îñåé ïðè ïðîêðóòêå êîëåñèêà ìûøè
if (event->angleDelta().y() > 0) { // Ïðè ïðîêðóòêå âïåðåä (ìàñøòàáèðóåì ââåðõ)
axisX->setRange(axisX->min() * 0.9, axisX->max() * 0.9);
axisY->setRange(axisY->min() * 0.9, axisY->max() * 0.9);
}
else { // Ïðè ïðîêðóòêå íàçàä (ìàñøòàáèðóåì âíèç)
axisX->setRange(axisX->min() * 1.1, axisX->max() * 1.1);
axisY->setRange(axisY->min() * 1.1, axisY->max() * 1.1);
}
}
}
void CustomChartView::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
m_isDragging = true;
m_dragStartPos = event->pos();
}
}
void CustomChartView::mouseMoveEvent(QMouseEvent* event)
{
if (m_isDragging) {
// Âû÷èñëÿåì ñìåùåíèå
int deltaX = event->pos().x() - m_dragStartPos.x();
int deltaY = event->pos().y() - m_dragStartPos.y();
// Èçìåíÿåì ïîçèöèþ ãðàôèêà
chart()->scroll(-deltaX, deltaY); // Ïåðåìåùåíèå ãðàôèêà âëåâî/âïðàâ
// Îáíîâëÿåì íà÷àëüíóþ ïîçèöèþ
m_dragStartPos = event->pos();
}
QPointF point = chart()->mapToValue(event->pos());
// Ïîëó÷àåì çíà÷åíèÿ ïî îñÿì X è Y
qreal x = point.x();
qreal yFood = 0;
qreal yAnimal = 0;
// Íàõîäèì çíà÷åíèÿ ïî Y äëÿ êàæäîé ñåðèè (ìîæíî äîðàáîòàòü äëÿ íàõîæäåíèÿ áëèæàéøèõ òî÷åê)
if (seriesCntrFood && seriesCntrFood->count() > 0) {
qreal minDistFood = std::numeric_limits<qreal>::max(); // Ìèíèìàëüíîå ðàññòîÿíèå
for (int i = 0; i < seriesCntrFood->count(); ++i) {
qreal dist = std::abs(seriesCntrFood->at(i).x() - x);
if (dist < minDistFood) {
minDistFood = dist;
yFood = seriesCntrFood->at(i).y(); // Çàïîìèíàåì çíà÷åíèå Y äëÿ áëèæàéøåé òî÷êè
}
}
}
// Ïîèñê áëèæàéøåé òî÷êè â ñåðèè animal
if (seriesCntrAnimal && seriesCntrAnimal->count() > 0) {
qreal minDistAnimal = std::numeric_limits<qreal>::max(); // Ìèíèìàëüíîå ðàññòîÿíèå
for (int i = 0; i < seriesCntrAnimal->count(); ++i) {
qreal dist = std::abs(seriesCntrAnimal->at(i).x() - x);
if (dist < minDistAnimal) {
minDistAnimal = dist;
yAnimal = seriesCntrAnimal->at(i).y(); // Çàïîìèíàåì çíà÷åíèå Y äëÿ áëèæàéøåé òî÷êè
}
}
}
QString tooltipText = QString("X: %1, Food: %2, Animal: %3").arg(x).arg(yFood).arg(yAnimal);
QToolTip::showText(event->globalPos(), tooltipText);
}
void CustomChartView::mouseReleaseEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
m_isDragging = false;
}
}