Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set custom axis range for QBarSeries chart?
Qt 6.11 is out! See what's new in the release blog

How to set custom axis range for QBarSeries chart?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 579 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DolfostD Offline
    DolfostD Offline
    Dolfost
    wrote on last edited by
    #1

    I have next program:

    #include <QtWidgets>
    #include <QtCharts>
    
    int main(int argc, char *argv[]) {
        QApplication a(argc, argv);
    
        // Create data
        std::vector<int> data = {3, 4, 2, 5, 8, 1, 3};
    
        // Create a Qt Charts series
        QBarSeries *series = new QBarSeries();
    	series->setBarWidth(1);
    
        // Create chart
        QChart *chart = new QChart();
        chart->addSeries(series);
        chart->setTitle("Bar Chart");
        chart->setAnimationOptions(QChart::SeriesAnimations);
    
        // Create axes
        QValueAxis *axisX = new QValueAxis();
        axisX->setRange(2, 13);
        chart->addAxis(axisX, Qt::AlignBottom);
        series->attachAxis(axisX);
    
        QValueAxis *axisY = new QValueAxis();
        axisY->setRange(0, *std::max_element(data.begin(), data.end()));
        chart->addAxis(axisY, Qt::AlignLeft);
        series->attachAxis(axisY);
    
        // Create chart view
        QChartView *chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
    
        // Add data to the series
        QBarSet *set = new QBarSet("Relative frequency");
        for (int value : data) {
            *set << value;
        }
        series->append(set);
    
    
        // Create main window
        QMainWindow window;
        window.setCentralWidget(chartView);
        window.resize(800, 600);
        window.show();
    
        return a.exec();
    }
    

    Screenshot 2024-02-09 at 1.21.55 AM.png

    So the first chart bar is half-hidden, and bars instead of spanning from 2 to 13 on xAxis only go from 2 to 6 I guess. It looks like the x coordinate of bar is linked with the bar index inside barSet. How to make bar chart take all the space on a chart, how to make qbarseries to span entire X axis? I could not find anything in docs. I need something like this:

    Screenshot 2024-02-09 at 1.24.44 AM.png

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved