QPolarChart 的反轴

Reverse axis of QPolarChart

我需要修改一个QPolarChart to create a Satellite SkyPlot.

为此,我需要反转径向轴(仰角轴),使 90 位于原点,0 位于外环。

list of members for QValueAxis I found the method setReverse中,这似乎满足了我的需要。

但是,在高程轴上执行 setReverse(true) 并没有像我预期的那样翻转轴。

那我怎样才能反转轴?

这是 运行 这个例子所需的最少代码:

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QPolarChart>
#include <QtCharts/QValueAxis>
#include <QScatterSeries>

QT_CHARTS_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QtCharts::QPolarChart *chart = new QtCharts::QPolarChart();
    chart->legend()->hide();

    QScatterSeries *series = new QScatterSeries();
    for (int i = 0; i < 360; i += 10) {
        series->append(i, i);
    }
    chart->addSeries(series);

    QValueAxis *azimuthAxis = new QValueAxis();
    azimuthAxis->setRange(0, 360);
    azimuthAxis->setTickCount(9);
    azimuthAxis->setLabelFormat("%d");
    azimuthAxis->setLabelsVisible(true);
    chart->addAxis(azimuthAxis, QPolarChart::PolarOrientationAngular);

    QValueAxis *elevationAxis = new QValueAxis();
    elevationAxis->setRange(0, 90);
    elevationAxis->setTickCount(7);
    elevationAxis->setLabelFormat("%d");
    elevationAxis->setLabelsVisible(true);
    elevationAxis->setReverse(true); // <-- REVERSE THE AXIS
    chart->addAxis(elevationAxis, QPolarChart::PolarOrientationRadial);

    QtCharts::QChartView *chartView = new QtCharts::QChartView();
    chartView->setChart(chart);
    chartView->setRenderHint(QPainter::Antialiasing);

    QMainWindow window;
    window.setCentralWidget(chartView);
    window.resize(800, 600);
    window.show();

    return app.exec();
}

更新:

看完:

You may check with isReverse if the command setReverse was taken into account

我通过在elevationAxis->setReverse(true);之前和之后立即放置qDebug() << elevationAxis->isReverse();来进行测试,它分别打印出falsetrue

qDebug() << elevationAxis->isReverse(); // <-- Prints out "false"
elevationAxis->setReverse(true);        // <-- REVERSE THE AXIS
qDebug() << elevationAxis->isReverse(); // <-- Prints out "true"

所以 reverse 属性 确实改变了。然而,这种变化并没有在视觉上反映在坐标轴上。

reverse 属性 的 documentation 为:

This property holds whether a reverse axis is used.

By default, the value is false.

The reverse axis is supported with a line, spline, and scatter series, as well as an area series with a cartesian chart. All axes of the same orientation attached to the same series must be reversed if one is reversed or the behavior is undefined.

极坐标图肯定不是笛卡尔图,所以这可能是问题的根源。

我无法使 setReversed(true) 以任何方式工作。

所以我找到了另一个解决方案:手动执行。

我把这个系列反着印了:

QScatterSeries *series = new QScatterSeries();
for (int i = 0; i < 360; i += 10) {
    series->append(i, MAX_ELEVATION - i);
}

MAX_ELEVATION 等于 90.

然后我颠倒了标签名称。为此,我不得不用派生的 QCategoryAxis.

替换 QValueAxis

完整代码

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QPolarChart>
#include <QtCharts/QValueAxis>
#include <QScatterSeries>
#include <QtCharts/QCategoryAxis>

QT_CHARTS_USE_NAMESPACE

#define MAX_ELEVATION 90

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QtCharts::QPolarChart *chart = new QtCharts::QPolarChart();
    chart->legend()->hide();

    QScatterSeries *series = new QScatterSeries();
    for (int i = 0; i < 360; i += 10) {
        series->append(i, MAX_ELEVATION - i);
    }
    chart->addSeries(series);

    QValueAxis *azimuthAxis = new QValueAxis();
    azimuthAxis->setRange(0, 360);
    azimuthAxis->setTickCount(9);
    azimuthAxis->setLabelFormat("%d");
    azimuthAxis->setLabelsVisible(true);
    chart->addAxis(azimuthAxis, QPolarChart::PolarOrientationAngular);

    /*
    QValueAxis *elevationAxis = new QValueAxis();
    elevationAxis->setRange(0, 90);
    elevationAxis->setTickCount(7);
    elevationAxis->setLabelFormat("%d");
    elevationAxis->setLabelsVisible(true);
    elevationAxis->setReverse(true); // <-- REVERSE THE AXIS
    chart->addAxis(elevationAxis, QPolarChart::PolarOrientationRadial);
    */

    QCategoryAxis *elevationAxis = new QCategoryAxis;
    elevationAxis->setRange(0, MAX_ELEVATION);
    for(unsigned int i = 0; i <= MAX_ELEVATION; i += 15)
        elevationAxis->append(QString::number(MAX_ELEVATION-i), i);
    elevationAxis->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue);
    elevationAxis->setLabelsVisible(true);
    chart->addAxis(elevationAxis, QPolarChart::PolarOrientationRadial);


    QtCharts::QChartView *chartView = new QtCharts::QChartView();
    chartView->setChart(chart);
    chartView->setRenderHint(QPainter::Antialiasing);

    QMainWindow window;
    window.setCentralWidget(chartView);
    window.resize(800, 600);
    window.show();

    return app.exec();
}

相当于setReversed(true)应该做的。它将完全按照您想要的方式显示您的系列,而无需修改任何数据计算方式。

在这里你可以看到它的样子:

它对我来说很成功。

希望对您有所帮助。