added example that shows how to draw parametric curves

This commit is contained in:
jkriege2 2018-12-16 15:35:50 +01:00
parent e9ba3c0bd8
commit 743a1865bd
9 changed files with 185 additions and 1 deletions

View File

@ -51,6 +51,7 @@ addSimpleTest(speed)
addSimpleTest(rgbimageplot_qt)
addSimpleTest(impulsesplot)
addSimpleTest(paramscatterplot)
addSimpleTest(parametriccurve)
#addSimpleTest(imageplot_nodatastore)
#addSimpleTest(rgbimageplot_opencv)
#addSimpleTest(imageplot_opencv)

View File

@ -27,7 +27,8 @@ All test-projects are Qt-projects that use qmake to build. You can load them int
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/JKQTPbarVerticalGraphStacked_small.png)<br>![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/JKQTPbarHorizontalGraphStacked_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_stackedbars) | [Stacked Bar Charts](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_stackedbars) | `JKQTPbarVerticalStackableGraph`, `JKQTPbarHorizontalStackableGraph`<br/>C++-style vectors of data |
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_filledgraphs_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_filledgraphs) | [Filled Curve Plots](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_filledgraphs) | `JKQTPbarVerticalGraph`<br/>setting/altering data in `JKQTPdatstore` directly<br/> transparent plots<br/>calculating histograms |
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_impulsesplot_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_impulsesplot) | [Impulse Plots](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_impulsesplot) | `JKQTPimpulsesVerticalGraph` and `JKQTPimpulsesHorizontalGraph`<br/>C++-style QVector as plot data |
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_paramscatterplot_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_paramscatterplot) | [Scatter Graph with Parametrized Symbols/Colors](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_paramscatterplot) | `JKQTPimpulsesVerticalGraph` and `JKQTPimpulsesHorizontalGraph`<br/>C++-style QVector as plot data |
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_paramscatterplot_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_paramscatterplot) | [Scatter Graph with Parametrized Symbols/Colors](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_paramscatterplot) | `JKQTPxyParametrizedScatterGraph`<br/>C++-style QVector as plot data<br/>modify scatter/points/line-graph properties by data |
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_parametriccurve_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_parametriccurve) | [Plotting Parametric Curves](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_parametriccurve) | `JKQTPxyLineGraph` and `JKQTPxyParametrizedScatterGraph`<br/>C++-style QVector as plot data<br/>parametric curve plotting |
### Styling the Plot, Keys, Axes, ...

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

View File

