/* Copyright (c) 2008-2024 Jan W. Krieger () 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 . */ #include "jkqtplotter/jkqtpgraphsbase.h" #include "jkqtplotter/jkqtpbaseplotter.h" #include #include #include #include "jkqtplotter/jkqtptools.h" #include "jkqtplotter/graphs/jkqtpimage.h" #include "jkqtplotter/jkqtpbaseelements.h" #include "jkqtplotter/jkqtplotter.h" #include "jkqtpgraphsbase.h" #define SmallestGreaterZeroCompare_xvsgz() if ((xvsgz>10.0*DBL_MIN)&&((smallestGreaterZero<10.0*DBL_MIN) || (xvsgzgetKeyBackgroundColor()); { JKQTPEnhancedPainter painter(&img); painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::TextAntialiasing, true); painter.setRenderHint(QPainter::SmoothPixmapTransform, true); #if (QT_VERSIONtitle = __value; } QString JKQTPPlotElement::getTitle() const { return this->title; } void JKQTPPlotElement::setVisible(bool __value) { this->visible = __value; } bool JKQTPPlotElement::isVisible() const { return this->visible; } void JKQTPPlotElement::setHighlighted(bool __value) { highlighted=__value; } bool JKQTPPlotElement::isHighlighted() const { return highlighted; } void JKQTPPlotElement::setParent(JKQTBasePlotter* parent) { this->parent=parent; QObject::setParent(parent); } void JKQTPPlotElement::setParent(JKQTPlotter *parent) { setParent(parent->getPlotter()); } bool JKQTPGraph::getDataMinMax(int column, double &minx, double &maxx, double &smallestGreaterZero) { bool start=true; minx=0; maxx=0; smallestGreaterZero=0; if (parent==nullptr) return false; const JKQTPDatastore* datastore=parent->getDatastore(); int imin=0; int imax=static_cast(datastore->getRows(column)); if (imin<0) imin=0; if (imax<0) imax=0; for (int i=imin; iget(column,static_cast(i)); if (start || xv>maxx) maxx=xv; if (start || xv(this); QString xerrstr; // retrieve x-error data if (errgx && datastore) { if (errgx->getXErrorColumn()>=0) { if (errgx->getXErrorColumnLower()>=0) { xerrstr=QString("\\:+%1\\:-%2") .arg(xFloatToString(datastore->get(errgx->getXErrorColumn(),static_cast(index)))) .arg(xFloatToString(datastore->get(errgx->getXErrorColumnLower(),static_cast(index)))); } else { xerrstr=QString("{\\:}{\\pm}%1") .arg(xFloatToString(datastore->get(errgx->getXErrorColumn(),static_cast(index)))); } } } // retrieve y-error data const JKQTPYGraphErrorData* errgy=dynamic_cast(this); QString yerrstr; if (errgy && datastore) { if (errgy->getYErrorColumn()>=0) { if (errgy->getYErrorColumnLower()>=0) { yerrstr=QString("\\:+%1\\:-%2") .arg(yFloatToString(datastore->get(errgy->getYErrorColumn(),static_cast(index)))) .arg(yFloatToString(datastore->get(errgy->getYErrorColumnLower(),static_cast(index)))); } else { yerrstr=QString("{\\:}{\\pm}%1") .arg(yFloatToString(datastore->get(errgy->getYErrorColumn(),static_cast(index)))); } } } return QString("\\ensuremath{\\left[{\\:}%1%3{\\;},{\\;}%2%4{\\:}\\right]}").arg(xFloatToString(x)).arg(yFloatToString(y)).arg(xerrstr).arg(yerrstr); } QString JKQTPPlotElement::xFloatToString(double v, int past_comma) const { if (past_comma<0) return getXAxis()->floattolabel(v); else return getXAxis()->floattolabel(v, past_comma); } QString JKQTPPlotElement::yFloatToString(double v, int past_comma) const { if (past_comma<0) return getYAxis()->floattolabel(v); else return getYAxis()->floattolabel(v, past_comma); } double JKQTPPlotElement::hitTest(const QPointF & posSystem, QPointF* closestSpotSystem, QString* label, HitTestMode mode) const { if (parent==nullptr) return JKQTP_NAN; int closest=-1; double closedist=JKQTP_NAN; double closedistsec=JKQTP_NAN; QPointF closestPos; QPointF posF=transform(posSystem); for (int i=0; i=0) { if (closestSpotSystem) *closestSpotSystem=closestPos; if (label) *label=m_hitTestData[closest].label; return closedist; } else { return JKQTP_NAN; } } double JKQTPPlotElement::transformX(double x) const { return getXAxis()->x2p(x); } double JKQTPPlotElement::transformY(double y) const { return getYAxis()->x2p(y); } QVector JKQTPPlotElement::transformX(const QVector& x) const { QVector res; res.resize(x.size()); for (int i=0; ix2p(x[i]); } return res; } QVector JKQTPPlotElement::transformY(const QVector& y) const { QVector res; res.resize(y.size()); for (int i=0; ix2p(y[i]); } return res; } void JKQTPPlotElement::setXAxis(JKQTPCoordinateAxisRef ref) { xAxisRef=ref; } void JKQTPPlotElement::setYAxis(JKQTPCoordinateAxisRef ref) { yAxisRef=ref; } JKQTPCoordinateAxisRef JKQTPPlotElement::getXAxisRef() const { return xAxisRef; } JKQTPCoordinateAxisRef JKQTPPlotElement::getYAxisRef() const { return yAxisRef; } double JKQTPPlotElement::backtransformX(double x) const { return getXAxis()->p2x(x); } double JKQTPPlotElement::backtransformY(double y) const { return getYAxis()->p2x(y); } bool JKQTPGraph::usesColumn(int /*column*/) const { return false; } void JKQTPGraph::drawErrorsBefore(JKQTPEnhancedPainter &) { } void JKQTPGraph::drawErrorsAfter(JKQTPEnhancedPainter &) { } QPolygonF JKQTPPlotElement::transform(const QPolygonF &x) const { QPolygonF res; for (int i=0; i0) { res.moveTo(transform(x[0])); for (int i=1; igetDatastore(); int imin=0; int imax=0; getIndexRange(imin, imax); for (int i=imin; iget(static_cast(xColumn),static_cast(i)); if (JKQTPIsOKFloat(xv)) { if (start || xv>maxx) maxx=xv; if (start || xvgetDatastore(); int imin=0; int imax=0; getIndexRange(imin, imax); for (int i=imin; iget(static_cast(yColumn),static_cast(i)); if (JKQTPIsOKFloat(yv)) { if (start || yv>maxy) maxy=yv; if (start || yvxColumn = __value; } int JKQTPXYGraph::getXColumn() const { return this->xColumn; } void JKQTPXYGraph::setXColumn(size_t __value) { this->xColumn = static_cast(__value); } void JKQTPXYGraph::setYColumn(int __value) { this->yColumn = __value; } int JKQTPXYGraph::getYColumn() const { return this->yColumn; } void JKQTPXYGraph::setYColumn(size_t __value) { this->yColumn = static_cast(__value); } void JKQTPXYGraph::setKeyColumn(int __value) { setXColumn(__value); } void JKQTPXYGraph::setValueColumn(int __value) { setYColumn(__value); } void JKQTPXYGraph::setDataSortOrder(JKQTPXYGraph::DataSortOrder __value) { this->sortData = __value; } JKQTPXYGraph::DataSortOrder JKQTPXYGraph::getDataSortOrder() const { return this->sortData; } int JKQTPXYGraph::getKeyColumn() const { return getXColumn(); } int JKQTPXYGraph::getValueColumn() const { return getYColumn(); } void JKQTPXYGraph::setDataSortOrder(int __value) { sortData=static_cast(__value); } void JKQTPXYGraph::setXYColumns(size_t xCol, size_t yCol) { setXColumn(xCol); setYColumn(yCol); } void JKQTPXYGraph::setXYColumns(int xCol, int yCol) { setXColumn(xCol); setYColumn(yCol); } #if QT_VERSION xyColPair) { setXColumn(xyColPair.first); setYColumn(xyColPair.second); } void JKQTPXYGraph::setXYColumns(std::pair xyColPair) { setXColumn(xyColPair.first); setYColumn(xyColPair.second); } #endif void JKQTPXYGraph::setXYColumns(QPair xyColPair) { setXColumn(xyColPair.first); setYColumn(xyColPair.second); } void JKQTPXYGraph::setXYColumns(QPair xyColPair) { setXColumn(xyColPair.first); setYColumn(xyColPair.second); } double JKQTPXYGraph::hitTest(const QPointF &posSystem, QPointF *closestSpotSystem, QString *label, HitTestMode mode) const { if (parent==nullptr) return JKQTP_NAN; // check base-class implementation and use it, if it returns a vaid value const double baseclassResult=JKQTPPlotElement::hitTest(posSystem, closestSpotSystem, label, mode); if (JKQTPIsOKFloat(baseclassResult)) return baseclassResult; const JKQTPDatastore* datastore=parent->getDatastore(); int imin=0; int imax=0; if (!getIndexRange(imin, imax)) return JKQTP_NAN; int closest=-1; double closedist=JKQTP_NAN; double closedistsec=JKQTP_NAN; QPointF closestPos; QPointF posF=transform(posSystem); for (int i=imin; iget(static_cast(xColumn),static_cast(i)), datastore->get(static_cast(yColumn),static_cast(i))); const QPointF xpix = transform(x); if (JKQTPIsOKFloat(xpix.x())&&JKQTPIsOKFloat(xpix.y())) { double d=0, dsecondary=0; switch (mode) { case HitTestXY: d=sqrt(jkqtp_sqr(xpix.x()-posF.x())+jkqtp_sqr(xpix.y()-posF.y())); dsecondary=0; break; case HitTestXOnly: d=fabs(xpix.x()-posF.x()); dsecondary=fabs(xpix.y()-posF.y()); break; case HitTestYOnly: d=fabs(xpix.y()-posF.y()); dsecondary=fabs(xpix.x()-posF.x()); break; } if (closest<0 || dget(static_cast(dxColumn),static_cast(i)); const double dy=datastore->get(static_cast(dyColumn),static_cast(i)); vec="\\vec{\\mbox{\\delta}}=("+jkqtp_floattolatexqstr(dx, 3)+"{\\;},{\\;}"+jkqtp_floattolatexqstr(dy, 3)+")"; } break; case AngleAndLengthLayout: { const double a=datastore->get(static_cast(angleColumn),static_cast(i)); const double l=(lengthColumn<0) ? 1.0 : datastore->get(static_cast(lengthColumn),static_cast(i)); vec= "\\angle="+jkqtp_floattolatexqstr(a/M_PI*180.0, 1)+"\\degree{\\;},{\\;}l="+jkqtp_floattolatexqstr(l, 3); } break; } } return QString("\\ensuremath{\\left|\\stackrel{x=({\\:}%1{\\;},{\\;}%2{\\:}),}{ %3 }\\right.").arg(jkqtp_floattolatexqstr(x, 3)).arg(jkqtp_floattolatexqstr(y, 3)).arg(vec); } void JKQTPXYAndVectorGraph::setDxColumn(int col) { dxColumn=col; vectorDataLayout=DeltaXDeltaYLayout; } void JKQTPXYAndVectorGraph::setDyColumn(int col) { dyColumn=col; vectorDataLayout=DeltaXDeltaYLayout; } void JKQTPXYAndVectorGraph::setDxDyColumn(int colDx, int colDy) { dxColumn=colDx; dyColumn=colDy; vectorDataLayout=DeltaXDeltaYLayout; } void JKQTPXYAndVectorGraph::setAngleColumn(int col) { angleColumn=col; vectorDataLayout=AngleAndLengthLayout; } void JKQTPXYAndVectorGraph::setAngleAndLengthColumn(int colAngle, int colLength) { angleColumn=colAngle; lengthColumn=colLength; vectorDataLayout=AngleAndLengthLayout; } void JKQTPXYAndVectorGraph::setLengthColumn(int col) { lengthColumn=col; vectorDataLayout=AngleAndLengthLayout; } QPointF JKQTPXYAndVectorGraph::getVectorDxDy(int i) const { const JKQTPDatastore* datastore=parent->getDatastore(); if (datastore) { switch(vectorDataLayout) { case DeltaXDeltaYLayout: { const double dx=datastore->get(static_cast(dxColumn),static_cast(i)); const double dy=datastore->get(static_cast(dyColumn),static_cast(i)); return QPointF(dx,dy); } break; case AngleAndLengthLayout: { const double a=datastore->get(static_cast(angleColumn),static_cast(i)); const double l=(lengthColumn<0) ? 1.0 : datastore->get(static_cast(lengthColumn),static_cast(i)); return QPointF(l*cos(a),l*sin(a)); } break; } } return QPointF(JKQTP_NAN, JKQTP_NAN); } bool JKQTPXYAndVectorGraph::getIndexRange(int &imin, int &imax) const { bool ok=JKQTPXYGraph::getIndexRange(imin, imax); if (ok) { if (vectorDataLayout==DeltaXDeltaYLayout) { if (parent==nullptr) return false; if (dxColumn<0||dyColumn<0) return false; const JKQTPDatastore* datastore=parent->getDatastore(); const int rowsDX=static_cast(datastore->getRows(static_cast(dxColumn))); const int rowsDY=static_cast(datastore->getRows(static_cast(dyColumn))); imax=qMin(imax, qMin(rowsDX,rowsDY)); } else if (vectorDataLayout==AngleAndLengthLayout) { if (parent==nullptr) return false; if (angleColumn<0 && lengthColumn<0) return false; if (angleColumn<0) return false; const JKQTPDatastore* datastore=parent->getDatastore(); const int rowsA=static_cast(datastore->getRows(static_cast(angleColumn))); const int rowsL=(lengthColumn>=0)?static_cast(datastore->getRows(static_cast(lengthColumn))):0; imax=qMin(imax, qMin(rowsA,rowsL)); } } return ok; } JKQTPXGraph::JKQTPXGraph(JKQTBasePlotter *parent): JKQTPGraph(parent), xColumn(-1), sortData(Unsorted) { } bool JKQTPXGraph::getXMinMax(double &minx, double &maxx, double &smallestGreaterZero) { bool start=true; minx=0; maxx=0; smallestGreaterZero=0; if (parent==nullptr) return false; const JKQTPDatastore* datastore=parent->getDatastore(); int imin=0; int imax=0; getIndexRange(imin, imax); for (int i=imin; iget(static_cast(xColumn),static_cast(i)); if (JKQTPIsOKFloat(xv)) { if (start || xv>maxx) maxx=xv; if (start || xv(__value); } void JKQTPXGraph::setDataSortOrder(DataSortOrder __value) { sortData=__value; } void JKQTPXGraph::setXColumn(int __value) { xColumn = static_cast(__value); } void JKQTPXGraph::setXColumn(size_t __value) { xColumn = static_cast(__value); } void JKQTPXGraph::setKeyColumn(int __value) { setXColumn(__value); } void JKQTPXGraph::intSortData() { sortedIndices.clear(); if (parent==nullptr) return ; if (sortData==JKQTPXGraph::Unsorted) return ; JKQTPDatastore* datastore=parent->getDatastore(); int imin=0; int imax=0; getIndexRange(imin, imax); QVector datas; if (sortData==SortedX) { for (int i=0; iget(static_cast(xColumn),static_cast(i)); sortedIndices<=0) { JKQTPDatastore* datastore=parent->getDatastore(); imin=0; imax=static_cast(datastore->getRows(static_cast(xColumn))); // ensure correct order, i.e. imin<=imax if (imax=0 if (imin<0) imin=0; if (imax<0) imax=0; return true; } return false; }