날짜 및 시간 축이 포함된 선 그래프
참고: 이 내용은 ‘위젯이 포함된 차트 갤러리’ 예제의 일부입니다.

예제 실행
다음 위치에서 예제를 실행할 수 있습니다.
- Qt Creator
Welcome 모드를 열고 Examples 에서 예제를 선택하십시오. 자세한 내용은 Qt Creator 의 ‘튜토리얼: 빌드 및 실행’을 참조하십시오.
- Qt Extension for Visual Studio Code
Command Palette 에서 Qt: Open Qt examples 명령을 실행하고 목록에서 예제를 선택하십시오. 자세한 내용은 Qt Extension for Visual Studio Code: 튜토리얼: 빌드 및 실행을 참조하십시오.
선 차트 만들기
선 차트를 생성하려면 QLineSeries 인스턴스가 필요합니다. 하나 만들어 봅시다.
auto series = new QLineSeries;이 차트에서는 태양 흑점 수가 시간에 따라 어떻게 변하는지 보여줄 것입니다. 데이터(우주 기상 예보 센터에서 제공)는 텍스트 파일에서 읽어옵니다.
아래 코드 조각에서, ` QDateTime::toMSecsSinceEpoch ` 메서드를 사용하여 ` QDateTime ` 객체를 `QLineSeries`의 `append` 메서드에 전달할 수 있는 숫자로 변환하는 방법을 확인해 보세요.
// data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt
// http://www.swpc.noaa.gov/ftpdir/weekly/README
// http://www.weather.gov/disclaimer
QFile sunSpots(":sun_spots");
if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_loadError = QStringLiteral("Failed to load '%1' file.").arg(sunSpots.fileName());
return false;
}
QTextStream stream(&sunSpots);
while (!stream.atEnd()) {
QString line = stream.readLine();
if (line.startsWith("#") || line.startsWith(":"))
continue;
QStringList values = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
QDateTime momentInTime;
momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15));
series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
}
sunSpots.close();차트에 데이터를 표시하려면 QChart 인스턴스가 필요합니다. 여기에 시리즈를 추가하고, 범례를 숨기며, 기본 축을 생성하고, 차트 제목을 설정합니다.
auto chart = new QChart;
chart->addSeries(series);
chart->legend()->hide();
chart->setTitle("Sunspots count (by Space Weather Prediction Center)");QLineSeries를 사용하기 때문에 createDefaultAxes를 호출하면 X축과 Y축 모두에 QValueAxis가 생성됩니다. QDateTimeAxis를 사용하려면 차트에 수동으로 설정해야 합니다. 먼저 QDateTimeAxis 인스턴스를 생성한 다음, 표시할 눈금 수를 설정합니다. 태양 흑점 수는 월별 평균값으로 제공됩니다. 따라서 축 레이블에 시간 및 요일 정보를 포함할 필요가 없습니다. 이는 사용자 정의 레이블 형식을 설정하여 구현할 수 있습니다. 사용 가능한 형식 옵션에 대해서는 QDateTime::toString() 메서드 문서를 참조하십시오.
auto axisX = new QDateTimeAxis;
axisX->setTickCount(10);
axisX->setFormat("MMM yyyy");
axisX->setTitleText("Date");
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);
auto axisY = new QValueAxis;
axisY->setLabelFormat("%i");
axisY->setTitleText("Sunspots count");
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisY);그런 다음 QChart를 매개변수로 사용하여 QChartView 객체를 생성합니다. 이렇게 하면 QGraphicsView 장면을 직접 생성할 필요가 없습니다. 또한 렌더링된 선이 더 깔끔하게 보이도록 앤티앨리어싱을 활성화합니다.
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.