QCustomPlot Discussion and Comments

v1.3.2 - Cannot plot charts with existing functionsReturn to overview

I'm trying to set up 3 separate charts in my Qt form, but I can't seem to get them to display any data.

For example, I have this code setup:

#include "../QCustomPlot/qcustomplot.h"
#include <QPen>
#include <QColor>

MainFrm::MainFrm(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainFrm)
{
    QPen linePen;
    linePen.setStyle(Qt::SolidLine);
    linePen.setWidthF(2.5);
    linePen.setColor(QColor(0, 0, 255));
    QPen scatterPen;
    scatterPen.setStyle(Qt::DotLine);
    scatterPen.setWidth(1);
    scatterPen.setColor(QColor(0, 0, 255));

    ui->lineChart->setBackground(QBrush(QColor(238, 238, 238)));
    ui->lineChart->addGraph();
    ui->lineChart->graph(0)->clearData();
    ui->lineChart->graph(0)->setPen(linePen);
    ui->lineChart->graph(0)->setLineStyle(QCPGraph::lsLine);
    ui->lineChart->xAxis->setTickLabels(true);
    ui->lineChart->yAxis->setTickLabels(true);

    ui->scatterChart->setBackground(QBrush(QColor(238, 238, 238)));
    ui->scatterChart->addGraph();
    ui->scatterChart->graph(0)->clearData();
    ui->scatterChart->graph(0)->setPen(scatterPen);
    ui->scatterChart->graph(0)->setLineStyle(QCPGraph::lsNone);
    ui->scatterChart->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4));
    ui->scatterChart->xAxis->setTickLabels(true);
    ui->scatterChart->yAxis->setTickLabels(true);

    ui->scatterChart2->setBackground(QBrush(QColor(238, 238, 238)));
    ui->scatterChart2->addGraph();
    ui->scatterChart2->graph(0)->clearData();
    ui->scatterChart2->graph(0)->setPen(scatterPen);
    ui->scatterChart2->graph(0)->setLineStyle(QCPGraph::lsNone);
    ui->scatterChart2->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4));
    ui->scatterChart2->xAxis->setTickLabels(true);
    ui->scatterChart2->yAxis->setTickLabels(true);
}

And I try to add data in a separate function:

void MainFrm::addValues()
{
    double value1 = 0.000001;
    double value2 = 0.000010;
    this->ui->scatterChart->graph(0)->clearData();
    this->ui->scatterChart->graph(0)->addData(value1, value2);
    this->ui->scatterChart->rescaleAxes();
}

But when the addValues function is called, no data appears in the selected chart. Is there something I'm doing wrong? The code compiles fine, I have promoted the QWidgets to QCustomPlot correctly, and I have included printsupport for Qt version 5.5.1.

Hi,

if you go through the basic plotting tutorial, you'll notice that it is up to you to refresh the plot display (because in most situations, the programmer knows best when more changes are about to be performed, and when it makes sense to present them to the user).
You need to call the QCustomPlot::replot() method.