マイナス・バーによる気温記録の表示
注: これはウィジェット付きチャート・ギャラリーの例の一部です。この例では温度データを使用します。
まず、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);
これでチャートを表示する準備が整いました。
© 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.