File indexing completed on 2024-05-05 03:46:04
0001 #include <QApplication> 0002 #include <QHBoxLayout> 0003 #include <QWidget> 0004 0005 #include <labplot/labplot.h> 0006 0007 int main(int argc, char **argv) { 0008 QApplication app (argc, argv); 0009 0010 auto* mainWidget = new QWidget(); 0011 auto* layout = new QHBoxLayout; 0012 mainWidget->setLayout(layout); 0013 0014 //create a worksheet and a plot 0015 auto* worksheet = new Worksheet("test"); 0016 0017 //create a plot 0018 auto* plot = new CartesianPlot("plot"); 0019 plot->setType(CartesianPlot::Type::FourAxes); 0020 worksheet->addChild(plot); 0021 0022 //Generate some data for f(x) = a*x^2 + b*x + c with additional noise 0023 int count = 10; 0024 double a = 1.27; 0025 double b = 4.375; 0026 double c = -6.692; 0027 0028 auto* x = new Column("x"); 0029 auto* y = new Column("y"); 0030 0031 for (int i = 0; i < count; ++i) { 0032 x->setValueAt(i, i); 0033 double rand_value = 10*double(qrand())/double(RAND_MAX); 0034 double value = a*pow(i, 2) + b*i + c + rand_value; 0035 y->setValueAt(i, value); 0036 } 0037 0038 auto* curve = new XYCurve("raw data"); 0039 curve->setXColumn(x); 0040 curve->setYColumn(y); 0041 // curve->setLineStyle(XYCurve::LineStyle::NoLine); 0042 // curve->symbol()->setStyle(Symbol::Circle); 0043 plot->addChild(curve); 0044 plot->autoScale(); 0045 0046 //perform a fit to the raw data and show it 0047 auto* fitCurve = new XYFitCurve("fit "); 0048 //TODO: 0049 fitCurve->recalculate(); 0050 plot->addChild(fitCurve); 0051 0052 //add a curve defined via a mathematical equation 0053 auto* eqCurve = new XYEquationCurve("eq"); 0054 plot->addChild(eqCurve); 0055 auto data = XYEquationCurve::EquationData(); 0056 data.expression1 = "50*sin(x)"; 0057 data.min = "0.0"; 0058 data.max = "10.0"; 0059 eqCurve->setEquationData(data); 0060 eqCurve->recalculate(); 0061 0062 //add legend 0063 plot->addLegend(); 0064 0065 //create another plot 0066 auto* plot2 = new CartesianPlot("plot 2"); 0067 plot2->setType(CartesianPlot::Type::FourAxes); 0068 worksheet->addChild(plot2); 0069 0070 //create some random data and plot a histogram for them 0071 auto* hist = new Histogram("histogram"); 0072 auto* random_data = new Column("x"); 0073 0074 for (int i = 0; i < 1000; ++i) 0075 random_data->setValueAt(i, double(100*qrand())/double(RAND_MAX)); 0076 0077 hist->setDataColumn(random_data); 0078 plot2->addChild(hist); 0079 0080 //create one more plot 0081 auto* plot3 = new CartesianPlot("plot 3"); 0082 plot3->setType(CartesianPlot::Type::FourAxes); 0083 worksheet->addChild(plot3); 0084 0085 //add box plot 0086 auto* boxPlot = new BoxPlot("boxplot"); 0087 QVector<const AbstractColumn*> columns{random_data}; 0088 boxPlot->setDataColumns(columns); 0089 plot3->addChild(boxPlot); 0090 0091 layout->addWidget(worksheet->view()); 0092 mainWidget->show(); 0093 0094 return app.exec(); 0095 }