JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
Example (JKQTPlotter): Barchart With Custom Draw Functor

This project (see barchart_customdrawfunctor shows how to draw customized barcharts, with a custom draw functor.

The source code of the main application is (see barchart_customdrawfunctor.cpp:

// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
JKQTPDatastore* ds=plot.getDatastore();
// 2. now we create two columns for key and value
size_t columnK=ds->addLinearColumn(7, 0, 6, "k");
size_t columnV=ds->addColumnCalculatedFromColumn(columnK, [](double x) { return jkqtp_sign(2.0*x-2.5)*qMax(2.0,fabs(2.0*x-2.5)); }, "v");
// 3. load a stylesheet
plot.loadCurrentPlotterStyle(QSettings(":/JKQTPlotter/styles/seaborn.ini", QSettings::IniFormat));
// 4. create graph in the plot, which plots the dataset:
graph->setKeyColumn(columnK);
graph->setValueColumn(columnV);
// this is the custom draw functor
[](JKQTPEnhancedPainter& painter, const QRectF& bar_px, const QPointF& datapoint, Qt::Orientation orientation, JKQTPBarGraphBase* graph) {
painter.save();
painter.setPen(Qt::NoPen);
QBrush b=painter.brush();
QBrush bDeco=b;
bDeco.setColor(b.color().lighter());
// we draw the bar with a rounded end and add a lightly colored circle near the top
if (orientation==Qt::Vertical) {
// for vertical barcharts
const double dx=bar_px.width()/2.0;
const double r=dx*0.85;
// depending in whether the bar elongates above or below the baseline,
// we have to align the circle wrt the top or the bottom of the rectangle bar_px
if (datapoint.y()>=graph->getBaseline()) {
painter.drawComplexRoundedRect(bar_px, dx, dx, 0, 0, Qt::AbsoluteSize);
painter.setBrush(bDeco);
painter.drawEllipse(QPointF(bar_px.center().x(), bar_px.top()+dx), r,r);
} else {
painter.drawComplexRoundedRect(bar_px, 0, 0, dx, dx, Qt::AbsoluteSize);
painter.setBrush(bDeco);
painter.drawEllipse(QPointF(bar_px.center().x(), bar_px.bottom()-dx), r,r);
}
} else {
// for horizontal barcharts
const double dx=bar_px.height()/2.0;
const double r=dx*0.85;
if (datapoint.x()>=graph->getBaseline()) {
painter.drawComplexRoundedRect(bar_px, 0, dx, 0, dx, Qt::AbsoluteSize);
painter.setBrush(bDeco);
painter.drawEllipse(QPointF(bar_px.right()-dx, bar_px.center().y()), r,r);
} else {
painter.drawComplexRoundedRect(bar_px, dx,0,dx,0, Qt::AbsoluteSize);
painter.setBrush(bDeco);
painter.drawEllipse(QPointF(bar_px.left()+dx, bar_px.center().y()), r,r);
}
}
painter.restore();
}
);
// enable usage of cutom draw functor
// set graph color
graph->setColor(QColor("blue"));
plot.addGraph(graph);
// 5. autoscale the plot so the graph is contained
plot.zoomToFit();
// 6. show plotter and make it a decent size
plot.setWindowTitle(title);
plot.show();
plot.resize(400,400);
This is a base-class for all bar graphs with vertical or horizontal orientation (the orientation is i...
Definition jkqtpbarchartbase.h:152
void setCustomDrawingFunctor(JKQTPBarGraphBase::CustomDrawingFunctor &&f)
this allows to provide custom drawing code for the bars. It is called for every visible bar if activa...
virtual void setColor(QColor c)
set outline and fill color at the same time
void setUseCustomDrawFunctor(bool enabled)
enabled custom drawing by m_customDrawFunctor
This implements a vertical bar graph with bars between and .
Definition jkqtpbarchart.h:51
This class manages data columns (with entries of type double ), used by JKQTPlotter/JKQTBasePlotter t...
Definition jkqtpdatastorage.h:282
size_t addLinearColumn(size_t rows, double start, double end, const QString &name=QString(""))
add a column to the datastore that contains rows rows with monotonely increasing value starting at st...
size_t addColumnCalculatedFromColumn(size_t otherColumn, const std::function< double(double)> &f, const QString &name=QString(""))
Definition jkqtpdatastorage.h:1333
this class extends the QPainter
Definition jkqtpenhancedpainter.h:33
void drawComplexRoundedRect(const QRectF &r, double rTopLeft, double rTopRight, double rBottomLeft, double rBottomRight, Qt::SizeMode mode=Qt::AbsoluteSize)
draw a rounded rect, where each corner has a separate radius
double getBaseline() const
baseline of the plot (NOTE: 0 is interpreted as until plot border in log-mode!!!)
virtual void setValueColumn(int __value)
sets the column used as "value" for the current graph (typically this call setXColumn(),...
virtual void setKeyColumn(int __value)
sets the column used as "key" for the current graph (typically this call setXColumn(),...
T jkqtp_sign(T x)
calculates the sign of number x (-1 for x<0 and +1 for x>=0)
Definition jkqtpmathtools.h:366

The result looks like this:

barchart_customdrawfunctor

In order to draw horizontal error bars, you have to use JKQTPBarHorizontalGraph instead of JKQTPBarVerticalGraph. The functor given above also covers that case by checking the parameter orientation:

barchart_customdrawfunctor_hor