1/*
2 testplot_widget.cpp
3 SPDX-FileCopyrightText: 2006 Jason Harris <kstars@30doradus.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#include <QComboBox>
9#include <QPen>
10#include <QVBoxLayout>
11#include <math.h>
12
13#include "kplotaxis.h"
14#include "kplotobject.h"
15#include "kplotwidget.h"
16#include "testplot_widget.h"
17
18TestPlot::TestPlot(QWidget *p)
19 : QMainWindow(p)
20 , po1(nullptr)
21 , po2(nullptr)
22{
23 QWidget *w = new QWidget(this);
24 vlay = new QVBoxLayout(w);
25
26 PlotSelector = new QComboBox(w);
27 PlotSelector->addItem(QStringLiteral("Points plot"));
28 PlotSelector->addItem(QStringLiteral("Lines plot"));
29 PlotSelector->addItem(QStringLiteral("Bars plot"));
30 PlotSelector->addItem(QStringLiteral("Points plot with labels"));
31 PlotSelector->addItem(QStringLiteral("Points, lines and bars"));
32 PlotSelector->addItem(QStringLiteral("Points, lines and bars with labels"));
33
34 plot = new KPlotWidget(w);
35 plot->setMinimumSize(minw: 400, minh: 400);
36 plot->setAntialiasing(true);
37 vlay->addWidget(PlotSelector);
38 vlay->addWidget(plot);
39
40 setCentralWidget(w);
41
42 connect(sender: PlotSelector, signal: qOverload<int>(&QComboBox::activated), context: this, slot: &TestPlot::slotSelectPlot);
43
44 slotSelectPlot(index: PlotSelector->currentIndex());
45}
46
47void TestPlot::slotSelectPlot(int n)
48{
49 plot->resetPlot();
50
51 switch (n) {
52 case 0: { // Points plot
53 plot->setLimits(x1: -6.0, x2: 11.0, y1: -10.0, y2: 110.0);
54
55 po1 = new KPlotObject(Qt::white, KPlotObject::Points, 4, KPlotObject::Asterisk);
56 po2 = new KPlotObject(Qt::green, KPlotObject::Points, 4, KPlotObject::Triangle);
57
58 for (float x = -5.0; x <= 10.0; x += 1.0) {
59 po1->addPoint(x, y: x * x);
60 po2->addPoint(x, y: 50.0 - 5.0 * x);
61 }
62
63 plot->addPlotObject(object: po1);
64 plot->addPlotObject(object: po2);
65
66 plot->update();
67 break;
68 }
69
70 case 1: { // Lines plot
71 plot->setLimits(x1: -0.1, x2: 6.38, y1: -1.1, y2: 1.1);
72 plot->setSecondaryLimits(x1: -5.73, x2: 365.55, y1: -1.1, y2: 1.1);
73 plot->axis(type: KPlotWidget::TopAxis)->setTickLabelsShown(true);
74 plot->axis(type: KPlotWidget::BottomAxis)->setLabel(QStringLiteral("Angle [radians]"));
75 plot->axis(type: KPlotWidget::TopAxis)->setLabel(QStringLiteral("Angle [degrees]"));
76
77 po1 = new KPlotObject(Qt::red, KPlotObject::Lines, 2);
78 po2 = new KPlotObject(Qt::cyan, KPlotObject::Lines, 2);
79
80 for (float t = 0.0; t <= 6.28; t += 0.04) {
81 po1->addPoint(x: t, y: sin(x: t));
82 po2->addPoint(x: t, y: cos(x: t));
83 }
84
85 plot->addPlotObject(object: po1);
86 plot->addPlotObject(object: po2);
87
88 plot->update();
89 break;
90 }
91
92 case 2: { // Bars plot
93 plot->setLimits(x1: -7.0, x2: 7.0, y1: -5.0, y2: 105.0);
94
95 po1 = new KPlotObject(Qt::white, KPlotObject::Bars, 2);
96 po1->setBarBrush(QBrush(Qt::green, Qt::Dense4Pattern));
97
98 for (float x = -6.5; x <= 6.5; x += 0.5) {
99 po1->addPoint(x, y: 100 * exp(x: -0.5 * x * x), label: QLatin1String(""), barWidth: 0.5);
100 }
101
102 plot->addPlotObject(object: po1);
103
104 plot->update();
105 break;
106 }
107
108 case 3: { // Points plot with labels
109 plot->setLimits(x1: -1.1, x2: 1.1, y1: -1.1, y2: 1.1);
110
111 po1 = new KPlotObject(Qt::yellow, KPlotObject::Points, 10, KPlotObject::Star);
112 po1->setLabelPen(QPen(Qt::green));
113
114 po1->addPoint(x: 0.0, y: 0.8, QStringLiteral("North"));
115 po1->addPoint(x: 0.57, y: 0.57, QStringLiteral("Northeast"));
116 po1->addPoint(x: 0.8, y: 0.0, QStringLiteral("East"));
117 po1->addPoint(x: 0.57, y: -0.57, QStringLiteral("Southeast"));
118 po1->addPoint(x: 0.0, y: -0.8, QStringLiteral("South"));
119 po1->addPoint(x: -0.57, y: -0.57, QStringLiteral("Southwest"));
120 po1->addPoint(x: -0.8, y: 0.0, QStringLiteral("West"));
121 po1->addPoint(x: -0.57, y: 0.57, QStringLiteral("Northwest"));
122
123 plot->addPlotObject(object: po1);
124
125 plot->update();
126 break;
127 }
128
129 case 4: { // Points, Lines and Bars plot
130 plot->setLimits(x1: -2.1, x2: 2.1, y1: -0.1, y2: 4.1);
131
132 po1 = new KPlotObject(Qt::white, KPlotObject::Points, 10, KPlotObject::Pentagon);
133
134 po1->setShowLines(true);
135 po1->setShowBars(true);
136 po1->setLabelPen(QPen(QColor("#AA8800")));
137 po1->setLinePen(QPen(Qt::red, 3.0, Qt::DashDotLine));
138 po1->setBarBrush(QBrush(Qt::blue, Qt::BDiagPattern));
139
140 po1->addPoint(x: -1.75, y: 0.5);
141 po1->addPoint(x: -1.25, y: 1.0);
142 po1->addPoint(x: -0.75, y: 1.25);
143 po1->addPoint(x: -0.25, y: 1.5);
144 po1->addPoint(x: 0.25, y: 2.5);
145 po1->addPoint(x: 0.75, y: 3.0);
146 po1->addPoint(x: 1.25, y: 1.5);
147 po1->addPoint(x: 1.75, y: 1.75);
148
149 plot->addPlotObject(object: po1);
150
151 update();
152 break;
153 }
154
155 case 5: { // Points, Lines and Bars plot with labels
156 plot->setLimits(x1: -2.1, x2: 2.1, y1: -0.1, y2: 4.1);
157
158 po1 = new KPlotObject(Qt::white, KPlotObject::Points, 10, KPlotObject::Pentagon);
159
160 po1->setShowLines(true);
161 po1->setShowBars(true);
162 po1->setLabelPen(QPen(QColor("#AA8800")));
163 po1->setLinePen(QPen(Qt::red, 3.0, Qt::DashDotLine));
164 po1->setBarBrush(QBrush(Qt::blue, Qt::BDiagPattern));
165
166 po1->addPoint(x: -1.75, y: 0.5, QStringLiteral("A"));
167 po1->addPoint(x: -1.25, y: 1.0, QStringLiteral("B"));
168 po1->addPoint(x: -0.75, y: 1.25, QStringLiteral("C"));
169 po1->addPoint(x: -0.25, y: 1.5, QStringLiteral("D"));
170 po1->addPoint(x: 0.25, y: 2.5, QStringLiteral("E"));
171 po1->addPoint(x: 0.75, y: 3.0, QStringLiteral("F"));
172 po1->addPoint(x: 1.25, y: 1.5, QStringLiteral("G"));
173 po1->addPoint(x: 1.75, y: 1.75, QStringLiteral("H"));
174
175 plot->addPlotObject(object: po1);
176
177 update();
178 break;
179 }
180 }
181}
182
183#include "moc_testplot_widget.cpp"
184

source code of kplotting/examples/testplot_widget.cpp