File indexing completed on 2024-04-28 03:48:06

0001 /*
0002     File                 : BarPlotTest.cpp
0003     Project              : LabPlot
0004     Description          : Tests for BarPlot
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2022-2023 Alexander Semke <alexander.semke@web.de>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "BarPlotTest.h"
0012 #include "backend/core/Project.h"
0013 #include "backend/core/column/Column.h"
0014 #include "backend/worksheet/Worksheet.h"
0015 #include "backend/worksheet/plots/cartesian/BarPlot.h"
0016 #include "backend/worksheet/plots/cartesian/LollipopPlot.h"
0017 
0018 /*!
0019  * \brief one dataset, grouped
0020  */
0021 void BarPlotTest::testRange01() {
0022     Project project;
0023     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0024     project.addChild(ws);
0025 
0026     auto* p = new CartesianPlot(QStringLiteral("plot"));
0027     ws->addChild(p);
0028 
0029     auto* barPlot = new BarPlot(QStringLiteral("barplot"));
0030     p->addChild(barPlot);
0031 
0032     // set the data
0033     QVector<const AbstractColumn*> dataColumns;
0034     auto* c = new Column(QStringLiteral("data"));
0035     c->setValueAt(0, 3.);
0036     c->setValueAt(1, 6.);
0037     c->setValueAt(2, 9.);
0038     c->setValueAt(3, 12.);
0039     dataColumns << c;
0040 
0041     barPlot->setDataColumns(dataColumns);
0042 
0043     // check the min and max range values
0044     QCOMPARE(barPlot->minimum(Dimension::X), 0.0);
0045     QCOMPARE(barPlot->maximum(Dimension::X), 4.0);
0046     QCOMPARE(barPlot->minimum(Dimension::Y), 0.);
0047     QCOMPARE(barPlot->maximum(Dimension::Y), 12.);
0048 }
0049 
0050 /*!
0051  * \brief one dataset, grouped, with a negative value
0052  */
0053 void BarPlotTest::testRange02() {
0054     Project project;
0055     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0056     project.addChild(ws);
0057 
0058     auto* p = new CartesianPlot(QStringLiteral("plot"));
0059     ws->addChild(p);
0060 
0061     auto* barPlot = new BarPlot(QStringLiteral("barplot"));
0062     p->addChild(barPlot);
0063 
0064     // set the data
0065     QVector<const AbstractColumn*> dataColumns;
0066     auto* c = new Column(QStringLiteral("data"));
0067     c->setValueAt(0, 3.);
0068     c->setValueAt(1, -6.);
0069     c->setValueAt(2, 9.);
0070     c->setValueAt(3, 12.);
0071     dataColumns << c;
0072 
0073     barPlot->setDataColumns(dataColumns);
0074 
0075     // check the min and max range values
0076     QCOMPARE(barPlot->minimum(Dimension::X), 0.0);
0077     QCOMPARE(barPlot->maximum(Dimension::X), 4.0);
0078     QCOMPARE(barPlot->minimum(Dimension::Y), -6.);
0079     QCOMPARE(barPlot->maximum(Dimension::Y), 12.);
0080 }
0081 
0082 /*!
0083  * \brief two datasets, stacked
0084  */
0085 void BarPlotTest::testRange03() {
0086     Project project;
0087     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0088     project.addChild(ws);
0089 
0090     auto* p = new CartesianPlot(QStringLiteral("plot"));
0091     ws->addChild(p);
0092 
0093     auto* barPlot = new BarPlot(QStringLiteral("barplot"));
0094     barPlot->setType(BarPlot::Type::Stacked);
0095     p->addChild(barPlot);
0096 
0097     // set the data
0098     QVector<const AbstractColumn*> dataColumns;
0099 
0100     auto* c1 = new Column(QStringLiteral("data1"));
0101     c1->setValueAt(0, 3.);
0102     c1->setValueAt(1, 6.);
0103     c1->setValueAt(2, 9.);
0104     c1->setValueAt(3, 12.);
0105     dataColumns << c1;
0106 
0107     auto* c2 = new Column(QStringLiteral("data2"));
0108     c2->setValueAt(0, 2.);
0109     c2->setValueAt(1, 5.);
0110     c2->setValueAt(2, 8.);
0111     c2->setValueAt(3, 11.);
0112     dataColumns << c2;
0113 
0114     barPlot->setDataColumns(dataColumns);
0115 
0116     // check the min and max range values
0117     QCOMPARE(barPlot->minimum(Dimension::X), 0.);
0118     QCOMPARE(barPlot->maximum(Dimension::X), 4.);
0119     QCOMPARE(barPlot->minimum(Dimension::Y), 0.);
0120     QCOMPARE(barPlot->maximum(Dimension::Y), 23.);
0121 }
0122 
0123 /*!
0124  * \brief two datasets, stacked, with a negative value
0125  */
0126 void BarPlotTest::testRange04() {
0127     Project project;
0128     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0129     project.addChild(ws);
0130 
0131     auto* p = new CartesianPlot(QStringLiteral("plot"));
0132     ws->addChild(p);
0133 
0134     auto* barPlot = new BarPlot(QStringLiteral("barplot"));
0135     barPlot->setType(BarPlot::Type::Stacked);
0136     p->addChild(barPlot);
0137 
0138     // set the data
0139     QVector<const AbstractColumn*> dataColumns;
0140 
0141     auto* c1 = new Column(QStringLiteral("data1"));
0142     c1->setValueAt(0, 3.);
0143     c1->setValueAt(1, 6.);
0144     c1->setValueAt(2, 9.);
0145     c1->setValueAt(3, 12.);
0146     dataColumns << c1;
0147 
0148     auto* c2 = new Column(QStringLiteral("data2"));
0149     c2->setValueAt(0, 2.);
0150     c2->setValueAt(1, 5.);
0151     c2->setValueAt(2, 8.);
0152     c2->setValueAt(3, -11.);
0153     dataColumns << c2;
0154 
0155     barPlot->setDataColumns(dataColumns);
0156 
0157     // check the min and max range values
0158     QCOMPARE(barPlot->minimum(Dimension::X), 0.0);
0159     QCOMPARE(barPlot->maximum(Dimension::X), 4.0);
0160     QCOMPARE(barPlot->minimum(Dimension::Y), -11.);
0161     QCOMPARE(barPlot->maximum(Dimension::Y), 17.);
0162 }
0163 
0164 /*!
0165  * \brief two datasets, stacked 100%
0166  */
0167 void BarPlotTest::testRange05() {
0168     Project project;
0169     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0170     project.addChild(ws);
0171 
0172     auto* p = new CartesianPlot(QStringLiteral("plot"));
0173     ws->addChild(p);
0174 
0175     auto* barPlot = new BarPlot(QStringLiteral("barplot"));
0176     barPlot->setType(BarPlot::Type::Stacked_100_Percent);
0177     p->addChild(barPlot);
0178 
0179     // set the data
0180     QVector<const AbstractColumn*> dataColumns;
0181 
0182     auto* c1 = new Column(QStringLiteral("data1"));
0183     c1->setValueAt(0, 3.);
0184     c1->setValueAt(1, 6.);
0185     c1->setValueAt(2, 9.);
0186     c1->setValueAt(3, 12.);
0187     dataColumns << c1;
0188 
0189     auto* c2 = new Column(QStringLiteral("data2"));
0190     c2->setValueAt(0, 2.);
0191     c2->setValueAt(1, 5.);
0192     c2->setValueAt(2, 8.);
0193     c2->setValueAt(3, 11.);
0194     dataColumns << c2;
0195 
0196     barPlot->setDataColumns(dataColumns);
0197 
0198     // check the min and max range values
0199     QCOMPARE(barPlot->minimum(Dimension::X), 0.);
0200     QCOMPARE(barPlot->maximum(Dimension::X), 4.);
0201     QCOMPARE(barPlot->minimum(Dimension::Y), 0.);
0202     QCOMPARE(barPlot->maximum(Dimension::Y), 100.);
0203 }
0204 
0205 /*!
0206  * \brief lollipop plot wiht one dataset, horizontal
0207  */
0208 void BarPlotTest::testRangeLollipopPlot01() {
0209     Project project;
0210     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0211     project.addChild(ws);
0212 
0213     auto* p = new CartesianPlot(QStringLiteral("plot"));
0214     ws->addChild(p);
0215 
0216     auto* barPlot = new LollipopPlot(QStringLiteral("barplot"));
0217     p->addChild(barPlot);
0218 
0219     // set the data
0220     QVector<const AbstractColumn*> dataColumns;
0221     auto* c = new Column(QStringLiteral("data"));
0222     c->setValueAt(0, 3.);
0223     c->setValueAt(1, 6.);
0224     c->setValueAt(2, 9.);
0225     c->setValueAt(3, 12.);
0226     dataColumns << c;
0227 
0228     barPlot->setDataColumns(dataColumns);
0229 
0230     // check the min and max range values
0231     QCOMPARE(barPlot->minimum(Dimension::X), 0.0);
0232     QCOMPARE(barPlot->maximum(Dimension::X), 4.0);
0233     QCOMPARE(barPlot->minimum(Dimension::Y), 0.);
0234     QCOMPARE(barPlot->maximum(Dimension::Y), 12.);
0235 }
0236 
0237 /*!
0238  * \brief lollipop plot wiht one dataset, vertical
0239  */
0240 void BarPlotTest::testRangeLollipopPlot02() {
0241     Project project;
0242     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0243     project.addChild(ws);
0244 
0245     auto* p = new CartesianPlot(QStringLiteral("plot"));
0246     ws->addChild(p);
0247 
0248     auto* barPlot = new LollipopPlot(QStringLiteral("barplot"));
0249     barPlot->setOrientation(LollipopPlot::Orientation::Horizontal);
0250     p->addChild(barPlot);
0251 
0252     // set the data
0253     QVector<const AbstractColumn*> dataColumns;
0254     auto* c = new Column(QStringLiteral("data"));
0255     c->setValueAt(0, 3.);
0256     c->setValueAt(1, 6.);
0257     c->setValueAt(2, 9.);
0258     c->setValueAt(3, 12.);
0259     dataColumns << c;
0260 
0261     barPlot->setDataColumns(dataColumns);
0262 
0263     // check the min and max range values
0264     QCOMPARE(barPlot->minimum(Dimension::X), 0.0);
0265     QCOMPARE(barPlot->maximum(Dimension::X), 12.0);
0266     QCOMPARE(barPlot->minimum(Dimension::Y), 0.);
0267     QCOMPARE(barPlot->maximum(Dimension::Y), 4.0);
0268 }
0269 
0270 QTEST_MAIN(BarPlotTest)