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. QtCharts setRange issue
Qt 6.11 is out! See what's new in the release blog

QtCharts setRange issue

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 1 Posters 2.2k 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hello!

    I want to display some data using QtCharts. I have tried the DynamicSpline example, it works well but when I set my data then nothing is displayed. I think the problem is with the setRange method.

    Code:

    chart.h:

    #ifndef CHART_H
    #define CHART_H
    
    #include <QChart>
    #include <QTimer>
    #include <QAbstractAxis>
    #include <QSplineSeries>
    #include <QValueAxis>
    #include <QDebug>
    
    QT_CHARTS_BEGIN_NAMESPACE
    class QSplineSeries;
    class QValueAxis;
    QT_CHARTS_END_NAMESPACE
    
    QT_CHARTS_USE_NAMESPACE
    
    class Chart: public QChart
    {
        Q_OBJECT
    public:
        Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
        virtual ~Chart();
    
    public slots:
        void handleTimeout();
    
    private:
        QTimer m_timer;
        QSplineSeries *m_series;
        QStringList m_titles;
        QValueAxis *m_axisX;
        QValueAxis *m_axisY;
        qreal m_step;
        qreal m_x;
        qreal m_y;
        QList<double> testList;
    };
    
    #endif /* CHART_H */
    

    chart.cpp:

    #include "chart.h"
    
    Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags):
        QChart(QChart::ChartTypeCartesian, parent, wFlags),
        m_series(0),
        m_step(0),
        m_x(1),
        m_y(1)
    {
        m_axisX = new QValueAxis(this);
        m_axisX->setLabelsVisible(false);
        m_axisY = new QValueAxis(this);
        m_axisY->setLabelsVisible(false);
        connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
        m_timer.setInterval(1000);
        m_series = new QSplineSeries(this);
        QPen green(Qt::green);
        green.setWidth(3);
        m_series->setPen(green);
        m_series->append(m_x, m_y);
        legend()->hide();
        addSeries(m_series);
        setAxisX(m_axisX, m_series);
        setAxisY(m_axisY, m_series);
        m_axisX->setTickCount(5);
        testList << 93.8436 << 777.797 << 2507.78 << 5999.44 << 6806.54 << 7481.16
                     << 8008.5 << 8093.8 << 8161.83 << 8216.99
                     << 8280.46 << 8328.4 << 8394.55 << 8469.84
                     << 8500.65 << 8558.16 << 8660.9 << 8638.87
                     << 8726.47 << 8715.25 << 8804.48 << 8793.86
                     << 8839.42 << 8875.75 << 8938.24 << 8977.09
                     << 9020.27 << 9046.7 << 9092.04 << 9121.58
                     << 9155.36 << 9199.46;
    
        axisX()->setRange(0, 100);
        axisY()->setRange(-1, 100);
        m_timer.start();
    }
    
    void Chart::handleTimeout()
    {
        qreal x = plotArea().width() / m_axisX->tickCount();
        qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
        m_x += y;
    
        if (!testList.isEmpty()) {
            m_y = testList.first();
            testList.takeFirst();
        }
    
        qDebug() << "m_x: " << m_x << " " << "m_y: " << m_y;
        m_series->append(m_x, m_y);
        scroll(x, 0);
    }
    
    Chart::~Chart()
    {
    
    }
    

    main.cpp:

    #include "chart.h"
    #include <QChartView>
    #include <QApplication>
    #include <QWidget>
    #include <QHBoxLayout>
    
    QT_CHARTS_USE_NAMESPACE
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QWidget window;
        Chart *chart = new Chart;
        //chart->setTitle("Dynamic spline chart");
        chart->setAnimationOptions(QChart::AllAnimations);
        QChartView chartView(chart);
        chartView.setRenderHint(QPainter::Antialiasing);
        chartView.resize(window.size());
        QHBoxLayout *layout = new QHBoxLayout();
        layout->addWidget(&chartView);
        window.setLayout(layout);
        window.setMinimumSize(810, 400);
        window.show();
        return a.exec();
    }
    

    Any ideas how to fix it? Thanks.

    1 Reply Last reply
    0
    • Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by
      #4

      Hello!

      I have fixed the issue by using QtNetworkMonitor example from GitHub. The issue is resolved.

      1 Reply Last reply
      1
      • Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by
        #2

        I have fixed the issue. Now it works well, but I need to set the axis rect background color.

        Screenshot:

        1456789.png

        Any ideas how to set the axis background color? Thanks.

        1 Reply Last reply
        0
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #3

          Hello!

          I tried to use the QAreaSeries to set the axis rect background. But it displays: "Can not find series on the chart.",
          "Can not find series on the chart." issues and draw incorrectly.

          Screenshot:

          alt text

          Code:
          chart.h

          #ifndef CHART_H
          #define CHART_H
          
          #include <QChart>
          #include <QTimer>
          #include <QAbstractAxis>
          #include <QLineSeries>
          #include <QValueAxis>
          #include <QAreaSeries>
          #include <QDebug>
          
          QT_CHARTS_BEGIN_NAMESPACE
          class QLineSeries;
          class QValueAxis;
          QT_CHARTS_END_NAMESPACE
          
          QT_CHARTS_USE_NAMESPACE
          
          class Chart: public QChart
          {
              Q_OBJECT
          public:
              Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
              void setLabelsOn(bool isOn);
              void setPenStyle(QColor color, int width);
              void setLegendOn(bool isOn);
              void setAxisGridLineOn(bool isOn);
              virtual ~Chart();
          
          public slots:
              void handleTimeout();
          
          private:
              QTimer m_timer;
              QLineSeries *m_series;
              QLineSeries *series1;
              QValueAxis *m_axisX;
              QValueAxis *m_axisY;
              qreal m_x;
              qreal m_y;
              QList<double> testList;
              QAreaSeries *areaSeries;
          };
          
          #endif /* CHART_H */
          

          chart.cpp

          #include "chart.h"
          
          Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags):
              QChart(QChart::ChartTypeCartesian, parent, wFlags),
              m_series(nullptr),
              m_x(5),
              m_y(0)
          {
              m_axisX = new QValueAxis(this);
              m_axisY = new QValueAxis(this);
              m_series = new QLineSeries(this);
              m_series->append(m_x, m_y);
              series1 = new QLineSeries(this);
              series1->append(1, 1);
              testList << 93.8436 << 777.797 << 2507.78 << 5999.44 << 6806.54 << 7481.16
                       << 8008.5 << 1500.5 << 8328.4 << 1998.4 << 9092.04 << 1885.60 << 8093.8 << 8161.83 << 8216.99
                       << 8280.46 << 8394.55 << 8469.84
                       << 8500.65 << 2500.88 << 8558.16 << 8660.9 << 8638.87
                       << 8726.47 << 8715.25 << 8804.48 << 8793.86
                       << 8839.42 << 8875.75 << 8938.24 << 8977.09
                       << 9020.27 << 9046.7 << 9092.04 << 9121.58
                       << 9155.36 << 9199.46 << 10 << 8 << 3 << 1 << 0;
              areaSeries = new QAreaSeries(m_series, series1);
              areaSeries->setBrush(QBrush(QColor(Qt::red)));
              addSeries(areaSeries);
              setAxisX(m_axisX, m_series);
              setAxisY(m_axisY, m_series);
              m_axisX->setTickCount(5);
              m_axisX->setRange(0, 5);
              m_axisY->setRange(0, 9199.4);
              connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
              m_timer.setInterval(1000);
              m_timer.start();
          }
          
          void Chart::setAxisGridLineOn(bool isOn)
          {
              m_axisX->setGridLineVisible(isOn);
              m_axisY->setGridLineVisible(isOn);
          }
          
          void Chart::setLegendOn(bool isOn)
          {
              if (isOn) {
                  legend()->show();
              } else {
                  legend()->hide();
              }
          }
          
          void Chart::setPenStyle(QColor color, int width)
          {
              QPen pen(color);
              pen.setWidth(width);
              m_series->setPen(pen);
          }
          
          void Chart::setLabelsOn(bool isOn)
          {
              m_axisX->setLabelsVisible(isOn);
              m_axisY->setLabelsVisible(isOn);
          }
          
          void Chart::handleTimeout()
          {
              qreal x = plotArea().width() / m_axisX->tickCount();
              qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
              m_x += y;
          
              if (!testList.isEmpty()) {
                  m_y = testList.first();
                  testList.takeFirst();
              } else {
                  m_timer.stop();
              }
          
              qDebug() << "m_x: " << m_x << " " << "m_y: " << m_y;
              m_series->append(m_x, m_y);
              series1->append(m_y, 0);
              scroll(x, 0);
          }
          
          Chart::~Chart()
          {
          
          }
          

          main.cpp

          #include "chart.h"
          #include <QChartView>
          #include <QApplication>
          #include <QWidget>
          #include <QHBoxLayout>
          #include <QGraphicsLayout>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              QWidget window;
              window.setWindowTitle("Test Chart Widget");
              Chart *chart = new Chart;
              chart->setLabelsOn(true);
              chart->setPenStyle(Qt::blue, 1);
              chart->setLegendOn(false);
              chart->setBackgroundRoundness(1);
              chart->setAxisGridLineOn(true);
              //chart->setContentsMargins(-60, -25, -25, -40);
              chart->layout()->setContentsMargins(0, 0, 0, 0);
              chart->setAnimationOptions(QChart::AllAnimations);
              QChartView chartView(chart);
              chartView.setStyleSheet("border: 1px solid blue;");
              chartView.setRenderHint(QPainter::Antialiasing);
              QHBoxLayout *layout = new QHBoxLayout();
              layout->setContentsMargins(QMargins());
              layout->addWidget(&chartView);
              window.setLayout(layout);
              window.setMinimumSize(810, 400);
              window.show();
              return a.exec();
          }
          

          How to draw it properly? Thanks.

          1 Reply Last reply
          0
          • Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #4

            Hello!

            I have fixed the issue by using QtNetworkMonitor example from GitHub. The issue is resolved.

            1 Reply Last reply
            1

            • Login

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