QChart Class

Main chart API for Qt Charts. 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, ..., ChartThemeQt }
enum ChartType { ChartTypeUndefined, ChartTypeCartesian, ChartTypePolar }

Properties

Public Functions

QChart(QGraphicsItem *parent = Q_NULLPTR, Qt::WindowFlags wFlags = Qt::WindowFlags())
~QChart()
void addAxis(QAbstractAxis *axis, Qt::Alignment alignment)
void addSeries(QAbstractSeries *series)
int animationDuration() const
QEasingCurve animationEasingCurve() const
AnimationOptions animationOptions() const
QList<QAbstractAxis *> axes(Qt::Orientations orientation = Qt::Horizontal | Qt::Vertical, QAbstractSeries *series = Q_NULLPTR) const
QAbstractAxis *axisX(QAbstractSeries *series = Q_NULLPTR) const
QAbstractAxis *axisY(QAbstractSeries *series = Q_NULLPTR) const
QBrush backgroundBrush() const
QPen backgroundPen() const
qreal backgroundRoundness() const
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 = Q_NULLPTR)
QPointF mapToValue(const QPointF &position, QAbstractSeries *series = Q_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(AnimationOptions options)
void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = Q_NULLPTR)
void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = Q_NULLPTR)
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 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)

Additional Inherited Members

Detailed Description

Main chart API for Qt Charts.

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. If you simply want to show a chart in a layout, you can use the convenience class QChartView instead of QChart.

See also QChartView and QPolarChart.

Member Type Documentation

enum QChart::AnimationOption
flags QChart::AnimationOptions

For enabling/disabling animations. Defaults to NoAnimation.

ConstantValue
QChart::NoAnimation0x0
QChart::GridAxisAnimations0x1
QChart::SeriesAnimations0x2
QChart::AllAnimations0x3

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.

ConstantValueDescription
QChart::ChartThemeLight0The default theme
QChart::ChartThemeBlueCerulean1 
QChart::ChartThemeDark2 
QChart::ChartThemeBrownSand3 
QChart::ChartThemeBlueNcs4 
QChart::ChartThemeHighContrast5 
QChart::ChartThemeBlueIcy6 
QChart::ChartThemeQt7 

enum QChart::ChartType

This enum describes the chart type.

ConstantValue
QChart::ChartTypeUndefined0
QChart::ChartTypeCartesian1
QChart::ChartTypePolar2

Property Documentation

animationDuration : int

The duration of the animation for the chart.

Access functions:

int animationDuration() const
void setAnimationDuration(int msecs)

animationEasingCurve : QEasingCurve

The easing curve of the animation for the chart.

Access functions:

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

animationOptions : QChart::AnimationOptions

The animation options for the chart. Animations are enabled/disabled based on this setting.

Access functions:

AnimationOptions animationOptions() const
void setAnimationOptions(AnimationOptions options)

backgroundRoundness : qreal

The diameter of the rounding circle at the corners of the chart background.

Access functions:

qreal backgroundRoundness() const
void setBackgroundRoundness(qreal diameter)

backgroundVisible : bool

Specifies whether the chart background is visible or not.

Access functions:

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

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

chartType : const QChart::ChartType

Chart type indicates if the chart is a cartesian chart or a polar chart. This property is set internally and it is read only.

Access functions:

ChartType chartType() const

See also QPolarChart.

dropShadowEnabled : bool

If set to true, the background drop shadow effect is enabled. If set to false, it is disabled. Note that the drop shadow effect depends on theme, which means the setting may be changed if you switch to another theme.

Access functions:

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

locale : QLocale

Sets the locale used to format various chart labels when localizeNumbers is true. This also determines the locale used to format QDateTimeAxis labels regardless of localizeNumbers property. Defaults to application default locale at the time the chart is constructed.

This property was introduced in QtCharts 2.0.

Access functions:

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

See also localizeNumbers.

localizeNumbers : bool

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 doesn't affect QDateTimeAxis labels, which always use the QLocale set with the locale property.

This property was introduced in QtCharts 2.0.

Access functions:

bool localizeNumbers() const
void setLocalizeNumbers(bool localize)

See also locale.

margins : QMargins

Margins between the plot area (axes) and the edge of the chart widget.

Access functions:

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

plotArea : const QRectF

Holds the rectangle within which the drawing of the chart is done. It does not include the area defined by margins.

Access functions:

QRectF plotArea() const

Notifier signal:

