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. QChart line series not shown
Forum Updated to NodeBB v4.3 + New Features

QChart line series not shown

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.5k 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.
  • P Offline
    P Offline
    Perdrix
    wrote on 25 Jun 2023, 12:31 last edited by Perdrix
    #1

    OK why aren't the points in the line series being displayed?

    //
    // Add the score and update the score chart
    //
    if (nullptr == scoreSeries)
    {
    	scoreSeries = new QLineSeries(this);
    	scoreSeries->setName(tr("Score", "IDC_SCORE"));
    	scoreSeries->setPointsVisible(true);
    	scoreChart->addSeries(scoreSeries);
    	scoreChart->createDefaultAxes();
    	connect(scoreSeries, &QLineSeries::hovered,
    		this, &ChartTab::scoreHovered);
    }
    
    scoreSeries->append(x, fScore);
    scoreMap.emplace(name, size - 1);
    axes = scoreChart->axes(Qt::Vertical);
    for (const auto& p : axes)
    {
    	QValueAxis* axis{ dynamic_cast<QValueAxis*>(p) };
    	if (axis)
    		axis->applyNiceNumbers();
    }
    axes = scoreChart->axes(Qt::Horizontal);
    for (const auto& p : axes)
    {
    	QValueAxis* axis{ dynamic_cast<QValueAxis*>(p) };
    	if (axis)
    	{
    		axis->setRange(1.0, size);
    		axis->setTickAnchor(1.0);
    		axis->setTickType(QValueAxis::TicksDynamic);
    		axis->setTickInterval(interval);
    	}
    }
    scoreChart->update();
    

    As more points are added to the series the horizontal axis updates take place just fine, but the actual data is never displayed (I can force this by removing and re-adding the line series but that is just SO WRONG). Here's the disply after adding six data points:

    9600500f-6424-48b5-9067-acdb55f5c221-image.png

    Note the total absence of points and lines between them.

    The following HORRID code sort of works:

    if (nullptr != fwhmSeries)
    	fwhmChart->removeSeries(fwhmSeries);
    else
    {
    	fwhmSeries = new QLineSeries(this);
    	fwhmSeries->setName(tr("FWHM", "IDC_FWHM"));
    	fwhmSeries->setPointsVisible(true);
    	connect(fwhmSeries, &QLineSeries::hovered,
    		this, &ChartTab::fwhmHovered);
    }
    
    fwhmSeries->append(x, fFWHM);
    fwhmMap.emplace(name, size - 1);
    fwhmChart->addSeries(fwhmSeries);
    

    With this result:

    6023a438-04af-471d-94de-3b43e109d99b-image.png

    But I REALLY shouldn't need to do that (should I ???).

    Does anyone here know how to make this QChart stuff work? It would seem that I haven't worked it out :(!!

    David

    J 2 Replies Last reply 25 Jun 2023, 12:51
    0
    • P Perdrix
      25 Jun 2023, 12:31

      OK why aren't the points in the line series being displayed?

      //
      // Add the score and update the score chart
      //
      if (nullptr == scoreSeries)
      {
      	scoreSeries = new QLineSeries(this);
      	scoreSeries->setName(tr("Score", "IDC_SCORE"));
      	scoreSeries->setPointsVisible(true);
      	scoreChart->addSeries(scoreSeries);
      	scoreChart->createDefaultAxes();
      	connect(scoreSeries, &QLineSeries::hovered,
      		this, &ChartTab::scoreHovered);
      }
      
      scoreSeries->append(x, fScore);
      scoreMap.emplace(name, size - 1);
      axes = scoreChart->axes(Qt::Vertical);
      for (const auto& p : axes)
      {
      	QValueAxis* axis{ dynamic_cast<QValueAxis*>(p) };
      	if (axis)
      		axis->applyNiceNumbers();
      }
      axes = scoreChart->axes(Qt::Horizontal);
      for (const auto& p : axes)
      {
      	QValueAxis* axis{ dynamic_cast<QValueAxis*>(p) };
      	if (axis)
      	{
      		axis->setRange(1.0, size);
      		axis->setTickAnchor(1.0);
      		axis->setTickType(QValueAxis::TicksDynamic);
      		axis->setTickInterval(interval);
      	}
      }
      scoreChart->update();
      

      As more points are added to the series the horizontal axis updates take place just fine, but the actual data is never displayed (I can force this by removing and re-adding the line series but that is just SO WRONG). Here's the disply after adding six data points:

      9600500f-6424-48b5-9067-acdb55f5c221-image.png

      Note the total absence of points and lines between them.

      The following HORRID code sort of works:

      if (nullptr != fwhmSeries)
      	fwhmChart->removeSeries(fwhmSeries);
      else
      {
      	fwhmSeries = new QLineSeries(this);
      	fwhmSeries->setName(tr("FWHM", "IDC_FWHM"));
      	fwhmSeries->setPointsVisible(true);
      	connect(fwhmSeries, &QLineSeries::hovered,
      		this, &ChartTab::fwhmHovered);
      }
      
      fwhmSeries->append(x, fFWHM);
      fwhmMap.emplace(name, size - 1);
      fwhmChart->addSeries(fwhmSeries);
      

      With this result:

      6023a438-04af-471d-94de-3b43e109d99b-image.png

      But I REALLY shouldn't need to do that (should I ???).

      Does anyone here know how to make this QChart stuff work? It would seem that I haven't worked it out :(!!

      David

      J Offline
      J Offline
      JonB
      wrote on 25 Jun 2023, 12:51 last edited by
      #2

      @Perdrix
      I typed in a fair sized answer to you yesterday at https://forum.qt.io/topic/145907/updating-a-chart-when-appending-to-a-line-series/4. But you have not commented there, and I don't fancy keep answering your chart questions if you do not do so.....

      P 1 Reply Last reply 25 Jun 2023, 13:02
      1
      • P Perdrix
        25 Jun 2023, 12:31

        OK why aren't the points in the line series being displayed?

        //
        // Add the score and update the score chart
        //
        if (nullptr == scoreSeries)
        {
        	scoreSeries = new QLineSeries(this);
        	scoreSeries->setName(tr("Score", "IDC_SCORE"));
        	scoreSeries->setPointsVisible(true);
        	scoreChart->addSeries(scoreSeries);
        	scoreChart->createDefaultAxes();
        	connect(scoreSeries, &QLineSeries::hovered,
        		this, &ChartTab::scoreHovered);
        }
        
        scoreSeries->append(x, fScore);
        scoreMap.emplace(name, size - 1);
        axes = scoreChart->axes(Qt::Vertical);
        for (const auto& p : axes)
        {
        	QValueAxis* axis{ dynamic_cast<QValueAxis*>(p) };
        	if (axis)
        		axis->applyNiceNumbers();
        }
        axes = scoreChart->axes(Qt::Horizontal);
        for (const auto& p : axes)
        {
        	QValueAxis* axis{ dynamic_cast<QValueAxis*>(p) };
        	if (axis)
        	{
        		axis->setRange(1.0, size);
        		axis->setTickAnchor(1.0);
        		axis->setTickType(QValueAxis::TicksDynamic);
        		axis->setTickInterval(interval);
        	}
        }
        scoreChart->update();
        

        As more points are added to the series the horizontal axis updates take place just fine, but the actual data is never displayed (I can force this by removing and re-adding the line series but that is just SO WRONG). Here's the disply after adding six data points:

        9600500f-6424-48b5-9067-acdb55f5c221-image.png

        Note the total absence of points and lines between them.

        The following HORRID code sort of works:

        if (nullptr != fwhmSeries)
        	fwhmChart->removeSeries(fwhmSeries);
        else
        {
        	fwhmSeries = new QLineSeries(this);
        	fwhmSeries->setName(tr("FWHM", "IDC_FWHM"));
        	fwhmSeries->setPointsVisible(true);
        	connect(fwhmSeries, &QLineSeries::hovered,
        		this, &ChartTab::fwhmHovered);
        }
        
        fwhmSeries->append(x, fFWHM);
        fwhmMap.emplace(name, size - 1);
        fwhmChart->addSeries(fwhmSeries);
        

        With this result:

        6023a438-04af-471d-94de-3b43e109d99b-image.png

        But I REALLY shouldn't need to do that (should I ???).

        Does anyone here know how to make this QChart stuff work? It would seem that I haven't worked it out :(!!

        David

        J Offline
        J Offline
        JonB
        wrote on 25 Jun 2023, 12:58 last edited by JonB
        #3

        @Perdrix
        BTW, in order to keep you happy I have loaded up QtCharts, which I had never used. The follwoing code works fine for me for updating the chart in real time:

        inline QLineSeries *ChartView::lineSeries(int i) const
        {
            return qobject_cast<QLineSeries *>(_chart->series()[i]);
        }
        
        inline QValueAxis *ChartView::axisX() const
        {
            return qobject_cast<QValueAxis *>(_chart->axes(Qt::Horizontal)[0]);
        }
        
        inline QValueAxis *ChartView::axisY() const
        {
            return qobject_cast<QValueAxis *>(_chart->axes(Qt::Vertical)[0]);
        }
        
        void ChartView::clear()
        {
            // clear all series from the chart
            _chart->removeAllSeries();
        }
        
        void ChartView::setSeries(const QStringList &names)
        {
            // set all the series the chart is to show
            clear();
            for (auto name : names)
            {
                QLineSeries *series = new QLineSeries();
                series->setName(name);
                _chart->addSeries(series);
            }
            // set the initial state of the axes
            _chart->createDefaultAxes();
            axisX()->setLabelFormat("%d");
            axisY()->setLabelFormat("%d");
            axisX()->setRange(0, 100);
            axisY()->setRange(0, 1000);
        }
        
        void ChartView::addSeriesValues(const QList<int> &values)
        {
            // add values for all series
            Q_ASSERT(values.count() == _chart->series().count());
            for (int i = 0; i < values.count(); i++)
            {
                // append the new point at the end of the last existing one
                int x = lineSeries(i)->count(), y = values.at(i);
                lineSeries(i)->append(x, y);
                // if x or y values exceed current axis maximum, increase axis maximum by 10%
                int maxX = axisX()->max() + 1;
                if (x >= maxX)
                    axisX()->setMax(x + (x + 9) / 10);
                int maxY = axisY()->max() + 1;
                if (y >= maxY)
                    axisY()->setMax(y + (y + 9) / 10);
            }
        }
        

        I am Linux, Qt 5.15. Note that as shown it is your job to reset axes as required when adding new points, as I suggested in the other post....

        P 1 Reply Last reply 28 Jun 2023, 14:35
        0
        • J JonB referenced this topic on 25 Jun 2023, 12:59
        • J JonB
          25 Jun 2023, 12:51

          @Perdrix
          I typed in a fair sized answer to you yesterday at https://forum.qt.io/topic/145907/updating-a-chart-when-appending-to-a-line-series/4. But you have not commented there, and I don't fancy keep answering your chart questions if you do not do so.....

          P Offline
          P Offline
          Perdrix
          wrote on 25 Jun 2023, 13:02 last edited by
          #4

          @JonB

          @JonB said in QChart line series not shown:

          @Perdrix
          I typed in a fair sized answer to you yesterday at https://forum.qt.io/topic/145907/updating-a-chart-when-appending-to-a-line-series/4. But you have not commented there, and I don't fancy keep answering your chart questions if you do not do so.....

          I never received the email notifying me of that update - if I had you can be sure I would have responded.

          J 1 Reply Last reply 25 Jun 2023, 13:07
          0
          • P Perdrix
            25 Jun 2023, 13:02

            @JonB

            @JonB said in QChart line series not shown:

            @Perdrix
            I typed in a fair sized answer to you yesterday at https://forum.qt.io/topic/145907/updating-a-chart-when-appending-to-a-line-series/4. But you have not commented there, and I don't fancy keep answering your chart questions if you do not do so.....

            I never received the email notifying me of that update - if I had you can be sure I would have responded.

            J Offline
            J Offline
            JonB
            wrote on 25 Jun 2023, 13:07 last edited by JonB
            #5

            @Perdrix
            I think you need to review your notifications then, or look back at your topics regularly :)

            The gist of that, plus the code shown here I am just playing with this morning, shows there is "nothing special to do" to see points being added and updated in real time (assuming you're allowing the vent loop to run). Note that if you plot a point outside the current axes' limits it is your job to adjust the axes' range to allow, else you don't see it.

            P 1 Reply Last reply 25 Jun 2023, 13:09
            0
            • J JonB
              25 Jun 2023, 13:07

              @Perdrix
              I think you need to review your notifications then, or look back at your topics regularly :)

              The gist of that, plus the code shown here I am just playing with this morning, shows there is "nothing special to do" to see points being added and updated in real time (assuming you're allowing the vent loop to run). Note that if you plot a point outside the current axes' limits it is your job to adjust the axes' range to allow, else you don't see it.

              P Offline
              P Offline
              Perdrix
              wrote on 25 Jun 2023, 13:09 last edited by
              #6

              @JonB >Note that if you plot a point outside the current axes' limits it is your job to adjust the axes' range to allow, else you don't see it.

              Indeed that would appear to be the crux of the problem.

              I think that some additional tutorial stuff in the docs would help a LOT :)

              Thanks
              David

              1 Reply Last reply
              0
              • J JonB referenced this topic on 27 Jun 2023, 16:15
              • J JonB
                25 Jun 2023, 12:58

                @Perdrix
                BTW, in order to keep you happy I have loaded up QtCharts, which I had never used. The follwoing code works fine for me for updating the chart in real time:

                inline QLineSeries *ChartView::lineSeries(int i) const
                {
                    return qobject_cast<QLineSeries *>(_chart->series()[i]);
                }
                
                inline QValueAxis *ChartView::axisX() const
                {
                    return qobject_cast<QValueAxis *>(_chart->axes(Qt::Horizontal)[0]);
                }
                
                inline QValueAxis *ChartView::axisY() const
                {
                    return qobject_cast<QValueAxis *>(_chart->axes(Qt::Vertical)[0]);
                }
                
                void ChartView::clear()
                {
                    // clear all series from the chart
                    _chart->removeAllSeries();
                }
                
                void ChartView::setSeries(const QStringList &names)
                {
                    // set all the series the chart is to show
                    clear();
                    for (auto name : names)
                    {
                        QLineSeries *series = new QLineSeries();
                        series->setName(name);
                        _chart->addSeries(series);
                    }
                    // set the initial state of the axes
                    _chart->createDefaultAxes();
                    axisX()->setLabelFormat("%d");
                    axisY()->setLabelFormat("%d");
                    axisX()->setRange(0, 100);
                    axisY()->setRange(0, 1000);
                }
                
                void ChartView::addSeriesValues(const QList<int> &values)
                {
                    // add values for all series
                    Q_ASSERT(values.count() == _chart->series().count());
                    for (int i = 0; i < values.count(); i++)
                    {
                        // append the new point at the end of the last existing one
                        int x = lineSeries(i)->count(), y = values.at(i);
                        lineSeries(i)->append(x, y);
                        // if x or y values exceed current axis maximum, increase axis maximum by 10%
                        int maxX = axisX()->max() + 1;
                        if (x >= maxX)
                            axisX()->setMax(x + (x + 9) / 10);
                        int maxY = axisY()->max() + 1;
                        if (y >= maxY)
                            axisY()->setMax(y + (y + 9) / 10);
                    }
                }
                

                I am Linux, Qt 5.15. Note that as shown it is your job to reset axes as required when adding new points, as I suggested in the other post....

                P Offline
                P Offline
                printfne
                wrote on 28 Jun 2023, 14:35 last edited by
                #7

                @JonB I can't quite understand this code, he seems to be the implementation of library functions? At the end of this page, the problem that is not displayed seems to be an axis problem, but my problem does not seem to use the axis, do I need to modify the axis?

                J 1 Reply Last reply 28 Jun 2023, 16:00
                0
                • P printfne
                  28 Jun 2023, 14:35

                  @JonB I can't quite understand this code, he seems to be the implementation of library functions? At the end of this page, the problem that is not displayed seems to be an axis problem, but my problem does not seem to use the axis, do I need to modify the axis?

                  J Offline
                  J Offline
                  JonB
                  wrote on 28 Jun 2023, 16:00 last edited by JonB
                  #8

                  @printfne
                  I believe (though not certain) that it is indeed an axes issue. When adding points to an existing series on the chart I think you have to adjust the axes to ensure it is within range to show,.

                  All that matters in my code is the updating of the axes' ranges in addSeriesValues().

                  What does qDebug() << chart->axes().count() report?

                  I assume the chart must always have some idea of axes and range/scale, else how would it know where to plot anything?

                  If you have some points visible initially, you might also trying adding new points within the range of what is already there instead of beyond it. Maybe you would see such points updated.

                  This question is about not seeing newly added points after the initial display of the chart. Even if you don't add points later you may need to call createDefaultAxes() anyway, I don't know.

                  1 Reply Last reply
                  0

                  4/8

                  25 Jun 2023, 13:02

                  • Login

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