2019-01-13 01:53:16 +08:00
|
|
|
/** \example jkqtplotter_simpletest_parsedfunctionplot.cpp
|
2019-01-20 23:15:10 +08:00
|
|
|
* JKQTPlotter: Examples: Plotting Parsed Mathematical Functions as Line Graphs
|
2019-01-13 01:53:16 +08:00
|
|
|
*
|
2019-01-20 23:15:10 +08:00
|
|
|
* \ref JKQTPlotterParsedFunctionPlot
|
2019-01-13 01:53:16 +08:00
|
|
|
*/
|
|
|
|
|
2018-12-24 03:27:24 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QLineEdit>
|
2018-12-24 19:29:33 +08:00
|
|
|
#include <QCheckBox>
|
2018-12-24 22:07:14 +08:00
|
|
|
#include <QFormLayout>
|
|
|
|
#include <QDoubleSpinBox>
|
2018-12-24 03:27:24 +08:00
|
|
|
#include "jkqtplotter/jkqtplotter.h"
|
2019-06-20 22:06:31 +08:00
|
|
|
#include "jkqtplotter/graphs/jkqtpscatter.h"
|
|
|
|
#include "jkqtplotter/graphs/jkqtpparsedfunction.h"
|
2018-12-24 03:27:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
|
|
|
|
|
|
|
// 1. create a window that conatins a line-edit to edit a function
|
2019-01-20 23:15:10 +08:00
|
|
|
// and a JKQTPlotter to display the function, combine everything in a layout
|
2018-12-24 03:27:24 +08:00
|
|
|
QWidget mainWin;
|
|
|
|
QLineEdit* edit=new QLineEdit(&mainWin);
|
|
|
|
edit->setToolTip("enter a function in dependence of the variable <tt>x</tt> and press ENTER to update the graph");
|
2018-12-24 19:29:33 +08:00
|
|
|
QCheckBox* check=new QCheckBox("display sample points");
|
2018-12-24 22:07:14 +08:00
|
|
|
QDoubleSpinBox* spinP1=new QDoubleSpinBox(&mainWin);
|
|
|
|
spinP1->setValue(8);
|
|
|
|
spinP1->setRange(-10000,10000);
|
|
|
|
spinP1->setToolTip("enter a Value for parameter <tt>p1</tt> and press ENTER to update the graph");
|
|
|
|
QDoubleSpinBox* spinP2=new QDoubleSpinBox(&mainWin);
|
|
|
|
spinP2->setValue(4);
|
|
|
|
spinP2->setRange(-10000,10000);
|
|
|
|
spinP2->setToolTip("enter a Value for parameter <tt>p1</tt> and press ENTER to update the graph");
|
2019-01-20 23:15:10 +08:00
|
|
|
JKQTPlotter* plot=new JKQTPlotter(&mainWin);
|
2018-12-24 22:07:14 +08:00
|
|
|
QFormLayout* flayout=new QFormLayout;
|
2018-12-24 03:27:24 +08:00
|
|
|
QVBoxLayout* layout=new QVBoxLayout;
|
|
|
|
mainWin.setLayout(layout);
|
2018-12-24 22:07:14 +08:00
|
|
|
flayout->addRow("equation:", edit);
|
|
|
|
flayout->addRow("p1 =", spinP1);
|
|
|
|
flayout->addRow("p2 =", spinP2);
|
|
|
|
flayout->addRow("", check);
|
|
|
|
layout->addLayout(flayout);
|
2018-12-24 03:27:24 +08:00
|
|
|
layout->addWidget(plot);
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
// 2. now we add a JKQTPXParsedFunctionLineGraph object, which will draw the function from
|
2018-12-24 03:27:24 +08:00
|
|
|
// the line edit
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXParsedFunctionLineGraph* parsedFunc=new JKQTPXParsedFunctionLineGraph(plot);
|
2018-12-24 03:27:24 +08:00
|
|
|
plot->addGraph(parsedFunc);
|
|
|
|
// finally we connect the line edit with the graph, whenever RETURN is pressed,
|
|
|
|
// the graph is updated:
|
|
|
|
auto updateGraphFunctor=
|
|
|
|
[=]() {
|
2019-01-26 20:00:40 +08:00
|
|
|
parsedFunc->setTitle(QString("user function: \\verb{"+edit->text()+"}, p_1=%1, p_2=%2").arg(spinP1->value()).arg(spinP2->value()));
|
|
|
|
parsedFunc->setFunction(edit->text());
|
|
|
|
parsedFunc->setParamsV(spinP1->value(), spinP2->value());
|
|
|
|
parsedFunc->setDisplaySamplePoints(check->isChecked());
|
2019-01-26 19:28:44 +08:00
|
|
|
plot->redrawPlot();
|
2018-12-24 03:27:24 +08:00
|
|
|
};
|
|
|
|
QObject::connect(edit, &QLineEdit::returnPressed, updateGraphFunctor);
|
|
|
|
QObject::connect(edit, &QLineEdit::editingFinished, updateGraphFunctor);
|
2018-12-24 19:29:33 +08:00
|
|
|
QObject::connect(check, &QCheckBox::toggled, updateGraphFunctor);
|
2018-12-24 22:07:14 +08:00
|
|
|
QObject::connect(spinP1, &QDoubleSpinBox::editingFinished, updateGraphFunctor);
|
|
|
|
QObject::connect(spinP2, &QDoubleSpinBox::editingFinished, updateGraphFunctor);
|
|
|
|
edit->setText("sin(x*p1)*exp(-x/p2)");
|
2018-12-24 03:27:24 +08:00
|
|
|
updateGraphFunctor();
|
|
|
|
|
|
|
|
|
|
|
|
// 3. set some axis properties (we use LaTeX for nice equation rendering)
|
2019-01-26 03:16:04 +08:00
|
|
|
plot->getXAxis()->setAxisLabel(QObject::tr("x-axis"));
|
|
|
|
plot->getYAxis()->setAxisLabel(QObject::tr("y-axis"));
|
2018-12-24 03:27:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
// 4. scale the plot so the graph is contained
|
|
|
|
plot->setXY(-10,10,-10,10);
|
|
|
|
|
|
|
|
// show window and make it a decent size
|
|
|
|
mainWin.show();
|
|
|
|
mainWin.resize(600,400);
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|