@ -0,0 +1,64 @@
[Back to JKQTPlotter main page](https://github.com/jkriege2/JKQtPlotter/)
# JKQtPlotter
## Plotting Parametric Curves
This project (see `./test/simpletest_parametriccurve/`) demonstrates how to draw parametric curves, using [`JKQTPxyLineGraph`}(https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest) and [`JKQTPxyParametrizedScatterGraph`](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_paramscatterplot).
The source code of the main application can be found in [`jkqtplotter_simpletest_parametriccurve.cpp`](https://github.com/jkriege2/JKQtPlotter/blob/master/test/simpletest_parametriccurve/jkqtplotter_simpletest_parametriccurve.cpp). First, the parametric curve (here a [logarithic spiral](https://en.wikipedia.org/wiki/Logarithmic_spiral)) is sampled into two columns containing the x- and y-values along the curve. In addition the radial distance from x=y=0 is added into a third column:
```c++
QVector<double> X, Y, R;
const int Ndata=500; // number of plot points in each curve
const double phiMax=4.0*M_PI;
const double a=1;
const double k=0.2;
for (double phi=-phiMax; phi<=phiMax; phi+=phiMax/double(Ndata)) {
const double x=a*exp(k*phi)*cos(phi);
const double y=a*exp(k*phi)*sin(phi);
X<<x;
Y<<y;
R<<sqrt(x*x+y*y);
}
// and copy it to the datastore
size_t columnX=ds->addCopiedColumn(X, "x");
size_t columnY=ds->addCopiedColumn(Y, "y");
size_t columnR=ds->addCopiedColumn(R, "r");
```
Then simples graph just uses the columns X and Y to plot the curve:
```c++
JKQTPxyLineGraph* graph1=new JKQTPxyLineGraph(&plot);
graph1->set_xColumn(columnX);
graph1->set_yColumn(columnY);
graph1->set_drawLine(true);
graph1->set_symbol(JKQTPnoSymbol);
graph1->set_title("one-colored spiral");
plot.addGraph(graph1);
```
If you use `JKQTPxyParametrizedScatterGraph` instead of `JKQTPxyLineGraph`, you can also modify the color of the line-segments, connecting the datapoints:
```c++
JKQTPxyParametrizedScatterGraph* graph2=new JKQTPxyParametrizedScatterGraph(&plot2);
graph2->set_xColumn(columnX);
graph2->set_yColumn(columnY);
graph2->set_colorColumn(columnR);
graph2->set_palette(JKQTPMathImageMATLAB);
graph2->set_symbol(JKQTPnoSymbol);
graph2->set_drawLine(true);
graph2->set_title("colored spiral");
graph2->get_colorBarRightAxis()->set_axisLabel("color scale radius $r(\\phi)$");
plot2.addGraph(graph2);
```
The result looks like this:
![jkqtplotter_simpletest_parametriccurve](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_parametriccurve1.png)
... and with the line-color set by the radius:
![jkqtplotter_simpletest_parametriccurve](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_parametriccurve2.png)
[Back to JKQTPlotter main page](https://github.com/jkriege2/JKQtPlotter/)

View File

@ -0,0 +1,87 @@
#include <QApplication>
#include "jkqtplotter/jkqtplotter.h"
#include "jkqtplotter/jkqtpelements.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
// 1. create two plotter windows that share the same internal datastore (for convenience)
JKQtPlotter plot, plot2;
JKQTPdatastore* ds=plot.getDatastore();
plot2.useExternalDatastore(ds);
// 2. now we create a vector of x- and y-values of the logarithmic spiral
// in addition the radius is stored in column R
QVector<double> X, Y, R;
const int Ndata=500; // number of plot points in each curve
const double phiMax=4.0*M_PI;
const double a=1;
const double k=0.2;
for (double phi=-phiMax; phi<=phiMax; phi+=phiMax/double(Ndata)) {
const double x=a*exp(k*phi)*cos(phi);
const double y=a*exp(k*phi)*sin(phi);
X<<x;
Y<<y;
R<<sqrt(x*x+y*y);
}
// and copy it to the datastore
size_t columnX=ds->addCopiedColumn(X, "x");
size_t columnY=ds->addCopiedColumn(Y, "y");
size_t columnR=ds->addCopiedColumn(R, "r");
// 3. now we make several graphs. In each one, another property of the graph is controlled by
// a datacolumn
// 3.1 the basic graph
JKQTPxyLineGraph* graph1=new JKQTPxyLineGraph(&plot);
graph1->set_xColumn(columnX);
graph1->set_yColumn(columnY);
graph1->set_drawLine(true);
graph1->set_symbol(JKQTPnoSymbol);
graph1->set_title("one-colored spiral");
plot.addGraph(graph1);
// 3.2 for graph2 is the same as graph 1, but in addition, the color of the lines
// is set, based on the contents of column R, via the color-palette JKQTPMathImageMATLAB
JKQTPxyParametrizedScatterGraph* graph2=new JKQTPxyParametrizedScatterGraph(&plot2);
graph2->set_xColumn(columnX);
graph2->set_yColumn(columnY);
graph2->set_colorColumn(columnR);
graph2->set_palette(JKQTPMathImageMATLAB);
graph2->set_symbol(JKQTPnoSymbol);
graph2->set_drawLine(true);
graph2->set_title("colored spiral");
graph2->get_colorBarRightAxis()->set_axisLabel("color scale radius $r(\\phi)$");
plot2.addGraph(graph2);
// 4. set the axis scale aspect ratios to 1,
// autoscale the plot so the graph is contained
// and format the coordinate system and key
plot.get_plotter()->set_maintainAspectRatio(true);
plot.get_plotter()->set_aspectRatio(1);
plot.getXAxis()->set_axisLabel("x-axis");
plot.getYAxis()->set_axisLabel("y-axis");
plot.getXAxis()->set_drawGrid(false);
plot.getYAxis()->set_drawGrid(false);
plot.setXY(-15,15,-15,15);
plot2.get_plotter()->set_maintainAspectRatio(true);
plot2.get_plotter()->set_aspectRatio(1);
plot2.getXAxis()->set_axisLabel("x-axis");
plot2.getYAxis()->set_axisLabel("y-axis");
plot2.getXAxis()->set_drawGrid(false);
plot2.getYAxis()->set_drawGrid(false);
plot2.setXY(-15,15,-15,15);
// 5. show plotters and make them a decent size
plot.show();
plot.resize(600,600);
plot2.show();
plot2.resize(600,600);
return app.exec();
}

View File

@ -0,0 +1,23 @@
# source code for this simple demo
SOURCES = jkqtplotter_simpletest_parametriccurve.cpp
# configure Qt
CONFIG += qt
QT += core gui xml svg
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
# output executable name
TARGET = jkqtplotter_simpletest_parametriccurve
# include JKQtPlotter source code
DEPENDPATH += . ../../lib
INCLUDEPATH += ../../lib
CONFIG (debug, debug|release):LIBS += -L../../lib/debug -ljkqtplotterlib
CONFIG (release):LIBS += -L../../lib/release -ljkqtplotterlib
win32-msvc*: DEFINES += _USE_MATH_DEFINES
# here you can activate some debug options
#DEFINES += SHOW_JKQTPLOTTER_DEBUG
#DEFINES += JKQTBP_AUTOTIMER

View File

@ -0,0 +1,8 @@
TEMPLATE = subdirs
SUBDIRS += jkqtplotterlib jkqtplotter_simpletest_parametriccurve
jkqtplotterlib.file = ../../lib/jkqtplotterlib.pro
jkqtplotter_simpletest_parametriccurve.file=$$PWD/jkqtplotter_simpletest_parametriccurve.pro
jkqtplotter_simpletest_parametriccurve.depends = jkqtplotterlib