How to set custom axis range for QBarSeries chart?
Unsolved
General and Desktop
-
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(); }
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: