負のバーを用いた気温記録の表示
注:これは 「ウィジェット付きチャート ギャラリー」のサンプルの一部です。このサンプルでは、気温データを使用します。

まず、2つのバーセットを作成し、データをそれらに追加します。1つのセットは最低気温を表し、もう1つのセットは最高気温を表します。
auto low = new QBarSet("Min");
auto high = new QBarSet("Max");
*low << -52 << -50 << -45.3 << -37.0 << -25.6 << -8.0
<< -6.0 << -11.8 << -19.7 << -32.8 << -43.0 << -48.0;
*high << 11.9 << 12.8 << 18.5 << 26.5 << 32.0 << 34.8
<< 38.2 << 34.8 << 29.8 << 20.4 << 15.1 << 11.8;シリーズを作成し、そのバーセットを追加します。シリーズがバーセットを管理します。
auto series = new QStackedBarSeries;
series->append(low);
series->append(high);ここで、チャートオブジェクトを作成し、その中にシリーズを追加します。setTitle を使用してチャートのタイトルを設定し、setAnimationOptions(QChart::SeriesAnimations) を呼び出してシリーズのアニメーションを有効にします。
auto chart = new QChart;
chart->addSeries(series);
chart->setTitle("Temperature records in Celcius");
chart->setAnimationOptions(QChart::SeriesAnimations);カテゴリを軸上に表示するには、QBarCategoryAxisを作成する必要があります。ここでは、カテゴリのリストを含むカテゴリ軸を作成し、下部に配置してx軸としてチャートに追加します。チャートが軸の所有権を取得します。y軸には、左側に配置された値軸を使用します。 自動スケーリングよりも見栄えが良くなるため、y軸の範囲を変更します。
QStringList categories = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
auto axisX = new QBarCategoryAxis;
axisX->append(categories);
axisX->setTitleText("Month");
chart->addAxis(axisX, Qt::AlignBottom);
auto axisY = new QValueAxis;
axisY->setRange(-52, 52);
axisY->setTitleText("Temperature [°C]");
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisX);
series->attachAxis(axisY);また、凡例も表示します。そのためには、チャートから凡例のポインタを取得し、可視に設定します。さらに、配置をQt::AlignBottom に設定することで、凡例をチャートの下部に配置します。
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.