折れ線グラフと棒グラフの組み合わせ
注:これは 「ウィジェット付きグラフギャラリー」のサンプルの一部です。
この例では、折れ線グラフと棒グラフを組み合わせ、カテゴリ軸を両方の共通軸として使用しています。

ここでは、棒グラフシリーズ用のデータを作成します。
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;棒グラフシリーズを作成し、そこにセットを追加します。各セットの最初の値は最初のカテゴリに、2番目の値は2番目のカテゴリに、というようにグループ化されます。
auto barseries = new QBarSeries;
barseries->append(set0);
barseries->append(set1);
barseries->append(set2);
barseries->append(set3);
barseries->append(set4);次に、折れ線シリーズを作成し、データを追加します。データを棒グラフと一致させるため、折れ線シリーズの X 値としてインデックスを使用します。これにより、最初の点は (0, 値)、2 番目の点は (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.