QCustomPlot i QT C++ - Hur kan jag lägga till värden och få automatisk skalning på plotten?

Permalänk

QCustomPlot i QT C++ - Hur kan jag lägga till värden och få automatisk skalning på plotten?

Jag har två korta koder här.
Denna kod skapar plottarna och sedan så ger den varje plot en så kallad "legend" eller "titel" för varje linje på plotten.
Argumentet QList<int> &dataTypeIDList innehåller datatyper av from av ID-nummer t.ex. 48 betyder float och 49 betyder int. Till exempelvis med andra ord. Detta är bara till för att jag ska veta vilken namn från argumentet QStringList &columnNameList som skall med då QStringList &columnNameList har samma antal element som QList<int> &dataTypeIDList i samma indexering.

void MainWindow::slotCreatePlotWithLegends(QStringList &columnNameList, int sampleTime, int showSamples, QList<int> &dataTypeIDList){ /* Clear the plot */ ui->plot->clearGraphs(); ui->plot->legend->clear(); ui->plot->legend->setVisible(true); this->sampleTime = sampleTime; this->showSamples = showSamples; countedSamples = 0; int graphIndex = 0; /* Loop the show in plot variable */ for(int i = 0; i < columnNameList.length(); i++){ if(dataTypeIDList.at(i) == QMetaType::Float){ /* Create a graph */ ui->plot->addGraph(); /* Set the legend as the column name */ ui->plot->graph(graphIndex)->setName(columnNameList.at(i)); /* Set the 8-bit color at the line and legend as well */ int r = rand() % 255; int g = rand() % 255; int b = rand() % 255; ui->plot->graph(graphIndex)->setPen(QPen(QColor(r, g, b))); /* Incriment */ graphIndex++; } } /* Updates the legends */ ui->plot->replot(); }

Sedan har jag denna kod. Thanken med detta kod är att datat i QVector<QVariant> &listItemValue skall överföras till grafen. QVector<QVariant> &listItemValue innehåller olika värden och datatyper. Som tur så hjälper QList<int> &dataTypeIDList argumentet hålla koll på vilken datatyp som har rätt ID.

Problemet är just denna kod nedan. Plotten har ingen automatisk skalning.

void MainWindow::slotUpdateChartWithMeasurements(QVector<QVariant> &listItemValue, QList<int> &dataTypeIDList){ int graphIndex = 0; for(int i = 0; i < listItemValue.length(); i++){ if(dataTypeIDList.at(i) == QMetaType::Float){ /* Remove the first point */ if(ui->plot->graph(graphIndex)->data()->size() >= showSamples){ double sortKey = ui->plot->graph(graphIndex)->data()->at(0)->sortKey(); ui->plot->graph(graphIndex)->data()->remove(sortKey); } ui->plot->graph(graphIndex)->addData(countedSamples*sampleTime, listItemValue.at(i).toFloat()); graphIndex++; } } /* Updates the legends */ ui->plot->replot(); /* Count the samples */ countedSamples++; }

Här kan ni se en liten liten röd linje. Ibland syns den bättre.

Så är ser min inställning ut för plotten. Jag vill ha automatisk skalning. Hur får jag det?

void MainWindow::setPlotSettings(){ /* This linde only make one empty chart so it looks like a big large plot at the beginning of the start up */ ui->plot->addGraph(); /* Tell plot that there are one sub layout at index row = 1 and column = 0 */ ui->plot->plotLayout()->addElement(1, 0, &subLayout); /* change the fill order of the legend, so it's filled left to right in columns */ ui->plot->legend->setFillOrder(QCPLegend::foColumnsFirst); /* set legend's row stretch factor very small so it ends up with minimum height */ ui->plot->plotLayout()->setRowStretchFactor(1, 0.001); /* Add the legends there inside the sub layout */ subLayout.addElement(0, 0, ui->plot->legend); subLayout.setMargins(QMargins(5, 0, 5, 5)); <------------- Här måste jag ha automatisk skalning. Hur får jag det? /* initialize random seed: */ srand(time(NULL)); }

En annan sak som jag skulle vilja fråga.
Gör jag rätt när jag tar bort första elementet, och sedan lägger till ett nytt element?
Alltså i denna kod.

if(ui->plot->graph(graphIndex)->data()->size() >= showSamples){ double sortKey = ui->plot->graph(graphIndex)->data()->at(0)->sortKey(); ui->plot->graph(graphIndex)->data()->remove(sortKey); } ui->plot->graph(graphIndex)->addData(countedSamples*sampleTime, listItemValue.at(i).toFloat());

Jag antar att sortKey är x-axel dvs countedSamples*sampleTime ? och när jag tar bort en sortkey så tar jag även bort samma värde som fanns i listItemValue.at(i)?

Ja, detta ska ske i realtid.

Permalänk
Medlem

Första resultatet på Google gav följande tråd https://www.qcustomplot.com/index.php/support/forum/1100
Använd QCustomPlot::rescaleAxes efter du lagt till ditt dataset. Anropa därefter replot för att se förändringen ( https://www.qcustomplot.com/documentation/classQCustomPlot.ht... )
Dvs. Innan ui->plot->replot(); kör du ui->plot->rescaleAxes();

Visa signatur

NZXT H510 Flow MSI B450 Tomahawk MAX
AMD Ryzen 5800X3D RX 7900XTX Kingston Fury 64GB
LG C2 42" 4K@120Hz AOC Q27G2U 1440P@144Hz

Permalänk
Skrivet av Pamudas:

Första resultatet på Google gav följande tråd https://www.qcustomplot.com/index.php/support/forum/1100
Använd QCustomPlot::rescaleAxes efter du lagt till ditt dataset. Anropa därefter replot för att se förändringen ( https://www.qcustomplot.com/documentation/classQCustomPlot.ht... )
Dvs. Innan ui->plot->replot(); kör du ui->plot->rescaleAxes();

Nu fungerar det!
Jag har sett denna sida förut som du länkade till. Men jag förstod inte att jag skulle använda detta kommando.

ui->plot->rescaleAxes();