File indexing completed on 2024-04-28 15:29:29

0001 /*
0002     testplot_widget.cpp
0003     SPDX-FileCopyrightText: 2006 Jason Harris <kstars@30doradus.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include <QComboBox>
0009 #include <QPen>
0010 #include <QVBoxLayout>
0011 #include <math.h>
0012 
0013 #include "kplotaxis.h"
0014 #include "kplotobject.h"
0015 #include "kplotwidget.h"
0016 #include "testplot_widget.h"
0017 
0018 TestPlot::TestPlot(QWidget *p)
0019     : QMainWindow(p)
0020     , po1(nullptr)
0021     , po2(nullptr)
0022 {
0023     QWidget *w = new QWidget(this);
0024     vlay = new QVBoxLayout(w);
0025 
0026     PlotSelector = new QComboBox(w);
0027     PlotSelector->addItem(QStringLiteral("Points plot"));
0028     PlotSelector->addItem(QStringLiteral("Lines plot"));
0029     PlotSelector->addItem(QStringLiteral("Bars plot"));
0030     PlotSelector->addItem(QStringLiteral("Points plot with labels"));
0031     PlotSelector->addItem(QStringLiteral("Points, lines and bars"));
0032     PlotSelector->addItem(QStringLiteral("Points, lines and bars with labels"));
0033 
0034     plot = new KPlotWidget(w);
0035     plot->setMinimumSize(400, 400);
0036     plot->setAntialiasing(true);
0037     vlay->addWidget(PlotSelector);
0038     vlay->addWidget(plot);
0039 
0040     setCentralWidget(w);
0041 
0042     connect(PlotSelector, qOverload<int>(&QComboBox::activated), this, &TestPlot::slotSelectPlot);
0043 
0044     slotSelectPlot(PlotSelector->currentIndex());
0045 }
0046 
0047 void TestPlot::slotSelectPlot(int n)
0048 {
0049     plot->resetPlot();
0050 
0051     switch (n) {
0052     case 0: { // Points plot
0053         plot->setLimits(-6.0, 11.0, -10.0, 110.0);
0054 
0055         po1 = new KPlotObject(Qt::white, KPlotObject::Points, 4, KPlotObject::Asterisk);
0056         po2 = new KPlotObject(Qt::green, KPlotObject::Points, 4, KPlotObject::Triangle);
0057 
0058         for (float x = -5.0; x <= 10.0; x += 1.0) {
0059             po1->addPoint(x, x * x);
0060             po2->addPoint(x, 50.0 - 5.0 * x);
0061         }
0062 
0063         plot->addPlotObject(po1);
0064         plot->addPlotObject(po2);
0065 
0066         plot->update();
0067         break;
0068     }
0069 
0070     case 1: { // Lines plot
0071         plot->setLimits(-0.1, 6.38, -1.1, 1.1);
0072         plot->setSecondaryLimits(-5.73, 365.55, -1.1, 1.1);
0073         plot->axis(KPlotWidget::TopAxis)->setTickLabelsShown(true);
0074         plot->axis(KPlotWidget::BottomAxis)->setLabel(QStringLiteral("Angle [radians]"));
0075         plot->axis(KPlotWidget::TopAxis)->setLabel(QStringLiteral("Angle [degrees]"));
0076 
0077         po1 = new KPlotObject(Qt::red, KPlotObject::Lines, 2);
0078         po2 = new KPlotObject(Qt::cyan, KPlotObject::Lines, 2);
0079 
0080         for (float t = 0.0; t <= 6.28; t += 0.04) {
0081             po1->addPoint(t, sin(t));
0082             po2->addPoint(t, cos(t));
0083         }
0084 
0085         plot->addPlotObject(po1);
0086         plot->addPlotObject(po2);
0087 
0088         plot->update();
0089         break;
0090     }
0091 
0092     case 2: { // Bars plot
0093         plot->setLimits(-7.0, 7.0, -5.0, 105.0);
0094 
0095         po1 = new KPlotObject(Qt::white, KPlotObject::Bars, 2);
0096         po1->setBarBrush(QBrush(Qt::green, Qt::Dense4Pattern));
0097 
0098         for (float x = -6.5; x <= 6.5; x += 0.5) {
0099             po1->addPoint(x, 100 * exp(-0.5 * x * x), QLatin1String(""), 0.5);
0100         }
0101 
0102         plot->addPlotObject(po1);
0103 
0104         plot->update();
0105         break;
0106     }
0107 
0108     case 3: { // Points plot with labels
0109         plot->setLimits(-1.1, 1.1, -1.1, 1.1);
0110 
0111         po1 = new KPlotObject(Qt::yellow, KPlotObject::Points, 10, KPlotObject::Star);
0112         po1->setLabelPen(QPen(Qt::green));
0113 
0114         po1->addPoint(0.0, 0.8, QStringLiteral("North"));
0115         po1->addPoint(0.57, 0.57, QStringLiteral("Northeast"));
0116         po1->addPoint(0.8, 0.0, QStringLiteral("East"));
0117         po1->addPoint(0.57, -0.57, QStringLiteral("Southeast"));
0118         po1->addPoint(0.0, -0.8, QStringLiteral("South"));
0119         po1->addPoint(-0.57, -0.57, QStringLiteral("Southwest"));
0120         po1->addPoint(-0.8, 0.0, QStringLiteral("West"));
0121         po1->addPoint(-0.57, 0.57, QStringLiteral("Northwest"));
0122 
0123         plot->addPlotObject(po1);
0124 
0125         plot->update();
0126         break;
0127     }
0128 
0129     case 4: { // Points, Lines and Bars plot
0130         plot->setLimits(-2.1, 2.1, -0.1, 4.1);
0131 
0132         po1 = new KPlotObject(Qt::white, KPlotObject::Points, 10, KPlotObject::Pentagon);
0133 
0134         po1->setShowLines(true);
0135         po1->setShowBars(true);
0136         po1->setLabelPen(QPen(QColor("#AA8800")));
0137         po1->setLinePen(QPen(Qt::red, 3.0, Qt::DashDotLine));
0138         po1->setBarBrush(QBrush(Qt::blue, Qt::BDiagPattern));
0139 
0140         po1->addPoint(-1.75, 0.5);
0141         po1->addPoint(-1.25, 1.0);
0142         po1->addPoint(-0.75, 1.25);
0143         po1->addPoint(-0.25, 1.5);
0144         po1->addPoint(0.25, 2.5);
0145         po1->addPoint(0.75, 3.0);
0146         po1->addPoint(1.25, 1.5);
0147         po1->addPoint(1.75, 1.75);
0148 
0149         plot->addPlotObject(po1);
0150 
0151         update();
0152         break;
0153     }
0154 
0155     case 5: { // Points, Lines and Bars plot with labels
0156         plot->setLimits(-2.1, 2.1, -0.1, 4.1);
0157 
0158         po1 = new KPlotObject(Qt::white, KPlotObject::Points, 10, KPlotObject::Pentagon);
0159 
0160         po1->setShowLines(true);
0161         po1->setShowBars(true);
0162         po1->setLabelPen(QPen(QColor("#AA8800")));
0163         po1->setLinePen(QPen(Qt::red, 3.0, Qt::DashDotLine));
0164         po1->setBarBrush(QBrush(Qt::blue, Qt::BDiagPattern));
0165 
0166         po1->addPoint(-1.75, 0.5, QStringLiteral("A"));
0167         po1->addPoint(-1.25, 1.0, QStringLiteral("B"));
0168         po1->addPoint(-0.75, 1.25, QStringLiteral("C"));
0169         po1->addPoint(-0.25, 1.5, QStringLiteral("D"));
0170         po1->addPoint(0.25, 2.5, QStringLiteral("E"));
0171         po1->addPoint(0.75, 3.0, QStringLiteral("F"));
0172         po1->addPoint(1.25, 1.5, QStringLiteral("G"));
0173         po1->addPoint(1.75, 1.75, QStringLiteral("H"));
0174 
0175         plot->addPlotObject(po1);
0176 
0177         update();
0178         break;
0179     }
0180     }
0181 }
0182 
0183 #include "moc_testplot_widget.cpp"