Important: Please read the Qt Code of Conduct -

how to set alignment of QChart axis

  • ![alt text](0_1524404842644_a.png image url)
    using QGraphicsView and scene and Qchart,but i can't set alignment of them , give some advice

  • @April-Zhang
    The QChart class has the setMargins method which you could use to set a left margin on the plot with the smallest Y max label value, but the problem is then to determine which margin value would align the two axes exactly and in all cases.

    The following seems to work:

        QFont labelFont = m_chart1->axisY()->labelsFont();
        QString str1("10000.0"); //set max. y label value here for chart1
        QString str2("10.0");    //set max. y label value here for chart2
        QFontMetrics fm(labelFont);
        int width1 = fm.boundingRect(str1).width();
        int width2 = fm.boundingRect(str2).width();
        QMargins margins = m_chart1->margins();
        int marg1 = margins.left();
        margins.setLeft(marg1 + width1 - width2);

    So if you can get the maximum label strings on both Y axes, the above will align the two Y axes.

    Although I have no experience with QChart, I have been using the very convenient QCustomPlot widget to some limited extend:

    It has a feature called QCPMarginGroup which allows you to obtain the alignment you seek.

    The QCustomPlot widget is simply added to your project by including only 2 files: a header and a .cpp file with no further dependencies and which you are free to modify to your needs.

  • This post is deleted!

Log in to reply