꺾은선형 차트와 막대형 차트 결합하기

참고: 이 예는 위젯이 있는 차트 갤러리 예제의 일부입니다.

이 예에서는 꺾은선형 차트와 막대형 차트를 결합하고 카테고리 축을 두 차트의 공통 축으로 사용합니다.

여기서는 막대형 시리즈에 대한 데이터를 만듭니다.

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);

© 2025 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.