2020-09-17 22:59:57 +08:00
|
|
|
/*
|
2022-07-19 19:40:43 +08:00
|
|
|
Copyright (c) 2008-2022 Jan W. Krieger (<jan@jkrieger.de>)
|
2020-09-17 22:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This software is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License (LGPL) as published by
|
|
|
|
the Free Software Foundation, either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License (LGPL) for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License (LGPL)
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "jkqtplotter/graphs/jkqtpbarchartbase.h"
|
|
|
|
#include "jkqtplotter/jkqtpbaseplotter.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <iostream>
|
|
|
|
#include "jkqtplotter/jkqtptools.h"
|
|
|
|
#include "jkqtplotter/jkqtplotter.h"
|
|
|
|
|
|
|
|
#define SmallestGreaterZeroCompare_xvsgz() if ((xvsgz>10.0*DBL_MIN)&&((smallestGreaterZero<10.0*DBL_MIN) || (xvsgz<smallestGreaterZero))) smallestGreaterZero=xvsgz;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTPBarGraphBase::JKQTPBarGraphBase(JKQTBasePlotter* parent):
|
2022-10-22 22:40:44 +08:00
|
|
|
JKQTPXYBaselineGraph(parent),
|
|
|
|
width(0.9), shift(0),
|
|
|
|
m_fillMode(FillMode::SingleFilling),
|
2022-10-29 03:48:42 +08:00
|
|
|
m_useCustomDrawFunctor(false),
|
2022-10-22 22:40:44 +08:00
|
|
|
m_lineColorDerivationModeForSpecialFill(parent->getCurrentPlotterStyle().graphsStyle.barchartStyle.graphColorDerivationMode),
|
|
|
|
rectRadiusAtBaseline(0),rectRadiusAtValue(0)
|
2020-09-17 22:59:57 +08:00
|
|
|
{
|
2020-09-26 21:58:58 +08:00
|
|
|
initFillStyle(parent, parentPlotStyle, JKQTPPlotStyleType::Barchart);
|
|
|
|
initLineStyle(parent, parentPlotStyle, JKQTPPlotStyleType::Barchart);
|
2022-09-10 20:35:16 +08:00
|
|
|
m_fillStyleBelow.initFillStyleInvertedColor(this);
|
2022-09-27 07:42:54 +08:00
|
|
|
rectRadiusAtBaseline= parent->getCurrentPlotterStyle().graphsStyle.barchartStyle.defaultRectRadiusAtBaseline;
|
|
|
|
rectRadiusAtValue= parent->getCurrentPlotterStyle().graphsStyle.barchartStyle.defaultRectRadiusAtValue;
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JKQTPBarGraphBase::JKQTPBarGraphBase(JKQTPlotter* parent):
|
|
|
|
JKQTPBarGraphBase(parent->getPlotter())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::drawKeyMarker(JKQTPEnhancedPainter& painter, QRectF& rect) {
|
|
|
|
painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();});
|
|
|
|
QPen p=getLinePenForRects(painter, parent);
|
|
|
|
QBrush b=getFillBrush(painter, parent);
|
|
|
|
//int y=rect.top()+rect.height()/2.0;
|
|
|
|
painter.setPen(p);
|
|
|
|
painter.setBrush(b);
|
|
|
|
painter.drawRect(rect);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor JKQTPBarGraphBase::getKeyLabelColor() const {
|
|
|
|
return getFillColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::autoscaleBarWidthAndShift(double maxWidth, double shrinkFactor)
|
|
|
|
{
|
|
|
|
if (parent) {
|
|
|
|
double cntH=0;
|
|
|
|
for (size_t i=0; i<parent->getGraphCount(); i++) {
|
|
|
|
JKQTPPlotElement* g=parent->getGraph(i);
|
|
|
|
JKQTPBarGraphBase* gb=qobject_cast<JKQTPBarGraphBase*>(g);
|
|
|
|
if (gb && considerForAutoscaling(gb)) {
|
|
|
|
cntH++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
double widthH=1.0/cntH*maxWidth*shrinkFactor;
|
|
|
|
double dH=maxWidth/(cntH);
|
|
|
|
double h=0.1+dH/2.0;
|
|
|
|
for (size_t i=0; i<parent->getGraphCount(); i++) {
|
|
|
|
JKQTPPlotElement* g=parent->getGraph(i);
|
|
|
|
JKQTPBarGraphBase* gb=qobject_cast<JKQTPBarGraphBase*>(g);
|
|
|
|
if (gb && considerForAutoscaling(gb)) {
|
|
|
|
if (cntH>1) {
|
|
|
|
gb->width=widthH;
|
|
|
|
gb->shift=h-0.5;
|
|
|
|
h=h+dH;
|
|
|
|
} else {
|
|
|
|
gb->width=maxWidth;
|
|
|
|
gb->shift=0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::autoscaleBarWidthAndShiftSeparatedGroups(double groupWidth) {
|
|
|
|
autoscaleBarWidthAndShift(groupWidth, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setColor(QColor c)
|
|
|
|
{
|
2022-10-22 22:40:44 +08:00
|
|
|
setFillColor(c);
|
|
|
|
setLineColor(JKQTPGetDerivedColor(parent->getCurrentPlotterStyle().graphsStyle.barchartStyle.graphColorDerivationMode, c));
|
2020-09-17 22:59:57 +08:00
|
|
|
c.setAlphaF(0.5);
|
|
|
|
setHighlightingLineColor(c);
|
2022-09-10 20:35:16 +08:00
|
|
|
m_fillStyleBelow.initFillStyleInvertedColor(this);
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
|
|
|
|
2022-10-22 22:40:44 +08:00
|
|
|
void JKQTPBarGraphBase::setBarPositionColumn(int column)
|
|
|
|
{
|
|
|
|
setKeyColumn(column);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setBarPositionColumn(size_t column)
|
|
|
|
{
|
|
|
|
setKeyColumn(static_cast<int>(column));
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setBarHeightColumn(int column)
|
|
|
|
{
|
|
|
|
setValueColumn(column);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setBarHeightColumn(size_t column)
|
|
|
|
{
|
|
|
|
setValueColumn(static_cast<int>(column));
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTPBarGraphBase::FillBrushFunctor JKQTPBarGraphBase::constructFillBrushFunctor() const
|
|
|
|
{
|
|
|
|
if (m_fillMode==FillMode::FunctorFilling) return m_fillBrushFunctor;
|
|
|
|
if (m_fillMode==FillMode::TwoColorFilling) return [](double , double value, JKQTPEnhancedPainter &painter, JKQTPBarGraphBase* graph) {
|
|
|
|
if (value<graph->getBaseline()) return graph->fillStyleBelow().getFillBrush(painter, graph->getParent());
|
|
|
|
return graph->getFillBrush(painter, graph->getParent());
|
|
|
|
};
|
|
|
|
else return [](double, double, JKQTPEnhancedPainter &painter, JKQTPBarGraphBase* graph) {
|
|
|
|
return graph->getFillBrush(painter, graph->getParent());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:59:57 +08:00
|
|
|
void JKQTPBarGraphBase::setShift(double __value)
|
|
|
|
{
|
|
|
|
this->shift = __value;
|
|
|
|
}
|
|
|
|
|
|
|
|
double JKQTPBarGraphBase::getShift() const
|
|
|
|
{
|
|
|
|
return this->shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setWidth(double __value)
|
|
|
|
{
|
|
|
|
this->width = __value;
|
|
|
|
}
|
|
|
|
|
|
|
|
double JKQTPBarGraphBase::getWidth() const
|
|
|
|
{
|
|
|
|
return this->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setFillColor_and_darkenedColor(QColor fill, int colorDarker)
|
|
|
|
{
|
|
|
|
setFillColor(fill);
|
|
|
|
setLineColor(fill.darker(colorDarker));
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:40:44 +08:00
|
|
|
int JKQTPBarGraphBase::getBarPositionColumn() const
|
|
|
|
{
|
|
|
|
return getKeyColumn();
|
|
|
|
}
|
|
|
|
|
|
|
|
int JKQTPBarGraphBase::getBarHeightColumn() const
|
|
|
|
{
|
|
|
|
return getValueColumn();
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:35:16 +08:00
|
|
|
JKQTPGraphFillStyleMixin &JKQTPBarGraphBase::fillStyleBelow()
|
|
|
|
{
|
|
|
|
return m_fillStyleBelow;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JKQTPGraphFillStyleMixin &JKQTPBarGraphBase::fillStyleBelow() const
|
|
|
|
{
|
|
|
|
return m_fillStyleBelow;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTPBarGraphBase::FillMode JKQTPBarGraphBase::getFillMode() const
|
|
|
|
{
|
|
|
|
return m_fillMode;
|
|
|
|
}
|
|
|
|
|
2022-09-27 07:42:54 +08:00
|
|
|
double JKQTPBarGraphBase::getRectRadiusAtValue() const
|
|
|
|
{
|
|
|
|
return rectRadiusAtValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
double JKQTPBarGraphBase::getRectRadiusAtBaseline() const
|
|
|
|
{
|
|
|
|
return rectRadiusAtBaseline;
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:40:44 +08:00
|
|
|
JKQTPBarGraphBase::FillBrushFunctor &JKQTPBarGraphBase::getFillBrushFunctor() {
|
|
|
|
return m_fillBrushFunctor;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JKQTPBarGraphBase::FillBrushFunctor &JKQTPBarGraphBase::getFillBrushFunctor() const {
|
|
|
|
return m_fillBrushFunctor;
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:35:16 +08:00
|
|
|
void JKQTPBarGraphBase::setFillMode(FillMode mode)
|
|
|
|
{
|
|
|
|
m_fillMode=mode;
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:40:44 +08:00
|
|
|
void JKQTPBarGraphBase::setFillBrushFunctor(const FillBrushFunctor &f) {
|
|
|
|
m_fillBrushFunctor=f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setFillBrushFunctor(FillBrushFunctor &&f) {
|
|
|
|
m_fillBrushFunctor=std::forward<FillBrushFunctor>(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setFillBrushFunctor(const SimpleFillBrushFunctor &f) {
|
|
|
|
m_fillBrushFunctor=SimpleFillBrushFunctorAdaptor(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setFillBrushFunctor(SimpleFillBrushFunctor &&f) {
|
|
|
|
m_fillBrushFunctor=SimpleFillBrushFunctorAdaptor(std::forward<SimpleFillBrushFunctor>(f));
|
|
|
|
}
|
|
|
|
|
2022-10-29 03:48:42 +08:00
|
|
|
void JKQTPBarGraphBase::setCustomDrawingFunctor(CustomDrawingFunctor &&f)
|
|
|
|
{
|
|
|
|
m_customDrawFunctor=std::forward<CustomDrawingFunctor>(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setCustomDrawingFunctor(const CustomDrawingFunctor &f)
|
|
|
|
{
|
|
|
|
m_customDrawFunctor=f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setUseCustomDrawFunctor(bool enabled)
|
|
|
|
{
|
|
|
|
m_useCustomDrawFunctor=enabled;
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:59:57 +08:00
|
|
|
double JKQTPBarGraphBase::getParentStackedMax(int /*index*/) const
|
|
|
|
{
|
2020-09-19 21:08:32 +08:00
|
|
|
return getBaseline();
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JKQTPBarGraphBase::hasStackParent() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JKQTPBarGraphBase::getValuesMinMax(double &mmin, double &mmax, double &smallestGreaterZero)
|
|
|
|
{
|
|
|
|
mmin=0;
|
|
|
|
mmax=0;
|
|
|
|
smallestGreaterZero=0;
|
2020-09-19 21:08:32 +08:00
|
|
|
if (getBaseline()>0) {
|
|
|
|
smallestGreaterZero=getBaseline();
|
|
|
|
mmin=getBaseline();
|
|
|
|
mmax=getBaseline();
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getBarPositionColumn()<0 || getBarHeightColumn()<0) return false;
|
|
|
|
|
|
|
|
const size_t datacol=static_cast<size_t>(getBarHeightColumn());
|
|
|
|
|
|
|
|
if (parent==nullptr) return false;
|
|
|
|
|
2020-09-19 04:03:12 +08:00
|
|
|
const JKQTPDatastore* datastore=parent->getDatastore();
|
|
|
|
int imin=0, imax=0;
|
|
|
|
if (getIndexRange(imin, imax)) {
|
|
|
|
|
|
|
|
|
|
|
|
for (int i=imin; i<imax; i++) {
|
|
|
|
double stack=0;
|
2020-09-19 21:08:32 +08:00
|
|
|
double yv=getBaseline();
|
2020-09-19 04:03:12 +08:00
|
|
|
const double boxstart=getParentStackedMax(i);
|
|
|
|
if (hasStackParent()) {
|
|
|
|
stack=boxstart;
|
|
|
|
yv=stack;
|
|
|
|
}
|
|
|
|
if (JKQTPIsOKFloat(yv)) {
|
|
|
|
if (yv>mmax) mmax=yv;
|
|
|
|
if (yv<mmin) mmin=yv;
|
|
|
|
double xvsgz;
|
|
|
|
xvsgz=yv; SmallestGreaterZeroCompare_xvsgz();
|
|
|
|
}
|
|
|
|
yv=stack+datastore->get(datacol,static_cast<size_t>(i));
|
|
|
|
if (JKQTPIsOKFloat(yv)) {
|
|
|
|
if (yv>mmax) mmax=yv;
|
|
|
|
if (yv<mmin) mmin=yv;
|
|
|
|
double xvsgz;
|
|
|
|
xvsgz=yv; SmallestGreaterZeroCompare_xvsgz();
|
|
|
|
}
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
2020-09-19 04:03:12 +08:00
|
|
|
return true;
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
2020-09-19 04:03:12 +08:00
|
|
|
return false;
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JKQTPBarGraphBase::getPositionsMinMax(double &mmin, double &mmax, double &smallestGreaterZero)
|
|
|
|
{
|
|
|
|
bool start=true;
|
|
|
|
mmin=0;
|
|
|
|
mmax=0;
|
|
|
|
smallestGreaterZero=0;
|
|
|
|
|
|
|
|
if (getBarPositionColumn()<0 || getBarHeightColumn()<0) return false;
|
|
|
|
|
|
|
|
const size_t poscol=static_cast<size_t>(getBarPositionColumn());
|
|
|
|
|
|
|
|
if (parent==nullptr) return false;
|
|
|
|
|
2020-09-19 04:03:12 +08:00
|
|
|
const JKQTPDatastore* datastore=parent->getDatastore();
|
|
|
|
int imin=0, imax=0;
|
|
|
|
if (getIndexRange(imin, imax)) {
|
|
|
|
for (int i=imin; i<imax; i++) {
|
|
|
|
double xv=datastore->get(poscol,static_cast<size_t>(i));
|
|
|
|
int sr=datastore->getNextLowerIndex(poscol, i);
|
|
|
|
int lr=datastore->getNextHigherIndex(poscol, i);
|
|
|
|
double delta, deltap, deltam;
|
|
|
|
|
|
|
|
if (sr<0 && lr<0) { // only one x-value
|
|
|
|
deltam=0.5;
|
|
|
|
deltap=0.5;
|
|
|
|
} else if (lr<0) { // the right-most x-value
|
|
|
|
deltap=deltam=fabs(xv-datastore->get(poscol,sr))/2.0;
|
|
|
|
} else if (sr<0) { // the left-most x-value
|
|
|
|
deltam=deltap=fabs(datastore->get(poscol,lr)-xv)/2.0;
|
|
|
|
} else {
|
|
|
|
deltam=fabs(xv-datastore->get(poscol,sr))/2.0;
|
|
|
|
deltap=fabs(datastore->get(poscol,lr)-xv)/2.0;
|
|
|
|
}
|
|
|
|
delta=deltap+deltam;
|
2020-09-17 22:59:57 +08:00
|
|
|
|
2020-09-19 04:03:12 +08:00
|
|
|
if (JKQTPIsOKFloat(xv) && JKQTPIsOKFloat(delta) ) {
|
2020-09-17 22:59:57 +08:00
|
|
|
|
2020-09-19 04:03:12 +08:00
|
|
|
if (start || xv+shift*delta+width*delta/2.0>mmax) mmax=xv+shift*delta+width*delta/2.0;
|
|
|
|
if (start || xv+shift*delta-width*delta/2.0<mmin) mmin=xv+shift*delta-width*delta/2.0;
|
|
|
|
double xvsgz;
|
|
|
|
xvsgz=xv+shift*delta+width*delta/2.0; SmallestGreaterZeroCompare_xvsgz();
|
|
|
|
xvsgz=xv+shift*delta-width*delta/2.0; SmallestGreaterZeroCompare_xvsgz();
|
|
|
|
start=false;
|
|
|
|
}
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
2020-09-19 04:03:12 +08:00
|
|
|
return !start;
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
2020-09-19 04:03:12 +08:00
|
|
|
return false;
|
2020-09-17 22:59:57 +08:00
|
|
|
}
|
|
|
|
|
2022-09-27 07:42:54 +08:00
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setRectRadiusAtValue(double __value)
|
|
|
|
{
|
|
|
|
rectRadiusAtValue=__value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setRectRadiusAtBaseline(double __value)
|
|
|
|
{
|
|
|
|
rectRadiusAtBaseline=__value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setRectRadius(double all)
|
|
|
|
{
|
|
|
|
setRectRadiusAtValue(all);
|
|
|
|
setRectRadiusAtBaseline(all);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setRectRadius(double atValue, double atBaseline)
|
|
|
|
{
|
|
|
|
setRectRadiusAtValue(atValue);
|
|
|
|
setRectRadiusAtBaseline(atBaseline);
|
|
|
|
}
|
2022-10-22 22:40:44 +08:00
|
|
|
|
|
|
|
void JKQTPBarGraphBase::setLineColorDerivationModeForSpecialFill(const JKQTPColorDerivationMode &m)
|
|
|
|
{
|
|
|
|
m_lineColorDerivationModeForSpecialFill=m;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTPColorDerivationMode JKQTPBarGraphBase::getLineColorDerivationModeForSpecialFill() const
|
|
|
|
{
|
|
|
|
return m_lineColorDerivationModeForSpecialFill;
|
|
|
|
}
|
2022-10-29 03:48:42 +08:00
|
|
|
|
|
|
|
bool JKQTPBarGraphBase::usesCustomDrawFunctor() const
|
|
|
|
{
|
|
|
|
return m_useCustomDrawFunctor;
|
|
|
|
}
|