JKQtPlotter/examples/impulsesplot/impulsesplot.cpp

115 lines
3.9 KiB
C++
Raw Normal View History

/** \example impulsesplot.cpp
* Shows how to plot impulse graphs with JKQTPlotter
*
* \ref JKQTPlotterImpulsePlots
*/
2022-08-27 04:32:48 +08:00
#include "jkqtpexampleapplication.h"
#include <QApplication>
#include "jkqtplotter/jkqtplotter.h"
2019-06-20 22:06:31 +08:00
#include "jkqtplotter/graphs/jkqtpimpulses.h"
// 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) {
JKQTPDatastore* ds=plot.getDatastore();
// 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;
X << xx;
Y << cos(xx)*exp(-xx/10.0);
2022-09-26 08:08:01 +08:00
E << 0.2*Y.last();
}
// 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");
// 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);
graph->setLineWidth(2);
2022-09-26 08:08:01 +08:00
graph->setBaseline(baseline);
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);
}
//graph->setDrawSymbols(true);
//graph->setSymbolType(JKQTPGraphSymbols::JKQTPFilledStar);
//graph->setBaseline(0.25);
// 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)
plot.getXAxis()->setAxisLabel(QObject::tr("x-axis"));
plot.getYAxis()->setAxisLabel(QObject::tr("y-axis"));
// 7. switch the grid off
plot.getXAxis()->setDrawGrid(false);
plot.getYAxis()->setDrawGrid(false);
// 8. autoscale the plot so the graph is contained
plot.zoomToFit();
2022-09-26 08:08:01 +08:00
plot.getPlotter()->setShowKey(false);
// show plotter and make it a decent size
plot.show();
plot.resize(600/plot.devicePixelRatioF(),400/plot.devicePixelRatioF());
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;
drawPlot<JKQTPImpulsesVerticalGraph>(plot);
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;
drawPlot<JKQTPImpulsesHorizontalGraph>(plotH);
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());
return app.exec();
}