Skip to content

Commit 558d299

Browse files
authored
Initial commit
0 parents  commit 558d299

11 files changed

+2667
-0
lines changed

axistag.cpp

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/***************************************************************************
2+
** **
3+
** QCustomPlot, an easy to use, modern plotting widget for Qt **
4+
** Copyright (C) 2011-2018 Emanuel Eichhammer **
5+
** **
6+
** This program is free software: you can redistribute it and/or modify **
7+
** it under the terms of the GNU General Public License as published by **
8+
** the Free Software Foundation, either version 3 of the License, or **
9+
** (at your option) any later version. **
10+
** **
11+
** This program is distributed in the hope that it will be useful, **
12+
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
13+
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
14+
** GNU General Public License for more details. **
15+
** **
16+
** You should have received a copy of the GNU General Public License **
17+
** along with this program. If not, see http://www.gnu.org/licenses/. **
18+
** **
19+
****************************************************************************
20+
** Author: Emanuel Eichhammer **
21+
** Website/Contact: http://www.qcustomplot.com/ **
22+
** Date: 25.06.18 **
23+
** Version: 2.0.1 **
24+
****************************************************************************/
25+
26+
#include "axistag.h"
27+
28+
AxisTag::AxisTag(QCPAxis *parentAxis) :
29+
QObject(parentAxis),
30+
mAxis(parentAxis)
31+
{
32+
// The dummy tracer serves here as an invisible anchor which always sticks to the right side of
33+
// the axis rect
34+
mDummyTracer = new QCPItemTracer(mAxis->parentPlot());
35+
mDummyTracer->setVisible(false);
36+
mDummyTracer->position->setTypeX(QCPItemPosition::ptAxisRectRatio);
37+
mDummyTracer->position->setTypeY(QCPItemPosition::ptPlotCoords);
38+
mDummyTracer->position->setAxisRect(mAxis->axisRect());
39+
mDummyTracer->position->setAxes(0, mAxis);
40+
mDummyTracer->position->setCoords(1, 0);
41+
42+
// the arrow end (head) is set to move along with the dummy tracer by setting it as its parent
43+
// anchor. Its coordinate system (setCoords) is thus pixels, and this is how the needed horizontal
44+
// offset for the tag of the second y axis is achieved. This horizontal offset gets dynamically
45+
// updated in AxisTag::updatePosition. the arrow "start" is simply set to have the "end" as parent
46+
// anchor. It is given a horizontal offset to the right, which results in a 15 pixel long arrow.
47+
mArrow = new QCPItemLine(mAxis->parentPlot());
48+
mArrow->setLayer("overlay");
49+
mArrow->setClipToAxisRect(false);
50+
mArrow->setHead(QCPLineEnding::esSpikeArrow);
51+
mArrow->end->setParentAnchor(mDummyTracer->position);
52+
mArrow->start->setParentAnchor(mArrow->end);
53+
mArrow->start->setCoords(15, 0);
54+
55+
// The text label is anchored at the arrow start (tail) and has its "position" aligned at the
56+
// left, and vertically centered to the text label box.
57+
mLabel = new QCPItemText(mAxis->parentPlot());
58+
mLabel->setLayer("overlay");
59+
mLabel->setClipToAxisRect(false);
60+
mLabel->setPadding(QMargins(3, 0, 3, 0));
61+
mLabel->setBrush(QBrush(Qt::white));
62+
mLabel->setPen(QPen(Qt::blue));
63+
mLabel->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter);
64+
mLabel->position->setParentAnchor(mArrow->start);
65+
}
66+
67+
AxisTag::~AxisTag()
68+
{
69+
if (mDummyTracer)
70+
mDummyTracer->parentPlot()->removeItem(mDummyTracer);
71+
if (mArrow)
72+
mArrow->parentPlot()->removeItem(mArrow);
73+
if (mLabel)
74+
mLabel->parentPlot()->removeItem(mLabel);
75+
}
76+
77+
void AxisTag::setPen(const QPen &pen)
78+
{
79+
mArrow->setPen(pen);
80+
mLabel->setPen(pen);
81+
}
82+
83+
void AxisTag::setBrush(const QBrush &brush)
84+
{
85+
mLabel->setBrush(brush);
86+
}
87+
88+
void AxisTag::setText(const QString &text)
89+
{
90+
mLabel->setText(text);
91+
}
92+
93+
void AxisTag::updatePosition(double value)
94+
{
95+
// since both the arrow and the text label are chained to the dummy tracer (via anchor
96+
// parent-child relationships) it is sufficient to update the dummy tracer coordinates. The
97+
// Horizontal coordinate type was set to ptAxisRectRatio so to keep it aligned at the right side
98+
// of the axis rect, it is always kept at 1. The vertical coordinate type was set to
99+
// ptPlotCoordinates of the passed parent axis, so the vertical coordinate is set to the new
100+
// value.
101+
mDummyTracer->position->setCoords(1, value);
102+
103+
// We want the arrow head to be at the same horizontal position as the axis backbone, even if
104+
// the axis has a certain offset from the axis rect border (like the added second y axis). Thus we
105+
// set the horizontal pixel position of the arrow end (head) to the axis offset (the pixel
106+
// distance to the axis rect border). This works because the parent anchor of the arrow end is
107+
// the dummy tracer, which, as described earlier, is tied to the right axis rect border.
108+
mArrow->end->setCoords(mAxis->offset(), 0);
109+
}

