パーセント・バー・チャートの作成

注: これはウィジェット・ギャラリーのチャート例の一部です。

パーセント棒グラフは、カテゴリごとの全セットに対するパーセントとして、セットのデータを表示します。

パーセント棒グラフの作成は、通常の棒グラフの作成と同じですが、パーセント棒グラフでは、QBarSeries の代わりにQPercentBarSeries API を使用します。また、棒グラフでは、Y軸の番号付けを見やすくするために、nice numbers アルゴリズムを使用しました。パーセント棒グラフでは、Y軸の最大値は常に100なので、その必要はありません。

バーセットは、すべてのバーチャートで同じように使用される。様々なバーチャートの違いを説明するために、すべての例で同じデータを使用する。棒グラフによって可視化されるデータは、QBarSet インスタンスによって定義される。ここでは、集合を作成し、それにデータを追加する。データは << 演算子で追加されます。あるいは、append メソッドを使うこともできます。

auto set0 = new QBarSet("Jane");
auto set1 = new QBarSet("John");
auto set2 = new QBarSet("Axel");
auto set3 = new QBarSet("Mary");
auto set4 = new QBarSet("Samantha");

*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 series = new QPercentBarSeries;
series->append(set0);
series->append(set1);
series->append(set2);
series->append(set3);
series->append(set4);

ここで、チャート・オブジェクトを作成し、系列を追加します。setTitleでチャートのタイトルを設定し、setAnimationOptions(QChart::SeriesAnimations)を呼び出してシリーズのアニメーションをオンにします。

auto chart = new QChart;
chart->addSeries(series);
chart->setTitle("Simple Percent Bar Chart");
chart->setAnimationOptions(QChart::SeriesAnimations);

カテゴリを軸に表示するには、まずQBarCategoryAxis 。ここでは、カテゴリのリストを持つカテゴリ軸を作成し、それをチャートに追加して、x軸として動作させます。チャートは軸の所有権を持ちます。Y軸には、左側に配置された値軸を使用します。

QStringList categories {"Jan", "Feb", "Mar", "Apr", "May", "Jun"};
auto axisX = new QBarCategoryAxis;
axisX->append(categories);
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);
auto axisY = new QValueAxis;
chart->addAxis(axisY, Qt::AlignLeft);
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.