QChart Class

The QChart class manages the graphical representation of the chart's series, legends, and axes. More...

Header: #include <QChart>
Inherits: QGraphicsWidget
Inherited By:

QPolarChart

Public Types

enum AnimationOption { NoAnimation, GridAxisAnimations, SeriesAnimations, AllAnimations }
flags AnimationOptions
enum ChartTheme { ChartThemeLight, ChartThemeBlueCerulean, ChartThemeDark, ChartThemeBrownSand, ChartThemeBlueNcs, …, ChartThemeQt }
enum ChartType { ChartTypeUndefined, ChartTypeCartesian, ChartTypePolar }

Properties

Public Functions

QChart(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags())
virtual ~QChart()
void addAxis(QAbstractAxis *axis, Qt::Alignment alignment)
void addSeries(QAbstractSeries *series)
int animationDuration() const
QEasingCurve animationEasingCurve() const
QChart::AnimationOptions animationOptions() const
QList<QAbstractAxis *> axes(Qt::Orientations orientation = Qt::Horizontal|Qt::Vertical, QAbstractSeries *series = nullptr) const
QBrush backgroundBrush() const
QPen backgroundPen() const
qreal backgroundRoundness() const
QChart::ChartType chartType() const
void createDefaultAxes()
bool isBackgroundVisible() const
bool isDropShadowEnabled() const
bool isPlotAreaBackgroundVisible() const
bool isZoomed()
QLegend *legend() const
QLocale locale() const
bool localizeNumbers() const
QPointF mapToPosition(const QPointF &value, QAbstractSeries *series = nullptr)
QPointF mapToValue(const QPointF &position, QAbstractSeries *series = nullptr)
QMargins margins() const
QRectF plotArea() const
QBrush plotAreaBackgroundBrush() const
QPen plotAreaBackgroundPen() const
void removeAllSeries()
void removeAxis(QAbstractAxis *axis)
void removeSeries(QAbstractSeries *series)
void scroll(qreal dx, qreal dy)
QList<QAbstractSeries *> series() const
void setAnimationDuration(int msecs)
void setAnimationEasingCurve(const QEasingCurve &curve)
void setAnimationOptions(QChart::AnimationOptions options)
void setBackgroundBrush(const QBrush &brush)
void setBackgroundPen(const QPen &pen)
void setBackgroundRoundness(qreal diameter)
void setBackgroundVisible(bool visible = true)
void setDropShadowEnabled(bool enabled = true)
void setLocale(const QLocale &locale)
void setLocalizeNumbers(bool localize)
void setMargins(const QMargins &margins)
void setPlotArea(const QRectF &rect)
void setPlotAreaBackgroundBrush(const QBrush &brush)
void setPlotAreaBackgroundPen(const QPen &pen)
void setPlotAreaBackgroundVisible(bool visible = true)
void setTheme(QChart::ChartTheme theme)
void setTitle(const QString &title)
void setTitleBrush(const QBrush &brush)
void setTitleFont(const QFont &font)
QChart::ChartTheme theme() const
QString title() const
QBrush titleBrush() const
QFont titleFont() const
void zoom(qreal factor)
void zoomIn()
void zoomIn(const QRectF &rect)
void zoomOut()
void zoomReset()

Signals

void plotAreaChanged(const QRectF &plotArea)

Detailed Description

QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical representation of different types of series and other chart related objects like legend and axes. To simply show a chart in a layout, the convenience class QChartView can be used instead of QChart. In addition, line, spline, area, and scatter series can be presented as polar charts by using the QPolarChart class.

See also QChartView and QPolarChart.

Member Type Documentation

enum QChart::AnimationOption
flags QChart::AnimationOptions

This enum describes the animations enabled in the chart.

ConstantValueDescription
QChart::NoAnimation0x0Animation is disabled in the chart. This is the default value.
QChart::GridAxisAnimations0x1Grid axis animation is enabled in the chart.
QChart::SeriesAnimations0x2Series animation is enabled in the chart.
QChart::AllAnimations0x3All animation types are enabled in the chart.

