Qt QPolarChart極座標圖(阿基米德線、四葉曲線、六葉花瓣、三葉花瓣、心形曲線)

一杯清酒邀明月發表於2024-03-21
  QChart還有專門畫極座標的類QPolarChart,它的介面是一個圓盤。注意在使用之前,包括名稱空間QT_CHARTS_USE_NAMESPACE,.pro檔案中也要增加QT += charts.
 1 #include <QApplication>
 2 #include <QDebug>
 3 #include <QtCharts/QScatterSeries>
 4 #include <QtCharts/QLineSeries>
 5 #include <QtCharts/QPolarChart>
 6 #include <QtCharts/QValueAxis>
 7 #include <QtCharts/QChartView>
 8 #include "mainwindow.h"
 9 //[1]
10 QT_CHARTS_USE_NAMESPACE
11  
12 int main(int argc, char *argv[])
13 {
14     QApplication a(argc, argv);
15  
16     //曲線
17     QLineSeries *seriesLine = new QLineSeries();    
18     seriesLine->setName("LinePolar");
19  
20  
21     //圖表
22     QPolarChart *polarChart = new QPolarChart();
23     polarChart->addSeries(seriesLine);
24     polarChart->setTitle("First Polar Chart");
25  
26     //座標軸
27     QValueAxis *angleAxis = new QValueAxis();
28     angleAxis->setTickCount(9);
29     angleAxis->setLabelFormat("%d");
30     //角度
31     polarChart->addAxis(angleAxis, QPolarChart::PolarOrientationAngular);    
32  
33     QValueAxis *radiusAxis = new QValueAxis();
34     radiusAxis->setTickCount(9);
35     radiusAxis->setLabelFormat("%d");
36     //半徑
37     polarChart->addAxis(radiusAxis, QPolarChart::PolarOrientationRadial);
38  
39     seriesLine->attachAxis(radiusAxis);
40     seriesLine->attachAxis(angleAxis);
41  
42     radiusAxis->setRange(0, 360); //設定半徑範圍
43     angleAxis->setRange(0, 360);    //設定角度範圍
44  
45     for (int i = 0; i <= 360; i += 10)
46         seriesLine->append(i, (i / 360.0) * 360);
47  
48     QChartView *polarChartView = new QChartView();
49     polarChartView->setChart(polarChart);
50  
51     MainWindow w;
52     w.SetWidget(polarChartView);
53     w.show();
54  
55     return a.exec();
56 }

修改間隔:

  setTickCount(int count)設定大刻度線的數目,預設是5,不能小於2。程式中設定的是9,但是數一下好像不對。setTickCount(4)換個4體驗一下。對於半徑,第4條線就是圓心,看不太出來。對於角度,第1條和第4條線會重合。

修改主題風格:

  QPolarChart自帶了一些主題,跟手機一樣可以換主題,預設主題就是上面的效果,換個Dark主題:

polarChart->setTheme(QChart::ChartThemeDark);     //設定主題 

  還有藍色,棕色等其他主題,具體主題如下,具體顏色可以自己試試。

設定交替陰影:

  為了實現交替顏色顯示,實現可以透過設定以下陰影屬性實現,預設顏色是黑色。

angleAxis->setShadesVisible(true);

  為了適應不同的風格,顏色是可以自定義的:

angleAxis->setShadesBrush(QBrush(QColor(199, 144, 235)));

改線為點:

  QLineSeries畫出來的是線,將它改成QScatterSeries,畫出來的是獨立的點:

  這個點的大小可以修改,透過setMarkerSize()設定大小

seriesLine->setMarkerSize(1);     //設定點大小

  修改座標可以畫出不同的圖案,比如這樣的螺旋線p=半徑*角度:

  這條綠線是透過多個點組成,顏色也可以改,但是改成曲線的話,畫出的效果不理想,如果誰用曲線畫出來這樣的歡迎告訴我.

 1 for (double i=0; i<=360*2; i+=0.5)    //新增點
 2 {
 3     auto angle = i;
 4     while (angle > 360)
 5     {
 6         angle = angle - 360;
 7     }
 8     seriesLine->append(angle, i/2.0);
 9 }
10 seriesLine->setPen(QPen(QColor(153, 232, 83), 2));    //設定線的顏色

