Results 1 to 1 of 1

Thread: QChart Questions

  1. #1
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QChart Questions

    I am creating a simple chart application:

    * Create chart with two series using a value y-axis and a datetime x-axis
    * Add data to the chart via a timer
    * Scale the chart manually when data is added using setRange

    The issues I'm having are:

    * In general, I don't call createdefaultaxes()...I can't get the x-axis to adjust ever using setRange of setMax...why?
    * I've tried updating the chartview as well...still no update

    This is the chart setup:
    Qt Code:
    1. // Create line series
    2. ser1 = new QLineSeries();
    3. ser2 = new QLineSeries();
    4.  
    5. // Create chart
    6. chart = new QChart();
    7. chart->addSeries(ser1);
    8. chart->addSeries(ser2);
    9. chart->legend()->hide();
    10. chart->setTitle("Test Chart");
    11. //chart->createDefaultAxes();
    12.  
    13. QDateTimeAxis *axisX = new QDateTimeAxis;
    14. axisX->setTickCount(10);
    15. axisX->setFormat("MM-dd-yy hh:mm:ss");
    16. axisX->setLabelsAngle(-90);
    17. axisX->setTitleText("Date");
    18. //chart->addAxis(axisX, Qt::AlignBottom);
    19. chart->setAxisX(axisX);
    20. chart->axisX()->setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime().addSecs(15));
    21. ser1->attachAxis(axisX);
    22. ser2->attachAxis(axisX);
    23.  
    24. QValueAxis *axisY = new QValueAxis;
    25. axisY->setLabelFormat("%i");
    26. axisY->setTitleText("Values");
    27. //chart->addAxis(axisY, Qt::AlignLeft);
    28. chart->setAxisY(axisY);
    29. chart->axisY()->setRange(-2, 2);
    30. ser1->attachAxis(axisY);
    31. ser2->attachAxis(axisY);
    32.  
    33. // createDefaultAxes() - critical for autoscaling
    34. //chart->createDefaultAxes();
    35. //chart->setAxisX(axisX);
    36. //chart->setAxisY(axisY);
    37. //chart->axisX()->setRange(QDateTime::currentDateTime(), QDateTime::currentDateTime().addSecs(15));
    38. //chart->axisY()->setRange(-2, 2);
    39. //ser1->attachAxis(axisX);
    40. //ser2->attachAxis(axisX);
    41. //ser1->attachAxis(axisY);
    42. //ser2->attachAxis(axisY);
    43.  
    44. // Setup view
    45. chartView = new QChartView(chart);
    46. chartView->setRenderHint(QPainter::Antialiasing);
    47. ui->vLayout->addWidget(chartView);
    48. chartView->setUpdatesEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    This is the timer:
    Qt Code:
    1. void MainWindow::AddData()
    2. {
    3. static int data = 0;
    4.  
    5. ++data;
    6. if (data > 359) {
    7. data = 0;
    8. }
    9. ser1->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), qSin(qDegreesToRadians(double(data))));
    10. ser2->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), qCos(qDegreesToRadians(double(data))));
    11. // Rescale
    12. QVector<QPointF> vec(ser1->pointsVector());
    13. std::sort(vec.begin(), vec.end(), xLessThan);
    14. chart->axisX()->setRange(vec.first().x(), vec.last().x());
    15. chart->axisX()->setMax(vec.last().x());
    16. chartView->update();
    17. //ui->vLayout->update();
    18. qDebug() << "Added data for " << data << " : " << QDateTime::currentDateTime().toMSecsSinceEpoch();
    19. qDebug() << QString::number(long(vec.first().x())) << " : " << QString::number(long((vec.last().x())));
    20. }
    To copy to clipboard, switch view to plain text mode 


    Added after 58 minutes:


    Disregard...wrong units! The chart xAxis is data time but the series values are epoch cnt:

    QVector<QPointF> vec(ser1->points().toVector());
    std::sort(vec.begin(), vec.end(), xLessThan);
    chart->axisX()->setMax(QDateTime::fromMSecsSinceEpoch(vec.last(). x()).addSecs(15));
    Last edited by rhb327; 17th May 2017 at 13:03.

Similar Threads

  1. qchart hide legend for part of series
    By marrierius in forum Newbie
    Replies: 5
    Last Post: 9th September 2021, 15:11
  2. adding new QChart in a QChartView
    By zemlemer in forum Newbie
    Replies: 3
    Last Post: 7th September 2019, 17:02
  3. Dynamically updating QChart
    By nbkhwjm in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2017, 18:06
  4. contextMenuEvent works partially in QChart
    By marrierius in forum Newbie
    Replies: 0
    Last Post: 16th December 2016, 19:48
  5. A few questions about Qt/Win
    By Bojan in forum Installation and Deployment
    Replies: 3
    Last Post: 16th January 2006, 10:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.