The AnimationOptions type is a typedef for QFlags<AnimationOption>. It stores an OR combination of AnimationOption values.

enum QChart::ChartTheme

This enum describes the theme used by the chart.

A theme is a built-in collection of UI style related settings applied to all the visual elements of a chart, such as colors, pens, brushes, and fonts of series, as well as axes, title, and legend. The Charts with Widgets Gallery illustrates how to use themes.

Note: Changing the theme will overwrite all customizations previously applied to the series.

ConstantValueDescription
QChart::ChartThemeLight0The light theme, which is the default theme.
QChart::ChartThemeBlueCerulean1The cerulean blue theme.
QChart::ChartThemeDark2The dark theme.
QChart::ChartThemeBrownSand3The sand brown theme.
QChart::ChartThemeBlueNcs4The natural color system (NCS) blue theme.
QChart::ChartThemeHighContrast5The high contrast theme.
QChart::ChartThemeBlueIcy6The icy blue theme.
QChart::ChartThemeQt7The Qt theme.

enum QChart::ChartType

This enum describes the chart type.

ConstantValueDescription
QChart::ChartTypeUndefined0The chart type is not defined.
QChart::ChartTypeCartesian1A cartesian chart.
QChart::ChartTypePolar2A polar chart.

Property Documentation

animationDuration : int

This property holds the duration of the animation for the chart.

Access functions:

int animationDuration() const
void setAnimationDuration(int msecs)

animationEasingCurve : QEasingCurve

This property holds the easing curve of the animation for the chart.

Access functions:

QEasingCurve animationEasingCurve() const
void setAnimationEasingCurve(const QEasingCurve &curve)

animationOptions : QChart::AnimationOptions

This property holds the animation options for the chart.

Animations are enabled or disabled based on this setting.

Access functions:

QChart::AnimationOptions animationOptions() const
void setAnimationOptions(QChart::AnimationOptions options)

backgroundRoundness : qreal

This property holds the diameter of the rounding circle at the corners of the chart background.

Access functions:

qreal backgroundRoundness() const
void setBackgroundRoundness(qreal diameter)

backgroundVisible : bool

This property holds whether the chart background is visible.

Access functions:

bool isBackgroundVisible() const
void setBackgroundVisible(bool visible = true)

See also setBackgroundBrush(), setBackgroundPen(), and plotAreaBackgroundVisible.

[read-only] chartType : const QChart::ChartType

This property holds whether the chart is a cartesian chart or a polar chart.

This property is set internally and it is read only.

Access functions:

QChart::ChartType chartType() const

See also QPolarChart.

dropShadowEnabled : bool

This property holds whether the background drop shadow effect is enabled.

If set to true, the background drop shadow effect is enabled. If set to false, it is disabled.

Note: The drop shadow effect depends on the theme, and therefore the setting may change if the theme is changed.

Access functions:

bool isDropShadowEnabled() const
void setDropShadowEnabled(bool enabled = true)

locale : QLocale

This property holds the locale used to format various chart labels.

Labels are localized only when localizeNumbers is true, except for QDateTimeAxis labels, which always use the QLocale set with this property.

Defaults to the application default locale at the time when the chart is constructed.

Access functions:

QLocale locale() const
void setLocale(const QLocale &locale)

See also localizeNumbers.

localizeNumbers : bool

This property holds whether numbers are localized.

When true, all generated numbers appearing in various series and axis labels will be localized using the QLocale set with the locale property. When false, the C locale is always used. Defaults to false.

Note: This property does not affect QDateTimeAxis labels, which always use the QLocale set with the locale property.

Access functions:

bool localizeNumbers() const
void setLocalizeNumbers(bool localize)

See also locale.

margins : QMargins

This property holds the minimum margins allowed between the edge of the chart rectangle and the plot area.

The margins are used for drawing the title, axes, and legend.

Access functions:

QMargins margins() const
void setMargins(const QMargins &margins)

plotArea : QRectF

This property holds the rectangle within which the chart is drawn.

