QCustomPlot Discussion and Comments

Remove yAxis and put labels above gridline -- like The EconomistReturn to overview

Please have a look at the Economist's style for plot, which they use quite consistently:
https://www.economist.com/graphic-detail/2019/12/11/finlands-new-prime-minister-is-the-worlds-youngest
The y-axis is absent altogether, while the y-values are just above the gridlines. Is this possible to achieve in QCustomPlot?
So far, I've been able to imitate their colors etc., thanks for the library being so good.

Thanks.

https://www.economist.com/graphic-detail/2019/12/11/finlands-new-prime-minister-is-the-worlds-youngest -- the above link. Thanks again.

You would need to create your own QCPAxis with your own QCPAxisPainterPrivate and override the getTickLabelDrawOffset method.

In your case (type == QCPAxis::atRight, tickLabelSide == QCPAxis::lsInside, doRotation==false) :
just set y to -labelData.totalBounds.height()

It could be

QPointF myAxisPainterPrivate::getTickLabelDrawOffset(const TickLabelData &labelData) const
{
  bool doRotation = !qFuzzyIsNull(tickLabelRotation);
  if (type == QCPAxis::atRight && tickLabelSide == QCPAxis::lsInside && doRotation==false) 
  {
      x = -labelData.totalBounds.width();
      y = -labelData.totalBounds.height();
      return QPointF(x, y);
  } else {
      return QCPAxisPainterPrivate::getTickLabelDrawOffset(labelData);
  }
}


Thanks for the answer, BSO.

Here is what I came up with based on the library's source code:

class myQCPAxisPainterPrivate: public QCPAxisPainterPrivate {
public:
    explicit myQCPAxisPainterPrivate(QCustomPlot *parentPlot);
protected:
    QPointF getTickLabelDrawOffset(const TickLabelData &labelData) const override;
};

class EconomistStyleQCPAxis: public QCPAxis {
    EconomistStyleQCPAxis( QCPAxisRect *parent, AxisType type ) ;
};

and
QPointF myQCPAxisPainterPrivate::getTickLabelDrawOffset(const QCPAxisPainterPrivate::TickLabelData &labelData) const {
    bool doRotation = !qFuzzyIsNull(tickLabelRotation);
    if (type == QCPAxis::atRight && tickLabelSide == QCPAxis::lsInside && !doRotation)
    {
        double x = -labelData.totalBounds.width();
        double y = -labelData.totalBounds.height();
        return QPointF(x, y);
    } else {
        return QCPAxisPainterPrivate::getTickLabelDrawOffset(labelData);
    }
}

myQCPAxisPainterPrivate::myQCPAxisPainterPrivate(QCustomPlot *parentPlot) : QCPAxisPainterPrivate(parentPlot) {
}

EconomistStyleQCPAxis::EconomistStyleQCPAxis(QCPAxisRect *parent, QCPAxis::AxisType type) : QCPAxis(parent, type) {
    mAxisPainter= new myQCPAxisPainterPrivate(parent->parentPlot());
}

Now the question is, how do I replace the axis2 of the customPlot with this new class? I guess it should look closer to this, but not quite:
customPlot->axisRect(0)->addAxis(QCPAxis::AxisType::atRight,EconomistStyleQCPAxis(customPlot->rect(),QCPAxis::AxisType::atRight));

OK, making the constructor of the custom class public and with the following line:

customPlot->axisRect(0)->removeAxis(customPlot->yAxis);
    customPlot->axisRect(0)->addAxis(QCPAxis::AxisType::atLeft,new EconomistStyleQCPAxis(customPlot->axisRect(),QCPAxis::AxisType::atLeft));

I was able to produce what I want. Thanks! The result: https://imagebin.ca/v/55m7JdPYlhrC
However, some fine-tuning needed (asked in a different thread in this forum).

Thanks again.