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

QScatterSeries + QChartView scaling issue

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 480 Views 2 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.
  • Z Offline
    Z Offline
    ZNohre
    wrote on last edited by
    #1

    All,

    I'm running into (what I assume is) a novice issue when adding some charts to the dashboard of my desktop app.

    When running tests with QScatterSeries with a QDateTimeAxis (X) and QValueAxis (Y) the default axis scaling is putting the oldest and newest points right in the corners of the chart. Is there a preferred method of more appropriately displaying these points (i.e. with some padding to the X- and Y-axis)?

    Reviewing the documentation for QChart and QScatterSeries I tried chart->zoomOut() to no avail. I also saw setMin(const QDateTime &min) and setMax(const QDateTime &max) but wasn't sure if this needed to be added manually, or if there was a more elegant solution?

    b8c44719-fa1c-4f04-afc8-8e74857cf7af-image.png

        QChartView *chartView = new QChartView(this);
        chartView->chart()->setTitle("Chart Title");
    
        QScatterSeries *series = new QScatterSeries(chartView->chart());
        series->setName("Series A");
        series->setMarkerShape(QScatterSeries::MarkerShapeStar);
    
        QDateTime dateTime1 = QDateTime::fromString("2024-01-15 10:00:00", "yyyy-MM-dd hh:mm:ss");
        QDateTime dateTime2 = QDateTime::fromString("2024-01-16 14:30:00", "yyyy-MM-dd hh:mm:ss");
        QDateTime dateTime3 = QDateTime::fromString("2024-01-17 14:30:00", "yyyy-MM-dd hh:mm:ss");
    
        series->append(dateTime1.toMSecsSinceEpoch(), 15);
        series->append(dateTime2.toMSecsSinceEpoch(), 25);
        series->append(dateTime3.toMSecsSinceEpoch(), 35);
    
        chartView->chart()->addSeries(series);
    
        //X axis
        QDateTimeAxis *axisX = new QDateTimeAxis();
        axisX->setFormat("yyyy-MM-dd hh:mm");
        chartView->chart()->addAxis(axisX, Qt::AlignBottom);
        series->attachAxis(axisX);
    
        //Y axis
        QValueAxis *axisY = new QValueAxis();
        chartView->chart()->addAxis(axisY, Qt::AlignLeft);
        series->attachAxis(axisY);
    
        ui->chartGroup1->layout()->addWidget(chartView);
    

    Thanks

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZNohre
      wrote on last edited by
      #2

      Maybe there is a more elegant solution out there but after some more digging I ended up implementing a simple min/max call here for each axis which is working fine.

          //Axis margins
          int x = 5;
          int y = 5;
      
          //X axis margin
          axisX->setMin(axisX->min().addDays(-x));
          axisX->setMax(axisX->max().addDays(x));
      
          //Y axis margin
          axisY->setMin(axisY->min()-y);
          axisY->setMax(axisY->max()+y);
      
      1 Reply Last reply
      0
      • Z ZNohre has marked this topic as solved on
      • Z ZNohre has marked this topic as solved on
      • A.A.SEZENA Offline
        A.A.SEZENA Offline
        A.A.SEZEN
        wrote on last edited by A.A.SEZEN
        #3

        I use the same method to place candles. I considered sharing it for you, but I decided against it because it might be a more practical solution or might be too complicated. I'm sharing it for variety and to provide the code, as I use it.

           auto axis = dynamic_cast<QDateTimeAxis *>(a);
           QDateTime min, max; 
           const unsigned long long imsec = classes->settingsManager->intervalMsec(klines->interval()) / 2;
           using namespace std::chrono;
           time_t timeLast = (static_cast<long>(klines->lastDate() + imsec)) / 1000;
           time_t timeFirst = (static_cast<long>(klines->firstDate() + imsec)) / 1000;
           min = QDateTime::fromString(ctime(&timeFirst));
           max = QDateTime::fromString(ctime(&timeLast));
           if (min != axis->min() || max != axis->max()) {
               axis->setMin(min);
               axis->setMax(max);
               axis->setRange(min, max);
           }
        
        
        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