完整的程式碼如下:

 1 #include <QApplication>
 2 #include <QDebug>
 3 #include <QtCharts/QScatterSeries>
 4 #include <QtCharts/QLineSeries>
 5 #include <QtCharts/QPolarChart>
 6 #include <QtCharts/QValueAxis>
 7 #include <QtCharts/QChartView>
 8 #include "mainwindow.h"
 9 //[1]
10 QT_CHARTS_USE_NAMESPACE
11  
12 int main(int argc, char *argv[])
13 {
14     QApplication a(argc, argv);
15  
16     //曲線
17     QScatterSeries *seriesLine = new QScatterSeries();    //點1
18     seriesLine->setName("LinePolar");
19     seriesLine->setMarkerSize(1);     //設定點大小1
20  
21  
22     //圖表
23     QPolarChart *polarChart = new QPolarChart();
24     polarChart->addSeries(seriesLine);
25     polarChart->setTheme(QChart::ChartThemeBlueCerulean);     //設定主題 1
26     polarChart->setTitle("First Polar Chart");
27  
28     //座標軸
29     QValueAxis *angleAxis = new QValueAxis();
30     angleAxis->setTickCount(9);
31     angleAxis->setLabelFormat("%d");
32     angleAxis->setShadesVisible(true);                        //增加shade區域 1
33     angleAxis->setShadesBrush(QBrush(QColor(19, 44, 25)));
34     polarChart->addAxis(angleAxis, QPolarChart::PolarOrientationAngular);
35  
36     QValueAxis *radiusAxis = new QValueAxis();
37     radiusAxis->setTickCount(9);
38     radiusAxis->setLabelFormat("%d");
39     polarChart->addAxis(radiusAxis, QPolarChart::PolarOrientationRadial);
40  
41     seriesLine->attachAxis(radiusAxis);
42     seriesLine->attachAxis(angleAxis);
43     //[1]!
44     radiusAxis->setRange(0, 360);
45     angleAxis->setRange(0, 360);
46  // 多圈
47     for (double i=0; i<=360*2; i+=0.5)
48     {
49         auto angle = i;
50         while (angle > 360)
51         {
52             angle = angle - 360;
53         }
54         seriesLine->append(angle, i/2.0);
55     }
56     seriesLine->setPen(QPen(QColor(153, 232, 83), 2));
57  
58  
59  
60     //[1]
61     QChartView *polarChartView = new QChartView();
62     polarChartView->setChart(polarChart);
63  
64     MainWindow w;
65     w.SetWidget(polarChartView);
66     w.show();
67     //[1]!
68     return a.exec();
69 }

再畫幾個其他型別的線:

四葉曲線1:p=半徑*fabs(sin(2*角度)):

1 int radius = 200;
2 for (double angle=0; angle<=360; angle+=1)
3 {
4     seriesLine->append(angle, qFabs(radius*qSin(2.0*M_PI*angle/180.0)));
5 }

四葉曲線2:p=半徑*fabs(cos(2*角度)):

1 int radius = 200;
2 for (double angle=0; angle<=360; angle+=1)
3 {
4     seriesLine->append(angle, qFabs(radius*qCos(2.0*M_PI*angle/180.0)));
5 }

六葉花瓣:

1 //六葉花瓣  p=半徑*fabs(cos(3*角度))
2 int radius = 200;
3 for (double angle=0; angle<=360; angle+=1)
4 {
5     seriesLine->append(angle, qFabs(radius*qCos(3.0*M_PI*angle/180.0)));
6 }

三葉花瓣:

1 //三葉花瓣  p=半徑*cos(3*角度)
2 int radius = 200;
3 for (double angle=0; angle<=360; angle+=1)
4 {
5     seriesLine->append(angle, radius*qCos(3.0*M_PI*angle/180.0));
6 }

玫瑰曲線有規律,

r = sin ( k θ ) 或 r = cos ( k θ )

當 k 是奇數時,玫瑰曲線有 k 個花瓣;當 k 是偶數時,玫瑰曲線有 2k 個花瓣。

心形曲線:

1 //心形曲線 p=半徑*(1-cos(角度))
2 int radius = 100;
3 for (double angle=0; angle<=360; angle+=1)
4 {
5     seriesLine->append(angle, radius*(1-qCos(M_PI*angle/180.0)));
6 }

這心形下方還挺飽滿,跟撲克上的紅心差很大

相關文章