2019-01-20 23:15:10 +08:00
# Example (JKQTPlotter): Filled Curve Plots {#JKQTPlotterFilledGraphs}
2019-06-21 04:24:47 +08:00
This project (see `./examples/filledgraphs/` ) simply creates a JKQTPlotter widget (as a new window) and adds several filled curve graphs (Histograms). Data is initialized from QVector< int > objects.
2018-12-29 00:46:47 +08:00
2019-06-21 04:24:47 +08:00
The source code of the main application can be found in [`filledgraphs.cpp` ](https://github.com/jkriege2/JKQtPlotter/tree/master/examples/filledgraphs/filledgraphs.cpp ).
2018-12-29 00:46:47 +08:00
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).
2019-01-19 16:40:52 +08:00
```.cpp
2018-12-29 00:46:47 +08:00
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` :
2019-01-19 16:40:52 +08:00
```.cpp
2018-12-29 00:46:47 +08:00
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 order to calculate the histograms, first all enries in the columns are set to 0:
2019-01-19 16:40:52 +08:00
```.cpp
2019-05-13 04:22:48 +08:00
ds->setAll(columnG, 0);
ds->setAll(columnR, 0);
ds->setAll(columnB, 0);
2018-12-29 00:46:47 +08:00
```
Finally the histogram is calculated:
2019-01-19 16:40:52 +08:00
```.cpp
2018-12-29 00:46:47 +08:00
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);
2019-05-13 04:22:48 +08:00
ds->inc(columnR, qRed(pix), 1);
ds->inc(columnG, qGreen(pix), 1);
ds->inc(columnB, qBlue(pix), 1);
2018-12-29 00:46:47 +08:00
}
}
2019-05-13 04:22:48 +08:00
ds->scaleColumnValues(columnR, 100.0/static_cast< double > (image.width()*image.height()));
ds->scaleColumnValues(columnG, 100.0/static_cast< double > (image.width()*image.height()));
ds->scaleColumnValues(columnB, 100.0/static_cast< double > (image.width()*image.height()));
2018-12-29 00:46:47 +08:00
```
2019-01-20 17:49:29 +08:00
Finally three `JKQTPFilledCurveXGraph` objects are generated and added to the plot (here we show the code for the R-channel only):
2018-12-29 00:46:47 +08:00
2019-01-19 16:40:52 +08:00
```.cpp
2019-01-20 17:49:29 +08:00
JKQTPFilledCurveXGraph* graphR=new JKQTPFilledCurveXGraph(&plot);
2018-12-29 00:46:47 +08:00
// set graph titles
2019-01-26 20:00:40 +08:00
graphR->setTitle("R-channel");
2018-12-29 00:46:47 +08:00
// set graph colors (lines: non-transparent, fill: semi-transparent
QColor col;
2019-01-26 20:00:40 +08:00
col=QColor("red"); graphR->setColor(col);
col.setAlphaF(0.25); graphR->setFillColor(col);
2018-12-29 00:46:47 +08:00
// set data
2019-01-26 20:00:40 +08:00
graphR->setXColumn(columnX); graphR->setYColumn(columnR);
2018-12-29 00:46:47 +08:00
// 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:
2019-06-21 04:24:47 +08:00
![filledgraphs ](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/filledgraphs.png )
2018-12-29 00:46:47 +08:00
2019-01-20 17:49:29 +08:00
If you use `JKQTPFilledCurveYGraph` instead of `JKQTPFilledCurveXGraph` , the curve will not be filled until the y=0-axis, but until the x=0-axis. Of course you will also have to swap the x- and y-data columns. The result will look like this:
2018-12-29 00:46:47 +08:00
2019-06-21 04:24:47 +08:00
![filledgraphs_yaxis ](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/filledgraphs_yaxis.png )
2018-12-29 00:46:47 +08:00
2019-01-13 01:53:16 +08:00