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. QT chart PlotArea
Forum Updated to NodeBB v4.3 + New Features

QT chart PlotArea

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.0k Views
  • 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.
  • I Offline
    I Offline
    IknowQT
    wrote on 23 Aug 2021, 00:17 last edited by
    #1

    In the qt chart, the value of m_pChartViewer->chart()->plotArea(); rect is 0,0,0,0. Why is that? I can't get a chart size.

    b660d778-e30a-4542-a8b6-38908cc228c7-image.png

    E 1 Reply Last reply 23 Aug 2021, 00:27
    0
    • I IknowQT
      23 Aug 2021, 00:52
      void ExChart::InitializeChart()
      {
      	m_pChartViewer = new QChartView(createLineChart());
      
          m_pChartViewer->setRenderHint(QPainter::Antialiasing);
          m_pChartViewer->chart()->legend()->hide();
      
          this->ui.verticalLayout->addWidget(m_pChartViewer);
      
          UpdateChart();  
      }
      
      QChart* ExChart::createLineChart()
      {
      	m_pChart = new QChart();
      	m_pChart->setTitle("Line chart");
      
      	QPen pen(QColor(47, 245, 76));
      	pen.setWidth(3);
      
          QString name("Series ");
          int nameIndex = 0;
      
          m_pLineSeries = new QLineSeries(m_pChart);
          m_pLineSeries->setPen(pen);
         
          m_pScatSeries = new QScatterSeries(m_pChart);
      
          m_pChart->addSeries(m_pLineSeries);
          m_pChart->addSeries(m_pScatSeries);
          m_pChart->createDefaultAxes();
      
          m_pChart->axes(Qt::Horizontal).first()->setRange(0, 100);
          m_pChart->axes(Qt::Vertical).first()->setRange(0, 100);
      
          return m_pChart;
      }
      
      void ExChart::UpdateChart()
      {
          QRectF	rect = m_pChartViewer->chart()->plotArea();		// only 차트 사이즈 (마진 제외)
      
          for (int i = 0; i < 100; i++)
          {
              qreal	spacing = (((rect.right() - rect.left()) / ((qreal)100 - (qreal)0)) * i) + rect.right();
      
              int y = QRandomGenerator::global()->bounded(0, 101);
              m_pLineSeries->append(i, y);
              m_pScatSeries->append(i, y);
      
              m_vPoint.push_back(QPointF(i, y));
      
              QString str = QString("X: %1\nY: %2").arg(i).arg(y);
              m_pTextItem = new QGraphicsTextItem(m_pChart);
              m_pTextItem->setFont(QFont("Bahnschrift Condensed"));
              m_pTextItem->setZValue(12);
              m_pTextItem->setDefaultTextColor(Qt::white);
              m_pTextItem->setHtml(QString("<div style='background-color: rgba(255,0,0,150);'>") + str + "</div>");
      
              qreal PosX = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).x();
              qreal PosY = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).y();
      
              m_pChart->mapToScene(PosX, PosY);
              m_pTextItem->setPos(PosX, PosY);
          }
      }
      

      What I want to do is to mark the x,y values for each position of the scatter.

      I don't think it's working. It's always zero.
      qreal PosX = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).x();
      qreal PosY = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).y();

      E Offline
      E Offline
      eyllanesc
      wrote on 23 Aug 2021, 01:21 last edited by
      #4

      @IknowQT Try changing UpdateChart(); to QTimer::singleShot(0, this, &ExChart::UpdateChart);

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      I 1 Reply Last reply 23 Aug 2021, 01:42
      0
      • I IknowQT
        23 Aug 2021, 00:17

        In the qt chart, the value of m_pChartViewer->chart()->plotArea(); rect is 0,0,0,0. Why is that? I can't get a chart size.

        b660d778-e30a-4542-a8b6-38908cc228c7-image.png

        E Offline
        E Offline
        eyllanesc
        wrote on 23 Aug 2021, 00:27 last edited by
        #2

        @IknowQT Could you provide a minimal and verifiable example, maybe some configuration has been done incorrectly, or maybe it is the expected output.

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          IknowQT
          wrote on 23 Aug 2021, 00:52 last edited by IknowQT
          #3
          void ExChart::InitializeChart()
          {
          	m_pChartViewer = new QChartView(createLineChart());
          
              m_pChartViewer->setRenderHint(QPainter::Antialiasing);
              m_pChartViewer->chart()->legend()->hide();
          
              this->ui.verticalLayout->addWidget(m_pChartViewer);
          
              UpdateChart();  
          }
          
          QChart* ExChart::createLineChart()
          {
          	m_pChart = new QChart();
          	m_pChart->setTitle("Line chart");
          
          	QPen pen(QColor(47, 245, 76));
          	pen.setWidth(3);
          
              QString name("Series ");
              int nameIndex = 0;
          
              m_pLineSeries = new QLineSeries(m_pChart);
              m_pLineSeries->setPen(pen);
             
              m_pScatSeries = new QScatterSeries(m_pChart);
          
              m_pChart->addSeries(m_pLineSeries);
              m_pChart->addSeries(m_pScatSeries);
              m_pChart->createDefaultAxes();
          
              m_pChart->axes(Qt::Horizontal).first()->setRange(0, 100);
              m_pChart->axes(Qt::Vertical).first()->setRange(0, 100);
          
              return m_pChart;
          }
          
          void ExChart::UpdateChart()
          {
              QRectF	rect = m_pChartViewer->chart()->plotArea();		// only 차트 사이즈 (마진 제외)
          
              for (int i = 0; i < 100; i++)
              {
                  qreal	spacing = (((rect.right() - rect.left()) / ((qreal)100 - (qreal)0)) * i) + rect.right();
          
                  int y = QRandomGenerator::global()->bounded(0, 101);
                  m_pLineSeries->append(i, y);
                  m_pScatSeries->append(i, y);
          
                  m_vPoint.push_back(QPointF(i, y));
          
                  QString str = QString("X: %1\nY: %2").arg(i).arg(y);
                  m_pTextItem = new QGraphicsTextItem(m_pChart);
                  m_pTextItem->setFont(QFont("Bahnschrift Condensed"));
                  m_pTextItem->setZValue(12);
                  m_pTextItem->setDefaultTextColor(Qt::white);
                  m_pTextItem->setHtml(QString("<div style='background-color: rgba(255,0,0,150);'>") + str + "</div>");
          
                  qreal PosX = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).x();
                  qreal PosY = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).y();
          
                  m_pChart->mapToScene(PosX, PosY);
                  m_pTextItem->setPos(PosX, PosY);
              }
          }
          

          What I want to do is to mark the x,y values for each position of the scatter.

          I don't think it's working. It's always zero.
          qreal PosX = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).x();
          qreal PosY = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).y();

          E 1 Reply Last reply 23 Aug 2021, 01:21
          0
          • I IknowQT
            23 Aug 2021, 00:52
            void ExChart::InitializeChart()
            {
            	m_pChartViewer = new QChartView(createLineChart());
            
                m_pChartViewer->setRenderHint(QPainter::Antialiasing);
                m_pChartViewer->chart()->legend()->hide();
            
                this->ui.verticalLayout->addWidget(m_pChartViewer);
            
                UpdateChart();  
            }
            
            QChart* ExChart::createLineChart()
            {
            	m_pChart = new QChart();
            	m_pChart->setTitle("Line chart");
            
            	QPen pen(QColor(47, 245, 76));
            	pen.setWidth(3);
            
                QString name("Series ");
                int nameIndex = 0;
            
                m_pLineSeries = new QLineSeries(m_pChart);
                m_pLineSeries->setPen(pen);
               
                m_pScatSeries = new QScatterSeries(m_pChart);
            
                m_pChart->addSeries(m_pLineSeries);
                m_pChart->addSeries(m_pScatSeries);
                m_pChart->createDefaultAxes();
            
                m_pChart->axes(Qt::Horizontal).first()->setRange(0, 100);
                m_pChart->axes(Qt::Vertical).first()->setRange(0, 100);
            
                return m_pChart;
            }
            
            void ExChart::UpdateChart()
            {
                QRectF	rect = m_pChartViewer->chart()->plotArea();		// only 차트 사이즈 (마진 제외)
            
                for (int i = 0; i < 100; i++)
                {
                    qreal	spacing = (((rect.right() - rect.left()) / ((qreal)100 - (qreal)0)) * i) + rect.right();
            
                    int y = QRandomGenerator::global()->bounded(0, 101);
                    m_pLineSeries->append(i, y);
                    m_pScatSeries->append(i, y);
            
                    m_vPoint.push_back(QPointF(i, y));
            
                    QString str = QString("X: %1\nY: %2").arg(i).arg(y);
                    m_pTextItem = new QGraphicsTextItem(m_pChart);
                    m_pTextItem->setFont(QFont("Bahnschrift Condensed"));
                    m_pTextItem->setZValue(12);
                    m_pTextItem->setDefaultTextColor(Qt::white);
                    m_pTextItem->setHtml(QString("<div style='background-color: rgba(255,0,0,150);'>") + str + "</div>");
            
                    qreal PosX = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).x();
                    qreal PosY = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).y();
            
                    m_pChart->mapToScene(PosX, PosY);
                    m_pTextItem->setPos(PosX, PosY);
                }
            }
            

            What I want to do is to mark the x,y values for each position of the scatter.

            I don't think it's working. It's always zero.
            qreal PosX = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).x();
            qreal PosY = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).y();

            E Offline
            E Offline
            eyllanesc
            wrote on 23 Aug 2021, 01:21 last edited by
            #4

            @IknowQT Try changing UpdateChart(); to QTimer::singleShot(0, this, &ExChart::UpdateChart);

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            I 1 Reply Last reply 23 Aug 2021, 01:42
            0
            • E eyllanesc
              23 Aug 2021, 01:21

              @IknowQT Try changing UpdateChart(); to QTimer::singleShot(0, this, &ExChart::UpdateChart);

              I Offline
              I Offline
              IknowQT
              wrote on 23 Aug 2021, 01:42 last edited by
              #5

              @eyllanesc

              Wow, thank you so much.
              I didn't know it was this simple.
              But why do you need to update me with a timer?

              E 1 Reply Last reply 23 Aug 2021, 01:53
              0
              • I IknowQT
                23 Aug 2021, 01:42

                @eyllanesc

                Wow, thank you so much.
                I didn't know it was this simple.
                But why do you need to update me with a timer?

                E Offline
                E Offline
                eyllanesc
                wrote on 23 Aug 2021, 01:53 last edited by
                #6

                @IknowQT Sometimes it happens that for efficiency reasons Qt does not update some properties and it does so when the widgets are just displayed. And it seems that this is the case.

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                1 Reply Last reply
                0

                1/6

                23 Aug 2021, 00:17

                • Login

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