The plot area does not include the area defined by margins. By default this will resize if inside a QChartView. If an explicit size is set for the plot area then it will respect this, to revert back to the default behavior, then calling setPlotArea(QRectF()); will achieve this.

Access functions:

QRectF plotArea() const
void setPlotArea(const QRectF &rect)

Notifier signal:

void plotAreaChanged(const QRectF &plotArea)

plotAreaBackgroundVisible : bool

This property holds whether the chart plot area background is visible.

Note: By default, the plot area background is invisible and the plot area uses the general chart background.

Access functions:

bool isPlotAreaBackgroundVisible() const
void setPlotAreaBackgroundVisible(bool visible = true)

See also setPlotAreaBackgroundBrush(), setPlotAreaBackgroundPen(), and backgroundVisible.

theme : QChart::ChartTheme

This property holds the theme used for the chart.

Access functions:

QChart::ChartTheme theme() const
void setTheme(QChart::ChartTheme theme)

title : QString

This property holds the title of the chart.

The title is shown as a headline on top of the chart. Chart titles support HTML formatting.

Access functions:

QString title() const
void setTitle(const QString &title)

Member Function Documentation

[explicit] QChart::QChart(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags())

Constructs a chart object that is a child of parent. The properties specified by wFlags are passed to the QGraphicsWidget constructor.

[virtual noexcept] QChart::~QChart()

Deletes the chart object and its children, such as the series and axis objects added to it.

void QChart::addAxis(QAbstractAxis *axis, Qt::Alignment alignment)

Adds the axis axis to the chart aligned as specified by alignment. The chart takes the ownership of the axis.

See also removeAxis(), createDefaultAxes(), and QAbstractSeries::attachAxis().

void QChart::addSeries(QAbstractSeries *series)

Adds the series series to the chart and takes ownership of it.

Note: A newly added series is not attached to any axes by default, not even those that might have been created for the chart using createDefaultAxes() before the series was added to the chart. If no axes are attached to the newly added series before the chart is shown, the series will get drawn as if it had axes with ranges that exactly fit the series to the plot area of the chart. This can be confusing if the same chart also displays other series that have properly attached axes, so always make sure you either call createDefaultAxes() after a series has been added or explicitly attach axes for the series.

See also removeSeries(), removeAllSeries(), createDefaultAxes(), and QAbstractSeries::attachAxis().

QList<QAbstractAxis *> QChart::axes(Qt::Orientations orientation = Qt::Horizontal|Qt::Vertical, QAbstractSeries *series = nullptr) const

Returns the axes attached to the series series with the orientation specified by orientation. If no series is specified, all axes added to the chart with the specified orientation are returned.

See also addAxis() and createDefaultAxes().

QBrush QChart::backgroundBrush() const

Gets the brush that is used for painting the background of the chart area.

See also setBackgroundBrush().

QPen QChart::backgroundPen() const

Gets the pen that is used for painting the background of the chart area.

See also setBackgroundPen().

void QChart::createDefaultAxes()

Creates axes for the chart based on the series that have already been added to the chart. Any axes previously added to the chart will be deleted.

Note: This function has to be called after all series have been added to the chart. The axes created by this function will NOT get automatically attached to any series added to the chart after this function has been called. A series with no axes attached will by default scale to utilize the entire plot area of the chart, which can be confusing if there are other series with properly attached axes also present.

Series typeHorizontal axis (X)Vertical axis (Y)
QXYSeriesQValueAxisQValueAxis
QBarSeriesQBarCategoryAxisQValueAxis
QPieSeriesNoneNone

If there are several QXYSeries derived series added to the chart and no series of other types have been added, then only one pair of axes is created. If there are several series of different types added to the chart, then each series gets its own axes pair.

The axes specific to the series can be later obtained from the chart by providing the series as the parameter for the axes() function call. QPieSeries does not create any axes.

See also axes() and QAbstractSeries::attachAxis().

bool QChart::isZoomed()

Returns true if any series has a zoomed domain.

