Polar Chart Example▲
It also shows how to implement scrolling and zooming of the polar chart as well as visually demonstrate how polar charts and cartesian charts relate to each other.
Running the Example▲
To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
Creating Polar Charts▲
Creating a polar chart is done with a QPolarChart instance instead of a QChart instance.
QPolarChart *
chart =
new
QPolarChart();
Axes are created similarly to cartesian charts, but when axes are added to the chart, you can use polar orientations instead of alignments.
QValueAxis *
angularAxis =
new
QValueAxis();
angularAxis-&
gt;setTickCount(9
); // First and last ticks are co-located on 0/360 angle.
angularAxis-&
gt;setLabelFormat("%.1f"
);
angularAxis-&
gt;setShadesVisible(true
);
angularAxis-&
gt;setShadesBrush(QBrush(QColor(249
, 249
, 255
)));
chart-&
gt;addAxis(angularAxis, QPolarChart::
PolarOrientationAngular);
QValueAxis *
radialAxis =
new
QValueAxis();
radialAxis-&
gt;setTickCount(9
);
radialAxis-&
gt;setLabelFormat("%d"
);
chart-&
gt;addAxis(radialAxis, QPolarChart::
PolarOrientationRadial);
Zooming and scrolling of a polar chart is logically nearly identical to zooming and scrolling of a cartesian chart. The main difference is that when scrolling along the X-axis (angular axis), the angle is used instead of the number of pixels. Another difference is that zooming to a rectangle cannot be done.
void
ChartView::
keyPressEvent(QKeyEvent *
event)
{
switch
(event-&
gt;key()) {
case
Qt::
Key_Plus:
chart()-&
gt;zoomIn();
break
;
case
Qt::
Key_Minus:
chart()-&
gt;zoomOut();
break
;
case
Qt::
Key_Left:
chart()-&
gt;scroll(-
1.0
, 0
);
break
;
case
Qt::
Key_Right:
chart()-&
gt;scroll(1.0
, 0
);
break
;
case
Qt::
Key_Up:
chart()-&
gt;scroll(0
, 1.0
);
break
;
case
Qt::
Key_Down:
chart()-&
gt;scroll(0
, -
1.0
);
break
;
case
Qt::
Key_Space:
switchChartType();
break
;
default
:
QGraphicsView::
keyPressEvent(event);
break
;
}
}
The same axes and series can be used in both cartesian and polar charts, though not simultaneously. To switch between chart types, you first need to remove the series and axes from the old chart, and then add them to the new chart. If you want to preserve the axis ranges, those need to be copied, too.
void
ChartView::
switchChartType()
{
QChart *
newChart;
QChart *
oldChart =
chart();
if
(oldChart-&
gt;chartType() ==
QChart::
ChartTypeCartesian)
newChart =
new
QPolarChart();
else
newChart =
new
QChart();
// Move series and axes from old chart to new one
const
QList&
lt;QAbstractSeries *&
gt; seriesList =
oldChart-&
gt;series();
const
QList&
lt;QAbstractAxis *&
gt; axisList =
oldChart-&
gt;axes();
QList&
lt;QPair&
lt;qreal, qreal&
gt; &
gt; axisRanges;
for
(QAbstractAxis *
axis : axisList) {
QValueAxis *
valueAxis =
static_cast
&
lt;QValueAxis *&
gt;(axis);
axisRanges.append(QPair&
lt;qreal, qreal&
gt;(valueAxis-&
gt;min(), valueAxis-&
gt;max()));
}
for
(QAbstractSeries *
series : seriesList)
oldChart-&
gt;removeSeries(series);
for
(QAbstractAxis *
axis : axisList) {
oldChart-&
gt;removeAxis(axis);
newChart-&
gt;addAxis(axis, axis-&
gt;alignment());
}
for
(QAbstractSeries *
series : seriesList) {
newChart-&
gt;addSeries(series);
for
(QAbstractAxis *
axis : axisList)
series-&
gt;attachAxis(axis);
}
int
count =
0
;
for
(QAbstractAxis *
axis : axisList) {
axis-&
gt;setRange(axisRanges[count].first, axisRanges[count].second);
count++
;
}
newChart-&
gt;setTitle(oldChart-&
gt;title());
setChart(newChart);
delete
oldChart;
}