mirror of
https://github.com/jkriege2/JKQtPlotter.git
synced 2024-12-24 17:41:39 +08:00
added new, but simple example
This commit is contained in:
parent
c922e59d90
commit
b36a021f13
77
README.md
77
README.md
@ -8,7 +8,7 @@ This software is licensed under the term of the GNU Lesser General Public Licens
|
||||
This section assembles some simple examples of usage. You can find more (complex) examples for the classes in this repository in the subfolder "test". All test-projects are Qt-projects that use qmake to build. You can load them into QtCreator easily.
|
||||
|
||||
###Very simple line-graph
|
||||
This project (see `./test/jkqtplotter_simpletest/`) simply creates a JKQtPlotter widget (as a new window) and adds a single line-graph (a sine-wave).
|
||||
This project (see `./test/jkqtplotter_simpletest/`) simply creates a JKQtPlotter widget (as a new window) and adds a single line-graph (a sine-wave). Data is initialized from two QVector<double> objects.
|
||||
The QMake project looks like this (see `./test/jkqtplotter_simpletest/jkqtplotter_simpletest.pro`):
|
||||
```qmake
|
||||
# source code for this simple demo
|
||||
@ -81,6 +81,81 @@ int main(int argc, char* argv[])
|
||||
The result looks like this:
|
||||
![jkqtplotter_simpletest1](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest1.png)
|
||||
|
||||
###Very simple line-graph
|
||||
This project (see `./test/jkqtplotter_simpletest_symbols_and_errors/`) simply creates a JKQtPlotter widget (as a new window) and adds a single line-graph (a sine-wave) that has y-errorbars. In addition, this example shows how to change some of the axis properties and how to use LaTeX markup to format axis labels (can actually be used for all labels in JKQtPlotter). Also, in comparison to the last example, here we initialize the data from C-type arrays (double*), instead of QVector<double> objects.
|
||||
|
||||
The soruce code of the main application is (see `./test/jkqtplotter_simpletest_symbols_and_errors/jkqtplotter_simpletest_symbols_and_errors.cpp`):
|
||||
```c++
|
||||
#include <QApplication>
|
||||
#include "jkqtplotter.h"
|
||||
|
||||
// number of datapoints:
|
||||
#define Ndata 10
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
|
||||
JKQtPlotter plot;
|
||||
JKQTPdatastore* ds=plot.getDatastore();
|
||||
|
||||
// 2. now we create data for a simple plot (a sine curve with lin. increasing errors)
|
||||
double X[Ndata], Y[Ndata], YERROR[Ndata];
|
||||
for (int i=0; i<Ndata; i++) {
|
||||
X[i]=double(i)/double(Ndata)*2.0*M_PI;
|
||||
Y[i]=sin(X[i]);
|
||||
YERROR[i]=0.2+double(i)/double(Ndata)*0.25;
|
||||
}
|
||||
|
||||
// 3. make data available to JKQtPlotter by adding it to the internal datastore.
|
||||
// Note: In this step the data is copied (of not specified otherwise), so you can
|
||||
// reuse X and Y afterwards!
|
||||
// the variables columnX and columnY will contain the internal column ID of the newly
|
||||
// created columns with names "x" and "y" and the (copied) data from X and Y.
|
||||
size_t columnX=ds->addCopiedColumn(X, Ndata, "x");
|
||||
size_t columnY=ds->addCopiedColumn(Y, Ndata, "y");
|
||||
size_t columnYE=ds->addCopiedColumn(YERROR, Ndata, "y-error");
|
||||
|
||||
// 4. create a graph in the plot, which plots the dataset X/Y:
|
||||
JKQTPxyLineErrorGraph* graph1=new JKQTPxyLineErrorGraph(&plot);
|
||||
graph1->set_xColumn(columnX);
|
||||
graph1->set_yColumn(columnY);
|
||||
graph1->set_yErrorColumn(columnYE);
|
||||
graph1->set_symbol(JKQTPfilledStar); // set symbol style
|
||||
graph1->set_yErrorStyle(JKQTPerrorBars); // set error indicator type
|
||||
graph1->set_drawLine(false); // don't draw a line
|
||||
graph1->set_title(QObject::tr("sine graph"));
|
||||
|
||||
// 5. add the graph to the plot, so it is actually displayed
|
||||
plot.addGraph(graph1);
|
||||
|
||||
// 6. hide 0-lines
|
||||
plot.getXAxis()->set_showZeroAxis(false);
|
||||
plot.getYAxis()->set_showZeroAxis(false);
|
||||
|
||||
// 7. set some axis properties (we use LaTeX for nice equation rendering)
|
||||
plot.getXAxis()->set_axisLabel(QObject::tr("x-axis $x$ [mm]"));
|
||||
plot.getYAxis()->set_axisLabel(QObject::tr("\\textbf{\\color{red}{y-axis} $\\left(y=\\sin(x)\\pm(0.2+0.25\\cdot x)\\right)$ [A.U.]}"));
|
||||
plot.getXAxis()->set_labelFont("Arial");
|
||||
plot.getYAxis()->set_labelFont("Times New Roman");
|
||||
plot.getYAxis()->set_labelFontSize(12); // large x-axis label
|
||||
plot.getYAxis()->set_tickLabelFontSize(10); // and larger y-axis tick labels
|
||||
|
||||
// 8. autoscale the plot so the graph is contained
|
||||
plot.zoomToFit();
|
||||
|
||||
// show plotter and make it a decent size
|
||||
plot.show();
|
||||
plot.resize(600,400);
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
```
|
||||
The result looks like this:
|
||||
![jkqtplotter_simpletest_symbols_and_errors](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_symbols_and_errors.png)
|
||||
|
||||
|
||||
##Screenshots
|
||||
###Scatter Plots and Boxplots
|
||||
|
@ -657,11 +657,12 @@ void JKQTPPeakStreamGraph::drawKeyMarker(JKQTPEnhancedPainter &painter, QRectF &
|
||||
|
||||
|
||||
|
||||
JKQTPgraphErrors::JKQTPgraphErrors() {
|
||||
errorColor=QColor("black");
|
||||
JKQTPgraphErrors::JKQTPgraphErrors(QColor graphColor) {
|
||||
errorColor=graphColor.darker();
|
||||
errorStyle=Qt::SolidLine;
|
||||
errorWidth=2;
|
||||
errorFillColor=QColor("lightgray");
|
||||
errorFillColor=graphColor.lighter();
|
||||
errorColor.setAlphaF(0.5);
|
||||
errorFillStyle=Qt::SolidPattern;
|
||||
errorbarSize=7;
|
||||
}
|
||||
@ -1054,8 +1055,8 @@ double JKQTPgraphErrors::getYErrorL(int/* i*/, JKQTPdatastore */*ds*/) const
|
||||
|
||||
|
||||
|
||||
JKQTPxGraphErrors::JKQTPxGraphErrors():
|
||||
JKQTPgraphErrors()
|
||||
JKQTPxGraphErrors::JKQTPxGraphErrors(QColor graphColor):
|
||||
JKQTPgraphErrors(graphColor)
|
||||
{
|
||||
xErrorSymmetric=true;
|
||||
xErrorColumn=-1;
|
||||
@ -1090,8 +1091,8 @@ double JKQTPxGraphErrors::getXErrorL(int i, JKQTPdatastore *ds) const
|
||||
|
||||
|
||||
|
||||
JKQTPyGraphErrors::JKQTPyGraphErrors():
|
||||
JKQTPgraphErrors()
|
||||
JKQTPyGraphErrors::JKQTPyGraphErrors(QColor graphColor):
|
||||
JKQTPgraphErrors(graphColor)
|
||||
{
|
||||
yErrorColumn=-1;
|
||||
yErrorSymmetric=true;
|
||||
@ -1126,8 +1127,8 @@ double JKQTPyGraphErrors::getYErrorL(int i, JKQTPdatastore *ds) const
|
||||
|
||||
|
||||
|
||||
JKQTPxyGraphErrors::JKQTPxyGraphErrors():
|
||||
JKQTPgraphErrors()
|
||||
JKQTPxyGraphErrors::JKQTPxyGraphErrors(QColor graphColor):
|
||||
JKQTPgraphErrors(graphColor)
|
||||
{
|
||||
xErrorSymmetric=true;
|
||||
yErrorSymmetric=true;
|
||||
@ -4216,6 +4217,7 @@ JKQTPbarHorizontalGraph::JKQTPbarHorizontalGraph(JKQtBasePlotter* parent):
|
||||
parentPlotStyle=parent->getNextStyle();
|
||||
fillColor=parent->getPlotStyle(parentPlotStyle).color();
|
||||
}
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
|
||||
|
||||
@ -4236,6 +4238,7 @@ JKQTPbarHorizontalGraph::JKQTPbarHorizontalGraph(JKQtPlotter* parent):
|
||||
parentPlotStyle=parent->getNextStyle();
|
||||
fillColor=parent->getPlotStyle(parentPlotStyle).color();
|
||||
}
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
|
||||
void JKQTPbarHorizontalGraph::drawKeyMarker(JKQTPEnhancedPainter& painter, QRectF& rect) {
|
||||
@ -5291,6 +5294,13 @@ bool JKQTPgraphErrors::errorUsesColumn(int /*c*/)
|
||||
return false;
|
||||
}
|
||||
|
||||
void JKQTPgraphErrors::setErrorColorFromGraphColor(QColor graphColor)
|
||||
{
|
||||
errorColor=graphColor.darker();
|
||||
errorFillColor=graphColor.lighter();
|
||||
//errorColor.setAlphaF(0.5);
|
||||
}
|
||||
|
||||
bool JKQTPyGraphErrors::errorUsesColumn(int c)
|
||||
{
|
||||
return (c==yErrorColumn) || (c==yErrorColumnLower);
|
||||
|
@ -397,7 +397,7 @@ class LIB_EXPORT JKQTPPeakStreamGraph: public JKQTPsingleColumnGraph {
|
||||
class LIB_EXPORT JKQTPgraphErrors {
|
||||
public:
|
||||
/** \brief class contructor */
|
||||
JKQTPgraphErrors();
|
||||
JKQTPgraphErrors(QColor graphColor=QColor("black"));
|
||||
|
||||
JKQTPGET_SET_MACRO(QColor, errorColor)
|
||||
JKQTPGET_SET_MACRO(Qt::PenStyle, errorStyle)
|
||||
@ -408,6 +408,8 @@ class LIB_EXPORT JKQTPgraphErrors {
|
||||
/** \brief returns true, if the error plots use the given column */
|
||||
virtual bool errorUsesColumn(int c);
|
||||
|
||||
void setErrorColorFromGraphColor(QColor graphColor);
|
||||
|
||||
protected:
|
||||
/** \brief color of the error lines/bars */
|
||||
QColor errorColor;
|
||||
@ -454,7 +456,7 @@ class LIB_EXPORT JKQTPgraphErrors {
|
||||
class LIB_EXPORT JKQTPxGraphErrors: public JKQTPgraphErrors {
|
||||
public:
|
||||
/** \brief class contructor */
|
||||
JKQTPxGraphErrors();
|
||||
JKQTPxGraphErrors(QColor graphColor=QColor("black"));
|
||||
|
||||
JKQTPGET_SET_MACRO(bool, xErrorSymmetric)
|
||||
JKQTPGET_MACRO(int, xErrorColumnLower)
|
||||
@ -505,7 +507,7 @@ class LIB_EXPORT JKQTPxGraphErrors: public JKQTPgraphErrors {
|
||||
class LIB_EXPORT JKQTPyGraphErrors: public JKQTPgraphErrors {
|
||||
public:
|
||||
/** \brief class contructor */
|
||||
JKQTPyGraphErrors();
|
||||
JKQTPyGraphErrors(QColor graphColor=QColor("black"));
|
||||
|
||||
JKQTPGET_SET_MACRO(bool, yErrorSymmetric)
|
||||
JKQTPGET_MACRO(int, yErrorColumnLower)
|
||||
@ -553,7 +555,7 @@ class LIB_EXPORT JKQTPyGraphErrors: public JKQTPgraphErrors {
|
||||
class LIB_EXPORT JKQTPxyGraphErrors: public JKQTPgraphErrors {
|
||||
public:
|
||||
/** \brief class contructor */
|
||||
JKQTPxyGraphErrors();
|
||||
JKQTPxyGraphErrors(QColor graphColor=QColor("black"));
|
||||
JKQTPGET_SET_MACRO(bool, xErrorSymmetric)
|
||||
JKQTPGET_SET_MACRO(bool, yErrorSymmetric)
|
||||
JKQTPGET_MACRO(int, xErrorColumnLower)
|
||||
@ -798,11 +800,11 @@ class LIB_EXPORT JKQTPxyLineErrorGraph: public JKQTPxyLineGraph, public JKQTPxyG
|
||||
/** \brief class constructor */
|
||||
inline JKQTPxyLineErrorGraph(JKQtBasePlotter* parent=NULL):
|
||||
JKQTPxyLineGraph(parent), JKQTPxyGraphErrors()
|
||||
{ }
|
||||
{ setErrorColorFromGraphColor(color); }
|
||||
/** \brief class constructor */
|
||||
inline JKQTPxyLineErrorGraph(JKQtPlotter* parent=NULL):
|
||||
JKQTPxyLineGraph(parent), JKQTPxyGraphErrors()
|
||||
{ }
|
||||
{ setErrorColorFromGraphColor(color); }
|
||||
|
||||
/** \brief get the maximum and minimum x-value of the graph
|
||||
*
|
||||
@ -838,10 +840,10 @@ class LIB_EXPORT JKQTPxyParametrizedErrorScatterGraph: public JKQTPxyParametrize
|
||||
public:
|
||||
inline JKQTPxyParametrizedErrorScatterGraph(JKQtBasePlotter* parent=NULL):
|
||||
JKQTPxyParametrizedScatterGraph(parent), JKQTPxyGraphErrors()
|
||||
{ }
|
||||
{ setErrorColorFromGraphColor(color); }
|
||||
inline JKQTPxyParametrizedErrorScatterGraph(JKQtPlotter* parent=NULL):
|
||||
JKQTPxyParametrizedScatterGraph(parent), JKQTPxyGraphErrors()
|
||||
{ }
|
||||
{ setErrorColorFromGraphColor(color); }
|
||||
|
||||
|
||||
/** \brief get the maximum and minimum x-value of the graph
|
||||
@ -926,10 +928,12 @@ class LIB_EXPORT JKQTPimpulsesHorizontalErrorGraph: public JKQTPimpulsesHorizont
|
||||
inline JKQTPimpulsesHorizontalErrorGraph(JKQtBasePlotter* parent=NULL):
|
||||
JKQTPimpulsesHorizontalGraph(parent), JKQTPxGraphErrors()
|
||||
{
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
inline JKQTPimpulsesHorizontalErrorGraph(JKQtPlotter* parent=NULL):
|
||||
JKQTPimpulsesHorizontalGraph(parent), JKQTPxGraphErrors()
|
||||
{
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
/** \copydoc JKQTPgraph::usesColumn() */
|
||||
virtual bool usesColumn(int c);
|
||||
@ -979,11 +983,13 @@ class LIB_EXPORT JKQTPimpulsesVerticalErrorGraph: public JKQTPimpulsesVerticalGr
|
||||
inline JKQTPimpulsesVerticalErrorGraph(JKQtBasePlotter* parent=NULL):
|
||||
JKQTPimpulsesVerticalGraph(parent), JKQTPyGraphErrors()
|
||||
{
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
/** \brief class constructor */
|
||||
inline JKQTPimpulsesVerticalErrorGraph(JKQtPlotter* parent=NULL):
|
||||
JKQTPimpulsesVerticalGraph(parent), JKQTPyGraphErrors()
|
||||
{
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
/** \copydoc JKQTPgraph::usesColumn() */
|
||||
virtual bool usesColumn(int c);
|
||||
@ -1079,11 +1085,13 @@ class LIB_EXPORT JKQTPfilledCurveXErrorGraph: public JKQTPfilledCurveXGraph, pub
|
||||
inline JKQTPfilledCurveXErrorGraph(JKQtBasePlotter* parent=NULL):
|
||||
JKQTPfilledCurveXGraph(parent), JKQTPyGraphErrors()
|
||||
{
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
/** \brief class constructor */
|
||||
inline JKQTPfilledCurveXErrorGraph(JKQtPlotter* parent=NULL):
|
||||
JKQTPfilledCurveXGraph(parent), JKQTPyGraphErrors()
|
||||
{
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
/** \copydoc JKQTPgraph::usesColumn() */
|
||||
virtual bool usesColumn(int c);
|
||||
@ -1135,10 +1143,12 @@ class LIB_EXPORT JKQTPfilledCurveYErrorGraph: public JKQTPfilledCurveYGraph, pub
|
||||
inline JKQTPfilledCurveYErrorGraph(JKQtBasePlotter* parent=NULL):
|
||||
JKQTPfilledCurveYGraph(parent), JKQTPxGraphErrors()
|
||||
{
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
inline JKQTPfilledCurveYErrorGraph(JKQtPlotter* parent=NULL):
|
||||
JKQTPfilledCurveYGraph(parent), JKQTPxGraphErrors()
|
||||
{
|
||||
setErrorColorFromGraphColor(color);
|
||||
}
|
||||
/** \copydoc JKQTPgraph::usesColumn() */
|
||||
virtual bool usesColumn(int c);
|
||||
|
BIN
screenshots/jkqtplotter_simpletest_symbols_and_errors.png
Normal file
BIN
screenshots/jkqtplotter_simpletest_symbols_and_errors.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
@ -0,0 +1,65 @@
|
||||
#include <QApplication>
|
||||
#include "jkqtplotter.h"
|
||||
|
||||
// number of datapoints:
|
||||
#define Ndata 10
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
|
||||
JKQtPlotter plot;
|
||||
JKQTPdatastore* ds=plot.getDatastore();
|
||||
|
||||
// 2. now we create data for a simple plot (a sine curve with lin. increasing errors)
|
||||
double X[Ndata], Y[Ndata], YERROR[Ndata];
|
||||
for (int i=0; i<Ndata; i++) {
|
||||
X[i]=double(i)/double(Ndata)*2.0*M_PI;
|
||||
Y[i]=sin(X[i]);
|
||||
YERROR[i]=0.2+double(i)/double(Ndata)*0.25;
|
||||
}
|
||||
|
||||
// 3. make data available to JKQtPlotter by adding it to the internal datastore.
|
||||
// Note: In this step the data is copied (of not specified otherwise), so you can
|
||||
// reuse X and Y afterwards!
|
||||
// the variables columnX and columnY will contain the internal column ID of the newly
|
||||
// created columns with names "x" and "y" and the (copied) data from X and Y.
|
||||
size_t columnX=ds->addCopiedColumn(X, Ndata, "x");
|
||||
size_t columnY=ds->addCopiedColumn(Y, Ndata, "y");
|
||||
size_t columnYE=ds->addCopiedColumn(YERROR, Ndata, "y-error");
|
||||
|
||||
// 4. create a graph in the plot, which plots the dataset X/Y:
|
||||
JKQTPxyLineErrorGraph* graph1=new JKQTPxyLineErrorGraph(&plot);
|
||||
graph1->set_xColumn(columnX);
|
||||
graph1->set_yColumn(columnY);
|
||||
graph1->set_yErrorColumn(columnYE);
|
||||
graph1->set_symbol(JKQTPfilledStar); // set symbol style
|
||||
graph1->set_yErrorStyle(JKQTPerrorBars); // set error indicator type
|
||||
graph1->set_drawLine(false); // don't draw a line
|
||||
graph1->set_title(QObject::tr("sine graph"));
|
||||
|
||||
// 5. add the graph to the plot, so it is actually displayed
|
||||
plot.addGraph(graph1);
|
||||
|
||||
// 6. hide 0-lines
|
||||
plot.getXAxis()->set_showZeroAxis(false);
|
||||
plot.getYAxis()->set_showZeroAxis(false);
|
||||
|
||||
// 7. set some axis properties (we use LaTeX for nice equation rendering)
|
||||
plot.getXAxis()->set_axisLabel(QObject::tr("x-axis $x$ [mm]"));
|
||||
plot.getYAxis()->set_axisLabel(QObject::tr("\\textbf{\\color{red}{y-axis} $\\left(y=\\sin(x)\\pm(0.2+0.25\\cdot x)\\right)$ [A.U.]}"));
|
||||
plot.getXAxis()->set_labelFont("Arial");
|
||||
plot.getYAxis()->set_labelFont("Times New Roman");
|
||||
plot.getYAxis()->set_labelFontSize(12); // large x-axis label
|
||||
plot.getYAxis()->set_tickLabelFontSize(10); // and larger y-axis tick labels
|
||||
|
||||
// 8. autoscale the plot so the graph is contained
|
||||
plot.zoomToFit();
|
||||
|
||||
// show plotter and make it a decent size
|
||||
plot.show();
|
||||
plot.resize(600,400);
|
||||
|
||||
return app.exec();
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
# source code for this simple demo
|
||||
SOURCES = jkqtplotter_simpletest_symbols_and_errors.cpp
|
||||
|
||||
# configure Qt
|
||||
CONFIG += qt
|
||||
QT += core gui svg
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
|
||||
|
||||
# output executable name
|
||||
TARGET = jkqtplotter_simpletest_symbols_and_errors
|
||||
|
||||
# include JKQtPlotter source code
|
||||
include(../../jkqtplotter.pri)
|
||||
# here you can activate some debug options
|
||||
#DEFINES += SHOW_JKQTPLOTTER_DEBUG
|
||||
#DEFINES += JKQTBP_AUTOTIMER
|
Loading…
Reference in New Issue
Block a user