2019-06-21 04:24:47 +08:00
|
|
|
/** \example impulsesplot.cpp
|
2019-01-20 23:15:10 +08:00
|
|
|
* Shows how to plot impulse graphs with JKQTPlotter
|
2019-01-13 01:53:16 +08:00
|
|
|
*
|
2019-01-20 23:15:10 +08:00
|
|
|
* \ref JKQTPlotterImpulsePlots
|
2019-01-13 01:53:16 +08:00
|
|
|
*/
|
|
|
|
|
2022-08-27 04:32:48 +08:00
|
|
|
#include "jkqtpexampleapplication.h"
|
2018-12-29 00:46:47 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include "jkqtplotter/jkqtplotter.h"
|
2019-06-20 22:06:31 +08:00
|
|
|
#include "jkqtplotter/graphs/jkqtpimpulses.h"
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// number of datapoints:
|
|
|
|
|
2022-09-26 08:08:01 +08:00
|
|
|
template<class TGRAPH>
|
|
|
|
TGRAPH* drawPlot(JKQTPlotter& plot, int NData=40, double baseline=0.0) {
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPDatastore* ds=plot.getDatastore();
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// 2. now we create data for a simple plot (a sine curve with lin. increasing errors)
|
2022-09-26 08:08:01 +08:00
|
|
|
QVector<double> X, Y, E;
|
|
|
|
for (int i=0; i<NData; i++) {
|
|
|
|
const double xx=double(i)/40.0*8.0*JKQTPSTATISTICS_PI;
|
2018-12-29 00:46:47 +08:00
|
|
|
X << xx;
|
|
|
|
Y << cos(xx)*exp(-xx/10.0);
|
2022-09-26 08:08:01 +08:00
|
|
|
E << 0.2*Y.last();
|
2018-12-29 00:46:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. add data from vectors to the datastore
|
|
|
|
size_t columnX=ds->addCopiedColumn(X, "x");
|
|
|
|
size_t columnY=ds->addCopiedColumn(Y, "y");
|
2022-09-26 08:08:01 +08:00
|
|
|
size_t columnE=ds->addCopiedColumn(E, "e");
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// 4. create a vertical impulse graph in the plot, which plots the dataset X/Y:
|
2022-09-26 08:08:01 +08:00
|
|
|
TGRAPH* graph=new TGRAPH(&plot);
|
|
|
|
graph->setKeyColumn(columnX);
|
|
|
|
graph->setValueColumn(columnY);
|
2019-01-26 03:16:04 +08:00
|
|
|
graph->setLineWidth(2);
|
2022-09-26 08:08:01 +08:00
|
|
|
graph->setBaseline(baseline);
|
2019-01-26 20:00:40 +08:00
|
|
|
graph->setColor(QColor("red"));
|
|
|
|
graph->setTitle(QObject::tr("$\\cos(x)\\cdot\\exp(-x/10)$"));
|
2022-09-26 08:08:01 +08:00
|
|
|
if (dynamic_cast<JKQTPYGraphErrors*>(graph)) {
|
|
|
|
dynamic_cast<JKQTPYGraphErrors*>(graph)->setYErrorColumn(columnE);
|
|
|
|
dynamic_cast<JKQTPYGraphErrors*>(graph)->setYErrorStyle(JKQTPErrorBars);
|
|
|
|
}
|
|
|
|
if (dynamic_cast<JKQTPXGraphErrors*>(graph)) {
|
|
|
|
dynamic_cast<JKQTPXGraphErrors*>(graph)->setXErrorColumn(columnE);
|
|
|
|
dynamic_cast<JKQTPXGraphErrors*>(graph)->setXErrorStyle(JKQTPErrorBars);
|
|
|
|
}
|
2019-04-22 19:27:50 +08:00
|
|
|
//graph->setDrawSymbols(true);
|
|
|
|
//graph->setSymbolType(JKQTPGraphSymbols::JKQTPFilledStar);
|
|
|
|
//graph->setBaseline(0.25);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// 5. add the graph to the plot, so it is actually displayed
|
|
|
|
plot.addGraph(graph);
|
|
|
|
|
|
|
|
// 6. 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-29 00:46:47 +08:00
|
|
|
|
|
|
|
// 7. switch the grid off
|
2019-01-26 20:00:40 +08:00
|
|
|
plot.getXAxis()->setDrawGrid(false);
|
|
|
|
plot.getYAxis()->setDrawGrid(false);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// 8. autoscale the plot so the graph is contained
|
|
|
|
plot.zoomToFit();
|
2022-09-26 08:08:01 +08:00
|
|
|
plot.getPlotter()->setShowKey(false);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// show plotter and make it a decent size
|
|
|
|
plot.show();
|
2022-08-29 04:48:14 +08:00
|
|
|
plot.resize(600/plot.devicePixelRatioF(),400/plot.devicePixelRatioF());
|
2018-12-29 00:46:47 +08:00
|
|
|
|
2022-09-26 08:08:01 +08:00
|
|
|
return graph;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
|
|
|
|
JKQTPAppSettingController highDPIController(argc, argv);
|
|
|
|
JKQTPExampleApplication app(argc, argv);
|
|
|
|
|
|
|
|
|
|
|
|
// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
|
|
|
|
JKQTPlotter plot;
|
2022-11-01 03:34:01 +08:00
|
|
|
auto grV=drawPlot<JKQTPImpulsesVerticalGraph>(plot);
|
2022-09-26 08:08:01 +08:00
|
|
|
plot.setWindowTitle("1: JKQTPImpulsesVerticalGraph");
|
|
|
|
|
|
|
|
// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
|
|
|
|
JKQTPlotter plotS;
|
|
|
|
auto grS=drawPlot<JKQTPImpulsesVerticalGraph>(plotS, 25);
|
|
|
|
plotS.setWindowTitle("2: JKQTPImpulsesVerticalGraph + Symbols");
|
|
|
|
grS->setDrawSymbols(true);
|
|
|
|
grS->setSymbolType(JKQTPFilledStar);
|
|
|
|
grS->setSymbolFillColor(QColor("cornflowerblue"));
|
|
|
|
grS->setSymbolColor(QColor("blue"));
|
|
|
|
grS->setSymbolSize(12);
|
|
|
|
|
|
|
|
JKQTPlotter plotH;
|
2022-11-01 03:34:01 +08:00
|
|
|
auto grH=drawPlot<JKQTPImpulsesHorizontalGraph>(plotH);
|
2022-09-26 08:08:01 +08:00
|
|
|
plotH.setWindowTitle("3: JKQTPImpulsesHorizontalGraph");
|
|
|
|
plotH.resize(400/plot.devicePixelRatioF(),600/plot.devicePixelRatioF());
|
|
|
|
|
|
|
|
JKQTPlotter plotE;
|
|
|
|
auto grE=drawPlot<JKQTPImpulsesVerticalErrorGraph>(plotE,10);
|
|
|
|
plotE.setWindowTitle("4: JKQTPImpulsesVerticalErrorGraph");
|
|
|
|
grE->setLineWidth(5);
|
|
|
|
|
|
|
|
JKQTPlotter plotEH;
|
|
|
|
auto grEH=drawPlot<JKQTPImpulsesHorizontalErrorGraph>(plotEH,10);
|
|
|
|
plotEH.setWindowTitle("5: JKQTPImpulsesHorizontalErrorGraph");
|
|
|
|
grEH->setLineWidth(4);
|
|
|
|
plotEH.resize(400/plot.devicePixelRatioF(),600/plot.devicePixelRatioF());
|
|
|
|
|
2022-11-01 03:34:01 +08:00
|
|
|
app.addExportStepFunctor([&]() {
|
|
|
|
grV->setDrawBaseline(false);
|
|
|
|
grS->setDrawBaseline(false);
|
|
|
|
grH->setDrawBaseline(false);
|
|
|
|
grE->setDrawBaseline(false);
|
|
|
|
grEH->setDrawBaseline(false);
|
|
|
|
|
|
|
|
plot.getXAxis()->setShowZeroAxis(false);
|
|
|
|
plot.getYAxis()->setShowZeroAxis(false);
|
|
|
|
|
|
|
|
plotE.getXAxis()->setShowZeroAxis(false);
|
|
|
|
plotE.getYAxis()->setShowZeroAxis(false);
|
|
|
|
|
|
|
|
plotEH.getXAxis()->setShowZeroAxis(false);
|
|
|
|
plotEH.getYAxis()->setShowZeroAxis(false);
|
|
|
|
|
|
|
|
plotH.getXAxis()->setShowZeroAxis(false);
|
|
|
|
plotH.getYAxis()->setShowZeroAxis(false);
|
|
|
|
|
|
|
|
plotS.getXAxis()->setShowZeroAxis(false);
|
|
|
|
plotS.getYAxis()->setShowZeroAxis(false);
|
|
|
|
});
|
|
|
|
app.addExportStepFunctor([&]() {
|
|
|
|
grV->setDrawBaseline(true);
|
|
|
|
grS->setDrawBaseline(true);
|
|
|
|
grH->setDrawBaseline(true);
|
|
|
|
grE->setDrawBaseline(true);
|
|
|
|
grEH->setDrawBaseline(true);
|
|
|
|
});
|
2022-09-26 08:08:01 +08:00
|
|
|
|
2018-12-29 00:46:47 +08:00
|
|
|
return app.exec();
|
|
|
|
}
|