c++qtqt5qtcharts

Using createDefaultAxes() only show a small X-axis and small Y-axis with 1 value in QT


According to QT documentation

void QChart::createDefaultAxes() Creates axes for the chart based on the series that have already been added to the chart. Any axes previously added to the chart will be deleted.

Note: This function has to be called after all series have been added to the chart. The axes created by this function will NOT get automatically attached to any series added to the chart after this function has been called. A series with no axes attached will by default scale to utilize the entire plot area of the chart, which can be confusing if there are other series with properly attached axes also present.

So then I first create my series after I have removed all series from qChart object and clear pwm_serie, ao_serie, ai_serie, dai_serie, encoder_serie and input_capture_serie.

void Easy_chart::create_new_series(){
    qChart.removeAllSeries();
    pwm_serie.clear();
    ao_serie.clear();
    ai_serie.clear();
    dai_serie.clear();
    encoder_serie.clear();
    input_capture_serie.clear();
    for(int i = 0; i < PWM_SERIES_LENGTH; i++)
        pwm_serie.append(new QLineSeries());
    for(int i = 0; i < AO_SERIES_LENGTH; i++)
        ao_serie.append(new QLineSeries());
    for(int i = 0; i < AI_SERIES_LENGTH; i++)
        ai_serie.append(new QLineSeries());
    for(int i = 0; i < DAI_SERIES_LENGTH; i++)
        dai_serie.append(new QLineSeries());
    for(int i = 0; i < ENCODER_SERIES_LENGTH; i++)
        encoder_serie.append(new QLineSeries());
    for(int i = 0; i < INPUT_CAPTURE_SERIES_LENGTH; i++)
        input_capture_serie.append(new QLineSeries());
}

Done that! Then I fill my series. For every new value, I call this function.

qChart.addSeries(append_to_series("PWM", pwm_serie.at(index), index, value));

Where pwm_serie.at(index) can be an arbitrary serie. The index is what type of serie we are going to use because the series are declared as:

private:
    QChart qChart;
    QList<QLineSeries*> pwm_serie;
    QList<QLineSeries*> ao_serie;
    QList<QLineSeries*> ai_serie;
    QList<QLineSeries*> dai_serie;
    QList<QLineSeries*> encoder_serie;
    QList<QLineSeries*> input_capture_serie;
    int show_samples;

Then I append values to the serie. What this function does is:

  1. Add a name to the series e.g PWM0 or DAI2 depending on label and index
  2. Append values to the serie if length of theserie is less than show_samples, else it will just replace the values.

And this function return back the pointer QLineSeries*.

QLineSeries* Easy_chart::append_to_series(const QString& label, QLineSeries* serie, int index, float value){
    serie->setName(label + QString::number(index) + "(" + QString::number(value) + ")");
    int count = serie->count();
    if(count >= show_samples){
        for(int i = 0; i < count; i++)
            if(i == count - 1)
                serie->replace(i, i, value);
            else
                serie->replace(i, i, serie->at(i+1).y());
    }else{
        serie->append(count, value);
    }
    return serie;
}

Then finally, I'm calling the famous function of QT: createDefaultAxes();

But then It looks like this.

Questions:

  1. I have 10 values in my PMW0 series. Why does I only se 1 value?
  2. Why aren't the axis automatically scaled?

enter image description here

How to reproduce this example

easy_chart.set_show_samples(10);
easy_chart.create_new_series();
easy_chart.append_to_pwm_serie(0, 10);
easy_chart.append_to_pwm_serie(0, 12);
easy_chart.append_to_pwm_serie(0, 14);
easy_chart.append_to_pwm_serie(0, 16);
easy_chart.append_to_pwm_serie(0, 19);
easy_chart.append_to_pwm_serie(0, 20);
qChart.createDefaultAxes();

Result:

enter image description here


Solution

  • I have an answer. It seems that QT have poor design for Chart class, but there is a way to walk around this problem.

    Once you have add your serie to the chart, you MUST remove it if you want to implmenet the same serie that have more appended points. But be aware, if you remove the serie from the chart, you need to create a new serie as well.

        QLineSeries* serie = new QLineSeries();
        serie->setName("First serie");
        serie->append(0, 1);
        serie->append(1, 2);
        serie->append(2, 2);
        serie->append(3, 6);
        serie->append(4, 5);
        serie->append(5, 4);
        serie->append(6, 8);
        serie->append(7, 1);
        serie->append(8, 5);
        serie->append(9, 9);
        ui->chart_graphicsView->chart()->addSeries(serie);
        ui->chart_graphicsView->chart()->createDefaultAxes();
        ui->chart_graphicsView->chart()->removeAllSeries(); // Step 1: Delete the series
        serie = new QLineSeries(); // Step 2: Create new series
        serie->setName("Second serie");
        serie->append(10, 10);
        serie->append(11, 20);
        serie->append(12, 20);
        serie->append(13, 60);
        serie->append(14, 50);
        serie->append(15, 40);
        serie->append(16, 80);
        serie->append(17, 10);
        serie->append(18, 50);
        serie->append(19, 90);
        ui->chart_graphicsView->chart()->addSeries(serie);
        ui->chart_graphicsView->chart()->createDefaultAxes();
    

    enter image description here

    Here is an example if you want to add multiple series to the chart.

        QLineSeries* serie = new QLineSeries();
        serie->setName("First serie");
        serie->append(0, 1);
        serie->append(1, 2);
        serie->append(2, 2);
        serie->append(3, 6);
        serie->append(4, 5);
        serie->append(5, 4);
        serie->append(6, 8);
        serie->append(7, 1);
        serie->append(8, 5);
        serie->append(9, 9);
        ui->chart_graphicsView->chart()->addSeries(serie);
        ui->chart_graphicsView->chart()->createDefaultAxes();
        ui->chart_graphicsView->chart()->removeAllSeries();
        serie = new QLineSeries();
        serie->setName("Second serie");
        serie->append(10, 10);
        serie->append(11, 20);
        serie->append(12, 20);
        serie->append(13, 60);
        serie->append(14, 50);
        serie->append(15, 40);
        serie->append(16, 80);
        serie->append(17, 10);
        serie->append(18, 50);
        serie->append(19, 90);
        ui->chart_graphicsView->chart()->addSeries(serie);
        QLineSeries* serie2 = new QLineSeries();
        serie2->setName("Third serie");
        serie2->append(10, 15);
        serie2->append(11, 24);
        serie2->append(12, 21);
        serie2->append(13, 66);
        serie2->append(14, 151);
        serie2->append(15, 40);
        serie2->append(16, 84);
        serie2->append(17, 12);
        serie2->append(18, 55);
        serie2->append(19, -45);
        ui->chart_graphicsView->chart()->addSeries(serie2);
        ui->chart_graphicsView->chart()->createDefaultAxes();
    

    enter image description here