How to stop two QBarSeries' overlapping one another.



  • The chart below has two sets of bar series and one overlaps the other. How do you stop this, and put a gap between the two?

    0_1476877558154_Screenshot from 2016-10-19 22:41:19.png

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QChartView>
    #include <QBarSeries>
    #include <QBarSet>
    #include <QLineSeries>
    #include <QLegend>
    #include <QBarCategoryAxis>
    #include <QValueAxis>
    #include <QtSql>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QBarSeries *barseries_hol = new QBarSeries();
        QBarSeries *barseries_econ = new QBarSeries();
        QLineSeries *lineseries_ex = new QLineSeries();
        lineseries_ex->setName("Export");
        QLineSeries *lineseries_im = new QLineSeries();
        lineseries_im->setName("Import");
    
        QStringList categories;
    
        QBarSet *set_holnobsidx = new QBarSet("Holiday Nobs Index");
        QBarSet *set_holnobswidx = new QBarSet("Holiday Nobs Weighted Index");
        QBarSet *set_econeventnobs = new QBarSet("Economic Event Nobs");
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
        db.setHostName("192.168.2.103");
        db.setPort(5433);
        db.setUserName("vorlket");
        db.setPassword("K1156312j");
        db.setDatabaseName("fxproj");
    
        if (db.open())
        {
            QSqlQuery query;
            if (query.exec("SELECT EXTRACT(YEAR FROM t1.month), EXTRACT(MONTH FROM t1.month), t1.holiday_nobsidx, t1.holiday_nobsidx_weighted, t2.econevent_nobs, t3.mtrade_lag3m_export, t3.mtrade_lag3m_import "
                           "FROM (SELECT month, holiday_nobsidx, holiday_nobsidx_weighted FROM audusd.ts_month_holiday) AS t1"
                           " LEFT JOIN (SELECT month, econevent_nobs FROM audusd.ts_month_econevent) AS t2 ON t2.month = t1.month"
                           " LEFT JOIN (SELECT month, mtrade_lag3m_export, mtrade_lag3m_import FROM audusd.ts_month_mtrade) AS t3 ON t3.month = t1.month"
                           " ORDER BY t1.month"
                           " LIMIT 10"))
            {
                int count = 0;
                while (query.next())
                {
                    categories << query.value(0).toString() + "-" + query.value(1).toString();
    
                    *set_holnobsidx << query.value(2).toFloat();
                    *set_holnobswidx << query.value(3).toFloat();
                    *set_econeventnobs << query.value(4).toInt();
    
                    lineseries_ex->append(count, query.value(5).toFloat());
                    lineseries_im->append(count, query.value(6).toFloat());
    
                    count++;
                }
                barseries_hol->append(set_holnobsidx);
                barseries_hol->append(set_holnobswidx);
                barseries_econ->append(set_econeventnobs);
            }
    
            QChart *chart = new QChart();
            chart->addSeries(barseries_hol);
            chart->addSeries(barseries_econ);
            chart->addSeries(lineseries_ex);
            chart->addSeries(lineseries_im);
    
            QBarCategoryAxis *axisX = new QBarCategoryAxis();
            axisX->append(categories);
            chart->addAxis(axisX, Qt::AlignBottom);
            chart->setAxisX(axisX, barseries_hol);
            chart->setAxisX(axisX, barseries_econ);
            chart->setAxisX(axisX, lineseries_ex);
            chart->setAxisX(axisX, lineseries_im);
    
            QValueAxis *axisY_hol = new QValueAxis();
            chart->addAxis(axisY_hol, Qt::AlignLeft);
            chart->setAxisY(axisY_hol, barseries_hol);
            chart->setAxisY(axisY_hol, lineseries_ex);
            chart->setAxisY(axisY_hol, lineseries_im);
            axisY_hol->setRange(0, 2);
    
            QValueAxis *axisY_econ = new QValueAxis();
            chart->addAxis(axisY_econ, Qt::AlignRight);
            chart->setAxisY(axisY_econ, barseries_econ);
            axisY_econ->setRange(0, 1000);
    
            chart->legend()->setVisible(true);
            chart->legend()->setAlignment(Qt::AlignBottom);
    
            ui->chartview->setChart(chart);
            ui->chartview->setRenderHint(QPainter::Antialiasing);
    
            db.close();
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    


  • Hi,
    the example "http://doc.qt.io/qt-5/qtcharts-barchart-example.html" only has one QBarSeries with multiple QBarSet-s in a QChart. Maybe just work with one series.
    -Michael.



  • The difference in the vertical axis scales of the two sets of QBarSet is huge, so binding them together in one bar series would make one too small to be seen. I want to avoid re-scaling of the other bar set if possible. It would be nice to know how to set geometry/location of bar series to enable multiple bar series to appear without overlapping one another.



  • Hi,
    you could probably use two QCharts then.
    -Michael.



  • Actually, setBarWidth method sorta does the job. Make one bar series wider than the other. They still overlap, partially but as a picture, unnoticeable, for now. may run into issues when implementing hovering, clicking and etc.,

    barseries_hol->setBarWidth(0.5);
    barseries_econ->setBarWidth(0.1);
    

    0_1476960480067_Screenshot from 2016-10-20 21:46:24.png

    How do you have two charts in one chartview? The program crashes when I set two charts in one chartview.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.