極座標グラフの作成

注:これは 「ウィジェット付きチャート ギャラリー」のサンプルの一部です。

また、極座標グラフのスクロールやズームの実装方法に加え、極座標グラフとデカルト座標グラフが互いにどのように関連しているかを視覚的に示しています。

青い点が散らばるスプラインと、星の外側・内側および面積がすべて極座標グラフ上に示されたスクリーンショット

極座標チャートの作成は、QChart インスタンスではなく QPolarChart インスタンスを使用して行います。

auto chart = new QPolarChart;

軸の作成方法はデカルト座標系チャートと同様ですが、チャートに軸を追加する際は、配置の代わりに極座標の向きを使用できます。

auto angularAxis = new QValueAxis;
angularAxis->setTickCount(9); // First and last ticks are co-located on 0/360 angle.
angularAxis->setLabelFormat("%.1f");
angularAxis->setShadesVisible(true);
angularAxis->setShadesBrush(QBrush(QColor(249, 249, 255)));
chart->addAxis(angularAxis, QPolarChart::PolarOrientationAngular);

auto radialAxis = new QValueAxis;
radialAxis->setTickCount(9);
radialAxis->setLabelFormat("%d");
chart->addAxis(radialAxis, QPolarChart::PolarOrientationRadial);

極座標チャートのズームおよびスクロールは、論理的には直交座標チャートのズームおよびスクロールとほぼ同じです。主な違いは、X 軸(角度軸)に沿ってスクロールする場合、ピクセル数ではなく角度が使用される点です。もう 1 つの違いは、矩形へのズームができないことです。

void PolarChartView::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Plus:
        chart()->zoomIn();
        break;
    case Qt::Key_Minus:
        chart()->zoomOut();
        break;
    case Qt::Key_Left:
        chart()->scroll(-1.0, 0);
        break;
    case Qt::Key_Right:
        chart()->scroll(1.0, 0);
        break;
    case Qt::Key_Up:
        chart()->scroll(0, 1.0);
        break;
    case Qt::Key_Down:
        chart()->scroll(0, -1.0);
        break;
    case Qt::Key_Space:
        switchChartType();
        break;
    default:
        QGraphicsView::keyPressEvent(event);
        break;
    }
}

デカルト座標系チャートと極座標系チャートの両方で同じ軸や系列を使用できますが、同時に使用することはできません。チャートの種類を切り替えるには、まず古いチャートから系列と軸を削除し、その後、新しいチャートにそれらを追加する必要があります。軸の範囲を維持したい場合は、それらもコピーする必要があります。

void PolarChartView::switchChartType()
{
    QChart *newChart;
    QChart *oldChart = chart();

    if (oldChart->chartType() == QChart::ChartTypeCartesian)
        newChart = new QPolarChart;
    else
        newChart = new QChart;

    // Move series and axes from old chart to new one
    const QList<QAbstractSeries *> seriesList = oldChart->series();
    const QList<QAbstractAxis *> axisList = oldChart->axes();
    QList<QPair<qreal, qreal> > axisRanges;

    for (QAbstractAxis *axis : axisList) {
        auto valueAxis = static_cast<QValueAxis *>(axis);
        axisRanges.append(QPair<qreal, qreal>(valueAxis->min(), valueAxis->max()));
    }

    for (QAbstractSeries *series : seriesList)
        oldChart->removeSeries(series);

    for (QAbstractAxis *axis : axisList) {
        oldChart->removeAxis(axis);
        newChart->addAxis(axis, axis->alignment());
    }

    for (QAbstractSeries *series : seriesList) {
        newChart->addSeries(series);
        for (QAbstractAxis *axis : axisList)
            series->attachAxis(axis);
    }

    int count = 0;
    for (QAbstractAxis *axis : axisList) {
        axis->setRange(axisRanges[count].first, axisRanges[count].second);
        count++;
    }

    newChart->setTitle(oldChart->title());
    setChart(newChart);
    delete oldChart;
}

© 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.