void plotAreaChanged(const QRectF &plotArea)

plotAreaBackgroundVisible : bool

Specifies whether the chart plot area background is visible or not.

Note: By default the plot area background is not visible 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

Theme is a built-in collection of UI style related settings applied for all visual elements of a chart, like colors, pens, brushes, and fonts of series, axes, title, and legend. Chart themes example shows an example with a few different themes.

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

Access functions:

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

title : QString

Title is the name (label) of a chart. It is shown as a headline on top of the chart. Chart title supports html formatting.

Access functions:

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

Member Function Documentation

QChart::QChart(QGraphicsItem *parent = Q_NULLPTR, Qt::WindowFlags wFlags = Qt::WindowFlags())

Constructs a chart object which is a child of a parent. Parameter wFlags is passed to the QGraphicsWidget constructor.

QChart::~QChart()

Destroys the chart object and its children, like series and axis objects added to it.

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

Adds the axis to the chart with alignment. The chart takes the ownership of the axis.

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

void QChart::addSeries(QAbstractSeries *series)

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

Note: A newly added series is attached to no axes by default, including any axes that were 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 = Q_NULLPTR) const

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

See also addAxis() and createDefaultAxes().

QAbstractAxis *QChart::axisX(QAbstractSeries *series = Q_NULLPTR) const

Returns a pointer to the horizontal axis attached to the specified series. If no series is specified, the first horizontal axis added to the chart is returned.

See also setAxisX(), addAxis(), and QAbstractSeries::attachAxis().

QAbstractAxis *QChart::axisY(QAbstractSeries *series = Q_NULLPTR) const

Returns a pointer to the vertical axis attached to the specified series. If no series is specified, the first vertical axis added to the chart is returned.

See also setAxisY(), addAxis(), and QAbstractSeries::attachAxis().

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.

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 axes() function call. QPieSeries does not create any axes.

See also axisX(), axisY(), axes(), setAxisX(), setAxisY(), 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 = Q_NULLPTR)

Returns the position on the chart widget that corresponds to the value in the series domain.

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

Returns the value in the series domain that corresponds to the position relative to chart widget.

QBrush QChart::plotAreaBackgroundBrush() const

Returns the brush for the background of the plot area of the chart.

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

QPen QChart::plotAreaBackgroundPen() const

Returns the pen for 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 from the chart. The chart releases its ownership of the specified axis object.

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

void QChart::removeSeries(QAbstractSeries *series)

Removes the series from the chart. The chart releases its 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 defined in the dx and dy.

For polar charts, dx indicates the angle along 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::setAxisX(QAbstractAxis *axis, QAbstractSeries *series = Q_NULLPTR)

Adds the axis to the chart and attaches it to the series as a bottom-aligned horizontal axis. The chart takes ownership of both the axis and the series. Any horizontal axes previously attached to the series are deleted.

See also axisX(), axisY(), setAxisY(), createDefaultAxes(), and QAbstractSeries::attachAxis().

void QChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series = Q_NULLPTR)

Adds the axis to the chart and attaches it to the series as a left-aligned vertical axis. The chart takes ownership of both the axis and the series. Any vertical axes previously attached to the series are deleted.

See also axisX(), axisY(), setAxisX(), createDefaultAxes(), and QAbstractSeries::attachAxis().

void QChart::setBackgroundBrush(const QBrush &brush)

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

See also backgroundBrush().

void QChart::setBackgroundPen(const QPen &pen)

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

See also backgroundPen().

void QChart::setPlotAreaBackgroundBrush(const QBrush &brush)

Sets the brush for the background of the plot area of the chart.

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

void QChart::setPlotAreaBackgroundPen(const QPen &pen)

Sets the pen for the background of the plot area of the chart.

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

void QChart::setTitleBrush(const QBrush &brush)

Sets the brush used for drawing the title text.

See also titleBrush().

void QChart::setTitleFont(const QFont &font)

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

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 in the view by a custom factor.

A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.

void QChart::zoomIn()

Zooms in the view by a factor of two.

void QChart::zoomIn(const QRectF &rect)

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

Note: This is not supported for polar charts.

void QChart::zoomOut()

Zooms out the view by a factor of two.

void QChart::zoomReset()

Resets the series domains to what they were before any zoom method was called. Note that this will also reset any scrolls and explicit axis range settings done between the first zoom operation and calling this method. If no zoom operation has been done, this method does nothing.

© 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.