QLegend *QChart::legend() const

Returns the legend object of the chart. Ownership stays with the chart.

QPointF QChart::mapToPosition(const QPointF &value, QAbstractSeries *series = nullptr)

Returns the position on the chart that corresponds to the value value in the series specified by series.

QPointF QChart::mapToValue(const QPointF &position, QAbstractSeries *series = nullptr)

Returns the value in the series specified by series at the position specified by position in a chart.

QBrush QChart::plotAreaBackgroundBrush() const

Returns the brush used to fill the background of the plot area of the chart.

See also plotArea(), plotAreaBackgroundVisible, plotAreaBackgroundPen(), and setPlotAreaBackgroundBrush().

QPen QChart::plotAreaBackgroundPen() const

Returns the pen used to draw the background of the plot area of the chart.

See also plotArea(), plotAreaBackgroundVisible, plotAreaBackgroundBrush(), and setPlotAreaBackgroundPen().

void QChart::removeAllSeries()

Removes and deletes all series objects that have been added to the chart.

See also addSeries() and removeSeries().

void QChart::removeAxis(QAbstractAxis *axis)

Removes the axis axis from the chart. The chart releases the ownership of the specified axis object.

See also addAxis(), createDefaultAxes(), and QAbstractSeries::detachAxis().

void QChart::removeSeries(QAbstractSeries *series)

Removes the series series from the chart. The chart releases the ownership of the specified series object.

See also addSeries() and removeAllSeries().

void QChart::scroll(qreal dx, qreal dy)

Scrolls the visible area of the chart by the distance specified by dx and dy.

For polar charts, dx indicates the angle along the angular axis instead of distance.

QList<QAbstractSeries *> QChart::series() const

Returns all series that are added to the chart.

See also addSeries(), removeSeries(), and removeAllSeries().

void QChart::setBackgroundBrush(const QBrush &brush)

Sets the brush that is used for painting the background of the chart area to brush.

See also backgroundBrush().

void QChart::setBackgroundPen(const QPen &pen)

Sets the pen that is used for painting the background of the chart area to pen.

See also backgroundPen().

void QChart::setPlotAreaBackgroundBrush(const QBrush &brush)

Sets the brush used to fill the background of the plot area of the chart to brush.

See also plotArea(), plotAreaBackgroundVisible, setPlotAreaBackgroundPen(), and plotAreaBackgroundBrush().

void QChart::setPlotAreaBackgroundPen(const QPen &pen)

Sets the pen used to draw the background of the plot area of the chart to pen.

See also plotArea(), plotAreaBackgroundVisible, setPlotAreaBackgroundBrush(), and plotAreaBackgroundPen().

void QChart::setTitleBrush(const QBrush &brush)

Sets the brush used for drawing the title text to brush.

See also titleBrush().

void QChart::setTitleFont(const QFont &font)

Sets the font that is used for drawing the chart title to font.

See also titleFont().

QBrush QChart::titleBrush() const

Returns the brush used for drawing the title text.

See also setTitleBrush().

QFont QChart::titleFont() const

Gets the font that is used for drawing the chart title.

See also setTitleFont().

void QChart::zoom(qreal factor)

Zooms into the view by the custom factor factor.

A factor over 1.0 zooms into the view and a factor between 0.0 and 1.0 zooms out of it.

void QChart::zoomIn()

Zooms into the view by a factor of two.

void QChart::zoomIn(const QRectF &rect)

Zooms into the view to a maximum level at which the rectangle rect is still fully visible.

Note: Applying a zoom may modify properties of attached axes, for instance QAbstractAxis::min and QAbstractAxis::max.

Note: This is not supported for polar charts.

void QChart::zoomOut()

Zooms out of the view by a factor of two.

Note: This will do nothing if the result would contain an invalid logarithmic axis range.

void QChart::zoomReset()

Resets the series domains to what they were before any zoom method was called.

Note: This will also reset scrolling and explicit axis range settings specified between the first zoom operation and calling this method. If no zoom operation has been performed, this method does nothing.