axistag.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/***************************************************************************
2+
** **
3+
** QCustomPlot, an easy to use, modern plotting widget for Qt **
4+
** Copyright (C) 2011-2018 Emanuel Eichhammer **
5+
** **
6+
** This program is free software: you can redistribute it and/or modify **
7+
** it under the terms of the GNU General Public License as published by **
8+
** the Free Software Foundation, either version 3 of the License, or **
9+
** (at your option) any later version. **
10+
** **
11+
** This program is distributed in the hope that it will be useful, **
12+
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
13+
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
14+
** GNU General Public License for more details. **
15+
** **
16+
** You should have received a copy of the GNU General Public License **
17+
** along with this program. If not, see http://www.gnu.org/licenses/. **
18+
** **
19+
****************************************************************************
20+
** Author: Emanuel Eichhammer **
21+
** Website/Contact: http://www.qcustomplot.com/ **
22+
** Date: 25.06.18 **
23+
** Version: 2.0.1 **
24+
****************************************************************************/
25+
26+
#ifndef AXISTAG_H
27+
#define AXISTAG_H
28+
29+
#include <QObject>
30+
#include "../../qcustomplot.h"
31+
32+
class AxisTag : public QObject
33+
{
34+
Q_OBJECT
35+
public:
36+
explicit AxisTag(QCPAxis *parentAxis);
37+
virtual ~AxisTag();
38+
39+
// setters:
40+
void setPen(const QPen &pen);
41+
void setBrush(const QBrush &brush);
42+
void setText(const QString &text);
43+
44+
// getters:
45+
QPen pen() const { return mLabel->pen(); }
46+
QBrush brush() const { return mLabel->brush(); }
47+
QString text() const { return mLabel->text(); }
48+
49+
// other methods:
50+
void updatePosition(double value);
51+
52+
protected:
53+
QCPAxis *mAxis;
54+
QPointer<QCPItemTracer> mDummyTracer;
55+
QPointer<QCPItemLine> mArrow;
56+
QPointer<QCPItemText> mLabel;
57+
};
58+
59+
60+
#endif // AXISTAG_H

balboa.jpg

36.8 KB
Loading

images.qrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>balboa.jpg</file>
4+
<file>solarpanels.jpg</file>
5+
<file>sun.png</file>
6+
</qresource>
7+
</RCC>

main.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/***************************************************************************
2+
** **
3+
** QCustomPlot, an easy to use, modern plotting widget for Qt **
4+
** Copyright (C) 2011-2018 Emanuel Eichhammer **
5+
** **
6+
** This program is free software: you can redistribute it and/or modify **
7+
** it under the terms of the GNU General Public License as published by **
8+
** the Free Software Foundation, either version 3 of the License, or **
9+
** (at your option) any later version. **
10+
** **
11+
** This program is distributed in the hope that it will be useful, **
12+
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
13+
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
14+
** GNU General Public License for more details. **
15+
** **
16+
** You should have received a copy of the GNU General Public License **
17+
** along with this program. If not, see http://www.gnu.org/licenses/. **
18+
** **
19+
****************************************************************************
20+
** Author: Emanuel Eichhammer **
21+
** Website/Contact: http://www.qcustomplot.com/ **
22+
** Date: 25.06.18 **
23+
** Version: 2.0.1 **
24+
****************************************************************************/
25+
26+
#include <QApplication>
27+
#include "mainwindow.h"
28+
29+
int main(int argc, char *argv[])
30+
{
31+
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
32+
QApplication::setGraphicsSystem("raster");
33+
#endif
34+
QApplication a(argc, argv);
35+
MainWindow w;
36+
w.show();
37+
38+
return a.exec();
39+
}

0 commit comments

Comments
 (0)