improved data-access via JKQTPcolumn

added example for filled curves and data-access via JKQTPcolumn
This commit is contained in:
jkriege2 2018-12-10 23:57:20 +01:00
parent 010f513b31
commit 49bac25809
12 changed files with 273 additions and 20 deletions

View File

@ -42,6 +42,7 @@ addSimpleTest(logaxes)
addSimpleTest(stackedbars)
addSimpleTest(symbols_and_errors)
addSimpleTest(symbols_and_styles)
addSimpleTest(filledgraphs)
addSimpleTest(speed)
addSimpleTest(rgbimageplot_qt)
#addSimpleTest(imageplot_nodatastore)

View File

@ -25,6 +25,7 @@ All test-projects are Qt-projects that use qmake to build. You can load them int
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_errorbarstyles_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_errorbarstyles) | [Different Types of Error Indicators](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_errorbarstyles) | `JKQTPxyLineErrorGraph`<br/>different styles of error indicators for x- and y-errors<br>C++-style QVector for data<br/>styling error indicators<br/>moving key and formatting plotter grid |
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_barchart_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_barchart) | [Simple Bar Charts](https://github.com/jkriege2/JKQtPlotter/tree/master/test/simpletest_barchart) | `JKQTPbarVerticalGraph`<br/>C-style arrays of data |
| [![](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 |
### Styling the Plot, Keys, Axes, ...

View File

@ -86,13 +86,6 @@ QVector<double> JKQTPcolumn::copyData()
return d;
}
////////////////////////////////////////////////////////////////////////////////////////////////
double JKQTPcolumn::getValue(size_t n) const
{
if (!datastore) return 0;
if (!datastore->getItem(datastoreItem)) return 0;
return datastore->getItem(datastoreItem)->get(datastoreOffset, n);
}
////////////////////////////////////////////////////////////////////////////////////////////////
double *JKQTPcolumn::getPointer(size_t n) const
@ -102,15 +95,6 @@ double *JKQTPcolumn::getPointer(size_t n) const
return datastore->getItem(datastoreItem)->getPointer(datastoreOffset, n);
}
////////////////////////////////////////////////////////////////////////////////////////////////
void JKQTPcolumn::setValue(size_t n, double val)
{
if (!datastore) return ;
if (!datastore->getItem(datastoreItem)) return ;
datastore->getItem(datastoreItem)->set(datastoreOffset, n, val);
}
////////////////////////////////////////////////////////////////////////////////////////////////
void JKQTPcolumn::copy(double* data, size_t N, size_t offset) {
@ -159,6 +143,19 @@ void JKQTPcolumn::scale(double factor)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
void JKQTPcolumn::setAll(double value)
{
if (!datastore) return ;
double* data=getPointer();
size_t N=getRows();
if (data){
for (size_t i=0; i<N; i++) {
data[i]=value;
}
}
}
/**************************************************************************************************************************
* JKQTPdatastoreItem

View File

@ -627,17 +627,30 @@ class LIB_EXPORT JKQTPcolumn {
* This method accesses the datastore and returns the double value stored in the \a n'th row of the according
* column.
*/
double getValue(size_t n) const;
inline double getValue(size_t n) const;
/** \brief gets a pointer to the n-th value in the column
*/
double* getPointer(size_t n=0) const ;
/** \brief sets the \a n'th value from the column
*
* This method accesses the datastore and returns the double value stored in the \a n'th row of the according
* This method accesses the datastore and sets the value stored in the \a n'th row of the according
* column.
*/
void setValue(size_t n, double val);
inline void setValue(size_t n, double val);
/** \brief increment the \a n'th value from the column
*
*/
inline void incValue(size_t n, double increment=1.0);
/** \brief decrement the \a n'th value from the column
*
*/
inline void decValue(size_t n, double decrement=1.0) {
incValue(n, -1.0*decrement);
}
/** \brief sets the element at (x,y) in the column, where the data is interpreted as a row-major ordered Matrix of the given width
*
@ -658,7 +671,7 @@ class LIB_EXPORT JKQTPcolumn {
*/
void copy(double* data, size_t N, size_t offset=0);
/** \brief exchange everz occurence of a given \a value by a \a replace value */
/** \brief exchange every occurence of a given \a value by a \a replace value */
void exchange(double value, double replace);
/** \brief subtracts a given value from all members of the column */
@ -666,6 +679,9 @@ class LIB_EXPORT JKQTPcolumn {
/** \brief scales all members of the column with the given factor */
void scale(double factor);
/** \brief set all values in the column to a specific \a value */
void setAll(double value);
JKQTPGET_MACRO(size_t, datastoreItem)
JKQTPGET_MACRO(size_t, datastoreOffset)
};
@ -796,4 +812,28 @@ class LIB_EXPORT JKQTPdatastoreModel: public QAbstractTableModel {
};
////////////////////////////////////////////////////////////////////////////////////////////////
inline void JKQTPcolumn::setValue(size_t n, double val){
if (!datastore) return ;
if (!datastore->getItem(datastoreItem)) return ;
datastore->getItem(datastoreItem)->set(datastoreOffset, n, val);
}
////////////////////////////////////////////////////////////////////////////////////////////////
inline void JKQTPcolumn::incValue(size_t n, double increment){
if (!datastore) return ;
if (!datastore->getItem(datastoreItem)) return ;
datastore->getItem(datastoreItem)->set(datastoreOffset, n, datastore->getItem(datastoreItem)->get(datastoreOffset, n)+increment);
}
////////////////////////////////////////////////////////////////////////////////////////////////
inline double JKQTPcolumn::getValue(size_t n) const {
if (!datastore) return 0;
if (!datastore->getItem(datastoreItem)) return 0;
return datastore->getItem(datastoreItem)->get(datastoreOffset, n);
}
#endif // JKQTPDATASTORAGE_H

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -0,0 +1,79 @@
[Back to JKQTPlotter main page](https://github.com/jkriege2/JKQtPlotter/)
# JKQtPlotter
## Filled Curve Plots
This project (see `./test/simpletest_filledgraphs/`) simply creates a JKQtPlotter widget (as a new window) and adds several filled curve graphs (Histograms). Data is initialized from QVector<int> objects.
The source code of the main application can be found in [`jkqtplotter_simpletest_filledgraphs.cpp`](https://github.com/jkriege2/JKQtPlotter/blob/master/test/simpletest_filledgraphs/jkqtplotter_simpletest_filledgraphs.cpp).
First the data columns for three x-y-curves are generated. One column of x-values with entries 0,1,2,...,254,255 (256 entries).
```c++
size_t columnX=ds->addLinearColumn(256, 0, 255, "x");
```
And three columns with 256 entries each, which will be filled with the R-, G- and B-histograms of an image `example.bmp`:
```c++
size_t columnR=ds->addColumn(256, "historam_R");
size_t columnG=ds->addColumn(256, "historam_G");
size_t columnB=ds->addColumn(256, "historam_B");
```
In this example we will access the data in the internal datastore directly. This access is possible through objects of type JKQTPcolumn, which is a proxy to the data in one of the columns in a `JKQTdatastore`:
```c++
JKQTPcolumn cG=ds->getColumn(columnG);
JKQTPcolumn cR=ds->getColumn(columnR);
JKQTPcolumn cB=ds->getColumn(columnB);
```c++
In order to calculate the histograms, first all enries in the columns are set to 0:
```c++
cR.setAll(0);
cG.setAll(0);
cB.setAll(0);
```
Finally the histogram is calculated:
```c++
QImage image(":/example.bmp");
for (int y=0; y<image.height(); y++) {
for (int x=0; x<image.width(); x++) {
QRgb pix=image.pixel(x,y);
cR.incValue(qRed(pix), 1);
cG.incValue(qGreen(pix), 1);
cB.incValue(qBlue(pix), 1);
}
}
cR.scale(100.0/static_cast<double>(image.width()*image.height()));
cG.scale(100.0/static_cast<double>(image.width()*image.height()));
cB.scale(100.0/static_cast<double>(image.width()*image.height()));
```
Finally three `JKQTPfilledCurveXGraph` objects are generated and added to the plot (here we show the code for the R-channel only):
```c++
JKQTPfilledCurveXGraph* graphR=new JKQTPfilledCurveXGraph(&plot);
// set graph titles
graphR->set_title("R-channel");
// set graph colors (lines: non-transparent, fill: semi-transparent
QColor col;
col=QColor("red"); graphR->set_color(col);
col.setAlphaF(0.25); graphR->set_fillColor(col);
// set data
graphR->set_xColumn(columnX); graphR->set_yColumn(columnR);
// add the graphs to the plot, so they are actually displayed
plot.addGraph(graphR);
```
The curves are fille with a semi-transparent color, which is achieved by setting `col.setAlphaF(0.25)` on the graph color `col`.
The result looks like this:
![jkqtplotter_simpletest_filledgraphs](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_filledgraphs.png)
[Back to JKQTPlotter main page](https://github.com/jkriege2/JKQtPlotter/)

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

View File

@ -0,0 +1,98 @@
#include <QApplication>
#include "jkqtplotter/jkqtplotter.h"
#include "jkqtplotter/jkqtpfilledcurveelements.h"
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 4 datacolumns with length 256 entries in the datastore
// these will later hold the RGB-histogram and a linear x-values vector
// - the x-values are directly initialized as linear vector 0..255 in 256 steps
// - the other columns are generated and size_t-type indexes are stored for later
// reference to these columns in the graphs-
size_t columnX=ds->addLinearColumn(256, 0, 255, "x");
size_t columnR=ds->addColumn(256, "historam_R");
size_t columnG=ds->addColumn(256, "historam_G");
size_t columnB=ds->addColumn(256, "historam_B");
// - in addition JKQTPcolumn objects are generated, which can be used to access
// the data in the columns
JKQTPcolumn cG=ds->getColumn(columnG);
JKQTPcolumn cR=ds->getColumn(columnR);
JKQTPcolumn cB=ds->getColumn(columnB);
// - now all columns for RGB are initialized to 0
cR.setAll(0);
cG.setAll(0);
cB.setAll(0);
// 3. now we open a BMP-file and load it into a QImage
QImage image(":/example.bmp");
// ... and calculate the RGB-histograms
for (int y=0; y<image.height(); y++) {
for (int x=0; x<image.width(); x++) {
QRgb pix=image.pixel(x,y);
cR.incValue(qRed(pix), 1);
cG.incValue(qGreen(pix), 1);
cB.incValue(qBlue(pix), 1);
}
}
// ... and normalize histograms
cR.scale(100.0/static_cast<double>(image.width()*image.height()));
cG.scale(100.0/static_cast<double>(image.width()*image.height()));
cB.scale(100.0/static_cast<double>(image.width()*image.height()));
// 4. now we add three semi-transparent, filled curve plots, one for each histogram
JKQTPfilledCurveXGraph* graphR=new JKQTPfilledCurveXGraph(&plot);
JKQTPfilledCurveXGraph* graphG=new JKQTPfilledCurveXGraph(&plot);
JKQTPfilledCurveXGraph* graphB=new JKQTPfilledCurveXGraph(&plot);
// set graph titles
graphR->set_title("R-channel");
graphG->set_title("G-channel");
graphB->set_title("B-channel");
// set graph colors (lines: non-transparent, fill: semi-transparent) and style
QColor col;
col=QColor("red"); graphR->set_color(col);
col.setAlphaF(0.25); graphR->set_fillColor(col);
col=QColor("green"); graphG->set_color(col);
col.setAlphaF(0.25); graphG->set_fillColor(col);
col=QColor("blue"); graphB->set_color(col);
col.setAlphaF(0.25); graphB->set_fillColor(col);
graphR->set_lineWidth(1);
graphG->set_lineWidth(1);
graphB->set_lineWidth(1);
// set data
graphR->set_xColumn(columnX); graphR->set_yColumn(columnR);
graphG->set_xColumn(columnX); graphG->set_yColumn(columnG);
graphB->set_xColumn(columnX); graphB->set_yColumn(columnB);
// add the graphs to the plot, so they are actually displayed
plot.addGraph(graphB);
plot.addGraph(graphG);
plot.addGraph(graphR);
// 5. set axis labels
plot.getXAxis()->set_axisLabel("R/G/B-value");
plot.getYAxis()->set_axisLabel("normalized frequency [%]");
// 4. set the maximum size of the plot to 0..100% and 0..256
plot.setAbsoluteX(0,256);
plot.setAbsoluteY(0,100);
// ... and scale plot automatically
plot.zoomToFit();
// 5. show plotter and make it a decent size
plot.show();
plot.resize(600,400);
return app.exec();
}

View File

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

View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>example.bmp</file>
</qresource>
</RCC>

View File

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