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

    eyllanescE 1 Reply Last reply
    0
    • I IknowQT
      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();

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on 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
      0
      • I IknowQT

        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

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on 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 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();

          eyllanescE 1 Reply Last reply
          0
          • I IknowQT
            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();

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on 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
            0
            • eyllanescE eyllanesc

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

              I Offline
              I Offline
              IknowQT
              wrote on 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?

              eyllanescE 1 Reply Last reply
              0
              • I IknowQT

                @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?

                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on 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

                • Login

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