선형 차트와 막대 차트 결합하기
참고: 이 예제는 ‘위젯을 사용한 차트 갤러리’ 예제의 일부입니다.
이 예제에서는 선 차트와 막대 차트를 결합하고, 범주 축을 두 차트 모두에 공통 축으로 사용합니다.

여기서는 막대 시리즈에 사용할 데이터를 생성합니다.
auto set0 = new QBarSet("Jane");
auto set1 = new QBarSet("John");
auto set2 = new QBarSet("Axel");
auto set3 = new QBarSet("Mary");
auto set4 = new QBarSet("Sam");
*set0 << 1 << 2 << 3 << 4 << 5 << 6;
*set1 << 5 << 0 << 0 << 4 << 0 << 7;
*set2 << 3 << 5 << 8 << 13 << 8 << 5;
*set3 << 5 << 6 << 7 << 3 << 4 << 5;
*set4 << 9 << 7 << 5 << 3 << 1 << 2;막대 시리즈를 생성하고 세트를 추가합니다. 각 세트의 첫 번째 값들은 첫 번째 범주에, 두 번째 값들은 두 번째 범주에 그룹화되며, 이 과정이 반복됩니다.
auto barseries = new QBarSeries;
barseries->append(set0);
barseries->append(set1);
barseries->append(set2);
barseries->append(set3);
barseries->append(set4);그런 다음 선 시리즈를 생성하고 데이터를 추가합니다. 데이터가 막대 차트와 일치하도록 하기 위해, 선 시리즈의 x값으로 인덱스를 사용하므로 첫 번째 점은 (0,값), 두 번째 점은 (1,값)에 위치하게 됩니다.
auto lineseries = new QLineSeries;
lineseries->setName("trend");
lineseries->append(QPoint(0, 4));
lineseries->append(QPoint(1, 15));
lineseries->append(QPoint(2, 20));
lineseries->append(QPoint(3, 4));
lineseries->append(QPoint(4, 12));
lineseries->append(QPoint(5, 17));여기서 차트를 생성하고 두 시계열을 모두 추가합니다.
auto chart = new QChart;
chart->addSeries(barseries);
chart->addSeries(lineseries);
chart->setTitle("Line and Bar Chart");차트가 시리즈를 올바르게 표시하도록 하려면 시리즈에 대한 사용자 정의 축을 생성해야 합니다. 사용자 정의 축을 생성하지 않으면 각 시리즈가 차트의 최대 영역을 사용하도록 크기가 조정되어(단일 시리즈의 경우와 마찬가지로) 결과가 부정확해집니다. 사용자 정의 축을 사용하면 두 시리즈의 범위가 동일한 축을 따르도록 설정할 수 있습니다. x축에는 QBarCategoryAxis를, y축에는 QValuesAxis를 사용합니다.
QStringList categories;
categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
auto axisX = new QBarCategoryAxis;
axisX->append(categories);
chart->addAxis(axisX, Qt::AlignBottom);
lineseries->attachAxis(axisX);
barseries->attachAxis(axisX);
axisX->setRange(QString("Jan"), QString("Jun"));
auto axisY = new QValueAxis;
chart->addAxis(axisY, Qt::AlignLeft);
lineseries->attachAxis(axisY);
barseries->attachAxis(axisY);
axisY->setRange(0, 20);또한 범례도 표시하고자 합니다.
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);마지막으로 차트를 뷰에 추가합니다.
createDefaultChartView(chart);© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.