Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to set alignment of QChart axis
Forum Updated to NodeBB v4.3 + New Features

how to set alignment of QChart axis

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    April Zhang
    wrote on 22 Apr 2018, 13:42 last edited by
    #1

    ![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

    D 1 Reply Last reply 24 Apr 2018, 02:36
    0
    • A April Zhang
      22 Apr 2018, 13:42

      ![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

      D Offline
      D Offline
      Diracsbracket
      wrote on 24 Apr 2018, 02:36 last edited by Diracsbracket
      #2

      @April-Zhang
      Hi
      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);
          m_chart2->setMargins(margins);
      

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

      FYI:
      Although I have no experience with QChart, I have been using the very convenient QCustomPlot widget to some limited extend:
      http://www.qcustomplot.com/

      It has a feature called QCPMarginGroup which allows you to obtain the alignment you seek.
      http://www.qcustomplot.com/documentation/classQCPMarginGroup.html

      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.

      1 Reply Last reply
      1
      • D Offline
        D Offline
        Diracsbracket
        wrote on 24 Apr 2018, 04:16 last edited by Diracsbracket
        #3
        This post is deleted!
        1 Reply Last reply
        0

        1/3

        22 Apr 2018, 13:42

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved