Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QChart setting axis



  • Hi,
    I'm a beginner in Qt and have some trouble with the QChart widget.

    Here is my code:
    In the constructor:

    ui->setupUi(this);
    m_chart = ui->graphView->chart();
    
    m_xAxis = new QValueAxis;
    m_xAxis->setMax(1200.0);
    m_xAxis->setMin(0.0);
    m_xAxis->setTitleText("Distance");
    m_chart->addAxis(m_xAxis, Qt::AlignBottom);
    
    m_yAxis = new QValueAxis;
    m_yAxis->setMax(25.0);
    m_yAxis->setMin(-25.0);
    m_yAxis->setTitleText("DDM (µA)");
    m_chart->addAxis(m_yAxis, Qt::AlignLeft);
    m_chart->setTitle("Bend limits");
    

    A pushButton with the following code:

    QLineSeries *series = new QLineSeries();
    m_series.append(series);
    QList<QPointF> data;
    for (int i = 0; i < 360; i++)
    {
       series->append(QPointF(i, Sin(qDegreesToRadians(qreal(i)))));
    }
    m_chart->addSeries(series);
    series->setName("Distances");
    series->attachAxis(m_xAxis);
    series->attachAxis(m_yAxis);
    

    A doubleSpinBox for changing the Axis range with the following code:

    double test = ui->doubleSpinBox->value();
    m_xAxis->setRange(0.0, test);
    

    The problem is that each time I change the m_xAxis range a new plot is added.

    First click on Draw button
    8825ed19-4c06-4edf-913b-38e1f624dbaa-image.png

    Setting X max value to 600
    062dd58e-99d0-4986-94d1-39bc9467b9c4-image.png

    What is the reason for this behavior and how can I get rid of it?
    Thank you


Log in to reply