我想将QPolarChart中角轴的 Angular 替换为
一些文字,如附图所示(例如,代替90.0->“Cat 1”)。
我尝试了QCategoryAxis,但在这种情况下,类别出现在轴之间(例如,介于90度和180度之间)。

现在,我使用以下代码来创建极坐标图:

QPolarChart *chart = new QPolarChart();

QValueAxis *angularAxis = new QValueAxis();
angularAxis->setRange(0, 360);

QLineSeries *series1 = new QLineSeries();
series1->append(0, 60);
series1->append(90, -60);
series1->append(180, 50);
series1->append(270, 62);
series1->append(360, 60);

QLineSeries *series2 = new QLineSeries();
series2->append(0, 0);
series2->append(90, 0);
series2->append(180, 0);
series2->append(270, 0);
series2->append(360, 0);

chart->addSeries(series1);
chart->addSeries(series2);
chart->addAxis(angularAxis, QPolarChart::PolarOrientationAngular);

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

series1->attachAxis(radialAxis);
series1->attachAxis(angularAxis);
series2->attachAxis(radialAxis);
series2->attachAxis(angularAxis);

radialAxis->setRange(-100, 100);

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

ui->mainPlot->addWidget(chartView);

c++ - Qt:具有分类角轴的极坐标图-LMLPHP

最佳答案

解决方案是使用QCategoryAxis,但在labelsPosition中使用 QCategoryAxis::AxisLabelsPositionOnValue

#include <QApplication>

#include <QtCharts>
using namespace QtCharts;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPolarChart *chart = new QPolarChart();

    QCategoryAxis *angularAxis = new QCategoryAxis;
    angularAxis->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue);
    angularAxis->setRange(0, 360);
    angularAxis->append("Cat 1", 90);

    QLineSeries *series1 = new QLineSeries();
    *series1 << QPointF(0, 60) << QPointF(90, -60) << QPointF(180, 50) << QPointF(270, 62) << QPointF(360, 60);

    QLineSeries *series2 = new QLineSeries();
    *series2 << QPointF(0, 0)<< QPointF(90, 0) << QPointF(180, 0) << QPointF(270, 0) << QPointF(360, 0);

    chart->addSeries(series1);
    chart->addSeries(series2);
    chart->addAxis(angularAxis, QPolarChart::PolarOrientationAngular);

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

    series1->attachAxis(radialAxis);
    series1->attachAxis(angularAxis);
    series2->attachAxis(radialAxis);
    series2->attachAxis(angularAxis);

    radialAxis->setRange(-100, 100);

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

    chartView->show();

    return a.exec();
}

c&#43;&#43; - Qt:具有分类角轴的极坐标图-LMLPHP

关于c++ - Qt:具有分类角轴的极坐标图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50000071/

10-11 18:02