2019-06-21 04:24:47 +08:00
|
|
|
/** \example logaxes.cpp
|
2019-01-20 23:15:10 +08:00
|
|
|
* Shows how to use logarithmic axes with JKQTPlotter
|
2019-01-13 01:53:16 +08:00
|
|
|
*
|
2019-01-20 23:15:10 +08:00
|
|
|
* \ref JKQTPlotterLogAxes
|
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/jkqtpscatter.h"
|
|
|
|
#include "jkqtplotter/graphs/jkqtpgeometric.h"
|
2019-05-30 04:40:02 +08:00
|
|
|
#include "jkqtplotter/jkqtptools.h"
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
#define sqr(x) ((x)*(x))
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2022-04-16 05:01:09 +08:00
|
|
|
|
2022-08-27 04:32:48 +08:00
|
|
|
JKQTPAppSettingController highDPIController(argc, argv);
|
|
|
|
JKQTPExampleApplication app(argc, argv);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
2022-04-16 05:01:09 +08:00
|
|
|
|
2018-12-29 00:46:47 +08:00
|
|
|
// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
|
2019-01-20 23:15:10 +08:00
|
|
|
JKQTPlotter plot;
|
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 a vector of x-values for a simple plot (resonance curve with different damping value D)
|
|
|
|
// see https://en.wikipedia.org/wiki/Resonance
|
|
|
|
QVector<double> X;
|
|
|
|
QVector<double> D { 0.001, 0.1, 1, 10};
|
|
|
|
const int Ndata=500; // number of plot points in each curve
|
|
|
|
for (int i=0; i<Ndata; i++) {
|
|
|
|
const double x=double(i)/double(Ndata)*3;
|
|
|
|
X<<x;
|
|
|
|
//Y1<<1.0/sqrt(sqr(1-sqr(x))+sqr(2*x*D1));
|
|
|
|
}
|
|
|
|
// and copy it to the datastore
|
|
|
|
size_t columnX=ds->addCopiedColumn(X, "x");
|
|
|
|
|
|
|
|
// 3. now we make several plots for different values of D
|
|
|
|
QVector<Qt::PenStyle> pens {Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine, Qt::DashDotDotLine };
|
|
|
|
for (int id=0; id<D.size(); id++) {
|
|
|
|
// generate some plot data
|
|
|
|
QVector<double> Y;
|
|
|
|
for (auto& xx: X) {
|
|
|
|
Y<<1.0/sqrt(sqr(1-sqr(xx))+sqr(2*xx*D[id]));
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXYLineGraph* graph=new JKQTPXYLineGraph(&plot);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// copy data into datastore and immediately set the yColumn
|
2019-01-26 20:00:40 +08:00
|
|
|
graph->setXColumn(columnX);
|
|
|
|
graph->setYColumn(ds->addCopiedColumn(Y, "y"+QString::number(id)));
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// don't use symbols
|
2019-04-22 19:27:50 +08:00
|
|
|
graph->setSymbolType(JKQTPNoSymbol);
|
2018-12-29 00:46:47 +08:00
|
|
|
// use one of different pens
|
2019-04-22 19:27:50 +08:00
|
|
|
graph->setLineStyle(pens[id%pens.size()]);
|
2018-12-29 00:46:47 +08:00
|
|
|
// set width of graph line
|
2019-01-26 03:16:04 +08:00
|
|
|
graph->setLineWidth(1.5);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// graph title is made from symbol+penstyle
|
2019-01-26 20:00:40 +08:00
|
|
|
graph->setTitle(QString("$D=\\delta/\\omega_0=%1$").arg(D[id]));
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// add the graph to the plot, so it is actually displayed
|
|
|
|
plot.addGraph(graph);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. Also we add a text-element in the plot to show the plotted function
|
2019-01-20 17:49:29 +08:00
|
|
|
// This element (JKQTPGeoText) is taken from the set of geometric elements
|
2018-12-29 00:46:47 +08:00
|
|
|
// and is simply parametrized by a position (1.25/10) and the text to display.
|
|
|
|
// In addition you can also set the font size (here to 15)
|
|
|
|
// Use '$...$' around the actual math string to ensure rendering with a math font
|
|
|
|
// (the internal renderer uses XITS fonts by default, which are free and auto-distributed
|
|
|
|
// and loaded in the library). If you don't use the math-mode modifiers, the default
|
|
|
|
// font of the other rendering text is used, which might not be suitable for
|
|
|
|
// high-quality math rendering.
|
2019-04-22 19:27:50 +08:00
|
|
|
plot.addGraph(new JKQTPGeoText(&plot, 1.25, 10, "$\\frac{A}{A_{stat}}=\\frac{1}{\\sqrt{\\left(1-\\eta^2\\right)^2+\\left(2{\\eta}D\\right)^2}}$", 15, QColor("black")));
|
2018-12-29 00:46:47 +08:00
|
|
|
|
2019-01-26 03:16:04 +08:00
|
|
|
// 5. set y-axis to logarithmic (x-axis would be analogous, but using `plot.getXAxis()`)
|
2019-01-26 20:00:40 +08:00
|
|
|
plot.getYAxis()->setLogAxis(true);
|
2018-12-29 00:46:47 +08:00
|
|
|
// now we set the number of label ticks to 9 (you give the count if minor between two majors,
|
|
|
|
// so if you want ticks for 1,2,3,...,10,20,30,...,100... you need to use 9:
|
2019-01-26 20:00:40 +08:00
|
|
|
plot.getYAxis()->setMinorTicks(9);
|
2018-12-29 00:46:47 +08:00
|
|
|
// the minor grid is not shown by default. You can switch it on:
|
2019-01-26 20:00:40 +08:00
|
|
|
plot.getYAxis()->setDrawMinorGrid(true);
|
2018-12-29 00:46:47 +08:00
|
|
|
// usually axis ticks are shown as numbers 0.01, 0.1, 1, 10, ... You can also force the scientific
|
|
|
|
// power-of-10 notation, using:
|
2019-01-26 20:00:40 +08:00
|
|
|
plot.getYAxis()->setLabelType(JKQTPCALTexponent);
|
2018-12-29 00:46:47 +08:00
|
|
|
// the number of digits in JKQTPCALTexponent determines which labels are drawn in standard-notation,
|
|
|
|
// as compared to power-notation (e.g. if set to 1, the labels 0.1=10^{-1}, 1, 10 are shown in
|
|
|
|
// standard notation, the rest in power-notation. This tweak improves readability)
|
2019-01-26 20:00:40 +08:00
|
|
|
plot.getYAxis()->setLabelDigits(0);
|
2018-12-29 00:46:47 +08:00
|
|
|
// minor tick labels are usually not displayed, but you can switch them on, using
|
2019-01-26 20:00:40 +08:00
|
|
|
//plot.getYAxis()->setMinorTickLabelsEnabled(true);
|
2018-12-29 00:46:47 +08:00
|
|
|
// the axis font sizes can be set with:
|
2019-01-26 20:00:40 +08:00
|
|
|
plot.getYAxis()->setTickLabelFontSize(10); // axis tick labels
|
|
|
|
plot.getYAxis()->setMinorTickLabelFontSize(7); // minor axis tick labels
|
|
|
|
plot.getYAxis()->setLabelFontSize(14); // axis label size
|
|
|
|
plot.getXAxis()->setTickLabelFontSize(10); // axis tick labels
|
|
|
|
plot.getXAxis()->setMinorTickLabelFontSize(7); // minor axis tick labels
|
|
|
|
plot.getXAxis()->setLabelFontSize(14); // axis label size
|
2018-12-29 00:46:47 +08:00
|
|
|
// ... and finally set axis labels (using LaTeX notation and $...$ to improve rendering)
|
2019-01-26 03:16:04 +08:00
|
|
|
plot.getYAxis()->setAxisLabel("Amplitude $A/A_{stat}$");
|
|
|
|
plot.getXAxis()->setAxisLabel("relative driving frequency $\\eta=\\omega/\\omega_0$");
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
// 6. autoscale the plot so the graph is contained
|
|
|
|
plot.zoomToFit();
|
|
|
|
|
|
|
|
// 7. show plotter and make it a decent size
|
|
|
|
plot.show();
|
2022-08-29 04:48:14 +08:00
|
|
|
plot.resize(700/plot.devicePixelRatioF(),500/plot.devicePixelRatioF());
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|