⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QtCharts problem with Spline

  • I've only recently started using QtCharts with Qt5.6 and testing out the different functionalities.
    I'm trying to use both QBarSeries and QSplineSeries to display two different versions of a histogram.
    However, i keep finding that QSplineSeries will display inconsistent graph compared to the QBarSeries.

    This is the result that I am getting, note that at the red circle, QSpline displays y-value much lower than it should be.
    As the amount of data points increase, so to does this occurrence.

    Can someone help me with this problem?

    Thank you.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you create a minimal compilable example that reproduces this ?

  • #include <stdlib.h>
    #include <QVector>
    #include <QDebug>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QMainWindow>
    #include <QtCharts/QChartView>
    #include <QtCharts/QSplineSeries>
    #include <iostream>
    using namespace std;
    int main(int argc, char *argv[])
          QApplication a(argc, argv);
          std::vector<double> xAxisData;
          std::vector<int> matClassFrequencyData;
          int iClassAmount = 50;
         xAxisData = {-4.05, -3.86, -3.68, -3.50, -3.32, -3.14, -2.96, -2.78, -2.59, -2.41, -2.23, -2.05, -1.87, -1.69, -1.51, -1.33, -1.14, -0.96, -0.78, -0.60, -0.42, -0.24, -0.06, 0.11, 0.30, 0.48, 0.66, 0.84, 1.02, 1.20, 1.38, 1.56, 1.75, 1.93, 2.11, 2.29, 2.47, 2.65, 2.83, 3.02, 3.20, 3.38, 3.56, 3.74, 3.92, 4.10, 4.28, 4.47, 4.65, 4.83, 5.01};
         matClassFrequencyData = {1, 6, 8, 22, 41, 41, 57, 83, 49, 199, 494, 1192, 2515, 5083, 8362, 9537, 5083, 11451, 13713, 28515, 46887, 58029, 243666, 45902, 17001, 19831, 10600, 9167, 7870, 7056, 4508, 2326, 1111, 319, 407, 142, 129, 99, 85, 72, 57, 19, 27, 28, 11, 4, 4, 2, 4, 1};
          QSplineSeries *series = new QSplineSeries();
          double minAxisX,
                 classMark;                            //class mark is the middle point between lower and upper class limit
          minAxisX = xAxisData[0];
          maxAxisX = xAxisData[iClassAmount];
          int maximumFrequency = 0;                 //maximumFrequency used to scale the y-axis
          for (int ir=0; ir < iClassAmount; ir++)
              classMark = (xAxisData[ir] + xAxisData[ir+1])/2;
              series->append(classMark, matClassFrequencyData[ir]);
              if (matClassFrequencyData[ir] > maximumFrequency)    //used to find the maximum y-axis
                  maximumFrequency = matClassFrequencyData[ir];
          QChart *chart = new QChart();
          chart->setTitle("Spline Histogram");
          chart->axisX()->setRange(minAxisX, maxAxisX);
          chart->axisY()->setRange(0, maximumFrequency);
          QChartView *chartView = new QChartView(chart);
          QMainWindow window;
          window.resize(400, 300);
          return a.exec();

    The problem is the inaccurate decrease of the Spline histogram shown when frequency is between 5083, 11451, 13713, 28515, 46887, 58029, 243666 which is supposed to show an increase instead of a random decrease prior to the jump to max frequency at 243666.

  • Moderators

    I don't use QtCharts, since it's (still) part of the commercial license, but I noticed you have 52 elements in xAxisData and 50 elements in matClassFrequencyData, while iClassAmount is 50. Is this a typo? If not I think you should fix that as well (you seem to have a rogue x point).

    Additionally, your x data is not monotonically increasing, so when you calculate the average, as you do, you get the artifact (the points get reordered).

    Kind regards.

  • @kshegunov
    Well spotted, there are supposed to be 51 elements in xAxisData (the extra one is an initial value).
    The vector contains class limits which will be averaged into class mark and used on the x axis for QSpline.

    The x axis should now increase incrementally from -4.05 to 5.01 with respective frequency for the y-axis.

    My only problem is the unexplained decrease when frequency is between x-axis (-1.14 to 0.11) with corresponding y-axis (5083 to 46887) which increases but gets displayed with a decrease.
    the decrease shown in red circle here (and what the graph should be): http://imgur.com/KTddg4I

  • Moderators

    One thing you could check very quickly is whether a simple non-interpolating chart (as lines chart, like QLineSeries) behaves. If you don't get such a decrease there I'd assume it's a bug in the interpolation routine.

    Kind regards.

  • QLineSeries seem to have no problem displaying the correct chart: http://i.imgur.com/B4odST9.png
    which begs to differ, is this a known bug with the Spline function and I'm wondering if there are ways to fix it.

    Thank you.

  • Moderators


    which begs to differ, is this a known bug with the Spline function

    I don't know, as I said I don't use the module, but you could look here (you can login with your forum credentials).
    One thing that caught my eye though is this bug, which doesn't speak well of the implementation of the charts module, as the developer provided some lazy excuse (this is simply implemented by notifying the owner of the destruction).
    Anyway, you could either file a bug report or try something else. My father (who uses a commercial license) gave up on the charts and is currently using QCustomPlot, so you could try that if it provides what you're after.

    I hope that helps.
    Kind regards.

Log in to reply