How to update a QCustomPlot data

  c++, charts, qt

So I’m trying to make a bar chart from a database and I managed to do that but each time I update the database the graph is not changing I’ve tried <
this but no use:

void MainWindow::update_graph()
{
    qDebug() <<ui->customPlot->graphCount();
    
    ui->customPlot->removeGraph(0);

    ui->customPlot->replot();
    stats();
}

while this is the graph making code :

void MainWindow::stats()
{
    int m = QDate::currentDate().month(), y = QDate::currentDate().year();


    QVector<double> data = e.data_from_Base(m, y);

    QLinearGradient gradient(0, 0, 0, 400);
    gradient.setColorAt(0, QColor(90, 90, 90));
    gradient.setColorAt(0.38, QColor(105, 105, 105));
    gradient.setColorAt(1, QColor(70, 70, 70));
    ui->customPlot->setBackground(QBrush(gradient));

    QCPBars *Bar_Date = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    Bar_Date->setAntialiased(false);
    Bar_Date->setStackingGap(1);

    Bar_Date->setName(QString::number(y));
    Bar_Date->setPen(QPen(QColor(111, 9, 176).lighter(170)));
    Bar_Date->setBrush(QColor(111, 9, 176));

    //stack bars

    //prepare x
    QVector<double> ticks;
    QVector<QString> labels;
    ticks << 1 << 2 << 3 ;
    labels << Fetch_Month(m) << Fetch_Month(m + 1) << Fetch_Month(m + 2);
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    textTicker->addTicks(ticks, labels);
    ui->customPlot->xAxis->setTicker(textTicker);
    ui->customPlot->xAxis->setTickLabelRotation(60);
    ui->customPlot->xAxis->setSubTicks(false);
    ui->customPlot->xAxis->setTickLength(0, 4);
    ui->customPlot->xAxis->setRange(0, 8);
    ui->customPlot->xAxis->setBasePen(QPen(Qt::white));
    ui->customPlot->xAxis->setTickPen(QPen(Qt::white));
    ui->customPlot->xAxis->grid()->setVisible(true);
    ui->customPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
    ui->customPlot->xAxis->setTickLabelColor(Qt::white);
    ui->customPlot->xAxis->setLabelColor(Qt::white);

    //prepare y
    ui->customPlot->yAxis->setRange(0, *(std::max_element(data.begin(), data.end())));
    ui->customPlot->yAxis->setPadding(5); // a bit more space to the left border
    ui->customPlot->yAxis->setLabel("Nombre des Evenements par mois");
    ui->customPlot->yAxis->setBasePen(QPen(Qt::white));
    ui->customPlot->yAxis->setTickPen(QPen(Qt::white));
    ui->customPlot->yAxis->setSubTickPen(QPen(Qt::white));
    ui->customPlot->yAxis->grid()->setSubGridVisible(true);
    ui->customPlot->yAxis->setTickLabelColor(Qt::white);
    ui->customPlot->yAxis->setLabelColor(Qt::white);
    ui->customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
    ui->customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));

    //set Data
    //QVector<double> doubleData(data.begin(), data.end());
    Bar_Date->setData(ticks, data);

    //setup legend
    ui->customPlot->legend->setVisible(true);
    ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
    ui->customPlot->legend->setBrush(QColor(255, 255, 255, 100));
    ui->customPlot->legend->setBorderPen(Qt::NoPen);
    QFont legendFont = font();
    legendFont.setPointSize(10);
    ui->customPlot->legend->setFont(legendFont);
    ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    //ui->customPlot->graph()->data()->clear();
    //ui->customPlot->replot();
}

I always end up with two charts on top of each other
this is before updating:
Before:

Before

And this after:

after:

after

As you can see I have 2 legends with the same name

Source: Windows Questions C++

LEAVE A COMMENT