QChart Class
QChart 类负责管理图表中数据系列、图例和坐标轴的图形显示。更多内容...
| Header: | #include <QChart> |
| Inherits: | QGraphicsWidget |
| Inherited By: | |
| Status: | Deprecated |
该类已弃用。我们强烈建议不要在新代码中使用它。
公共类型
| enum | AnimationOption { NoAnimation, GridAxisAnimations, SeriesAnimations, AllAnimations } |
| flags | AnimationOptions |
| enum | ChartTheme { ChartThemeLight, ChartThemeBlueCerulean, ChartThemeDark, ChartThemeBrownSand, ChartThemeBlueNcs, …, ChartThemeQt } |
| enum | ChartType { ChartTypeUndefined, ChartTypeCartesian, ChartTypePolar } |
属性
|
|
公共函数
| 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() |
信号
| void | plotAreaChanged(const QRectF &plotArea) |
详细说明
QChart 是一个QGraphicsWidget ,可显示在QGraphicsScene 中。它负责管理不同类型数据序列的图形表示,以及图例和坐标轴等其他与图表相关的对象。 若要在布局中简单地显示图表,可以使用便捷类QChartView 代替QChart。此外,通过使用QPolarChart 类,线型、样条、面积和散点数据系列均可呈现为极坐标图。
另请参阅 QChartView 和QPolarChart 。
成员类型文档
enum QChart::AnimationOption
flags QChart::AnimationOptions
此枚举描述了图表中启用的动画效果。
| 常量 | 值 | 描述 |
|---|---|---|
QChart::NoAnimation | 0x0 | 图表中禁用了动画。这是默认值。 |
QChart::GridAxisAnimations | 0x1 | 图表中启用了网格轴动画。 |
QChart::SeriesAnimations | 0x2 | 图表中已启用系列动画。 |
QChart::AllAnimations | 0x3 | 图表中启用了所有动画类型。 |
AnimationOptions 类型是QFlags<AnimationOption> 的 typedef。它存储 AnimationOption 值的按“或”运算组合。
enum QChart::ChartTheme
此枚举描述了图表所使用的主题。
主题是一组内置的 UI 样式相关设置,适用于图表的所有视觉元素,例如数据系列的颜色、笔、画笔和字体,以及坐标轴、标题和图例。带小部件的图表图库展示了如何使用主题。
注意:更改 主题将覆盖之前对数据系列所做的所有自定义设置。
| 常量 | 值 | 描述 |
|---|---|---|
QChart::ChartThemeLight | 0 | 浅色主题,即默认主题。 |
QChart::ChartThemeBlueCerulean | 1 | 天蓝色主题。 |
QChart::ChartThemeDark | 2 | 深色主题。 |
QChart::ChartThemeBrownSand | 3 | 沙棕色主题。 |
QChart::ChartThemeBlueNcs | 4 | 自然色彩系统(NCS)蓝色主题。 |
QChart::ChartThemeHighContrast | 5 | 高对比度主题。 |
QChart::ChartThemeBlueIcy | 6 | 冰蓝色主题。 |
QChart::ChartThemeQt | 7 | Qt主题。 |
enum QChart::ChartType
此枚举用于描述图表类型。
| 常量 | 值 | 描述 |
|---|---|---|
QChart::ChartTypeUndefined | 0 | 未定义图表类型。 |
QChart::ChartTypeCartesian | 1 | 笛卡尔图表。 |
QChart::ChartTypePolar | 2 | 极坐标图。 |
属性文档
animationDuration : int
该属性用于指定图表动画的持续时间。
访问函数:
| int | animationDuration() const |
| void | setAnimationDuration(int msecs) |
animationEasingCurve : QEasingCurve
该属性存储图表动画的缓动曲线。
访问函数:
| QEasingCurve | animationEasingCurve() const |
| void | setAnimationEasingCurve(const QEasingCurve &curve) |
animationOptions : QChart::AnimationOptions
此属性用于设置图表的动画选项。
动画功能将根据此设置启用或禁用。
访问函数:
| QChart::AnimationOptions | animationOptions() const |
| void | setAnimationOptions(QChart::AnimationOptions options) |
backgroundRoundness : qreal
该属性存储图表背景各角处的圆角半径。
访问函数:
| qreal | backgroundRoundness() const |
| void | setBackgroundRoundness(qreal diameter) |
backgroundVisible : bool
该属性控制图表背景是否可见。
访问函数:
| bool | isBackgroundVisible() const |
| void | setBackgroundVisible(bool visible = true) |
另请参阅 setBackgroundBrush()、setBackgroundPen(),以及plotAreaBackgroundVisible 。
[read-only] chartType : QChart::ChartType
该属性用于指定图表是笛卡尔坐标系图表还是极坐标系图表。
该属性由系统内部设置,且为只读。
访问函数:
| QChart::ChartType | chartType() const |
另请参阅 QPolarChart 。
dropShadowEnabled : bool
该属性用于控制背景投影效果是否启用。
如果设置为true ,则启用背景阴影效果;如果设置为false ,则禁用该效果。
注意:投影效果 取决于主题,因此如果更改主题,该设置可能会随之改变。
访问函数:
| bool | isDropShadowEnabled() const |
| void | setDropShadowEnabled(bool enabled = true) |
locale : QLocale
此属性存储用于格式化各种图表标签的区域设置。
只有当localizeNumbers 的值为true 时,标签才会进行本地化,但QDateTimeAxis标签除外,它们始终使用通过此属性设置的QLocale 。
默认值为构建图表时应用程序的默认区域设置。
访问函数:
| QLocale | locale() const |
| void | setLocale(const QLocale &locale) |
另请参阅 localizeNumbers 。
localizeNumbers : bool
无论数字是否经过本地化,此属性均有效。
当true 时,各种数据系列和坐标轴标签中出现的所有生成的数字都将使用通过locale 属性设置的QLocale 进行本地化。当false 时,始终使用C语言环境。默认值为false 。
注意:此 属性不影响 QDateTimeAxis 标签,这些标签始终使用通过 locale 属性设置的QLocale 。
访问函数:
| bool | localizeNumbers() const |
| void | setLocalizeNumbers(bool localize) |
另请参阅 locale 。
margins : QMargins
此属性指定图表矩形边缘与绘图区域之间允许的最小边距。
这些边距用于绘制标题、坐标轴和图例。
访问函数:
| QMargins | margins() const |
| void | setMargins(const QMargins &margins) |
plotArea : QRectF
该属性用于指定绘制图表的矩形范围。
绘图区域不包含由边距定义的区域。默认情况下,若位于 QChartView 内部,该区域会自动调整大小。若为绘图区域显式设置了尺寸,则会遵循该设置;若要恢复默认行为,可调用 `setPlotArea(QRectF()); ` 实现。
访问函数:
| QRectF | plotArea() const |
| void | setPlotArea(const QRectF &rect) |
通知器信号:
| void | plotAreaChanged(const QRectF &plotArea) |
plotAreaBackgroundVisible : bool
该属性控制图表绘图区域的背景是否可见。
注意:默认情况下 ,绘图区域背景不可见,且绘图区域使用图表的通用背景。
访问函数:
| bool | isPlotAreaBackgroundVisible() const |
| void | setPlotAreaBackgroundVisible(bool visible = true) |
另请参阅 setPlotAreaBackgroundBrush()、setPlotAreaBackgroundPen() 和backgroundVisible 。
theme : QChart::ChartTheme
此属性存储图表所使用的主题。
访问函数:
| QChart::ChartTheme | theme() const |
| void | setTheme(QChart::ChartTheme theme) |
title : QString
此属性用于存储图表的标题。
标题将作为图表顶部的标题显示。图表标题支持 HTML 格式。
Access 函数:
| QString | title() const |
| void | setTitle(const QString &title) |
成员函数文档
[explicit] QChart::QChart(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags())
创建一个作为parent 子节点的图表对象。由wFlags 指定的属性将传递给QGraphicsWidget 构造函数。
[virtual noexcept] QChart::~QChart()
删除图表对象及其子对象,例如添加到该图表中的系列和坐标轴对象。
void QChart::addAxis(QAbstractAxis *axis, Qt::Alignment alignment)
将坐标轴axis 添加到图表中,并对齐方式按alignment 中指定的方式进行设置。图表将拥有该坐标轴的所有权。
另请参阅 removeAxis()、createDefaultAxes() 和QAbstractSeries::attachAxis()。
void QChart::addSeries(QAbstractSeries *series)
将数据系列series 添加到图表中,并将其设为当前所有者。
注意: 默认情况下,新添加的系列 不会与任何坐标轴关联,即使是在该系列被添加到图表之前,已使用createDefaultAxes() 为该图表创建的坐标轴也不例外。 如果在显示图表之前,新添加的系列未关联任何坐标轴,则该系列将被绘制为:仿佛其坐标轴的范围与图表的绘图区域完全吻合。 如果同一张图表中还显示了其他已正确关联坐标轴的系列,这可能会造成混淆,因此请务必在添加系列后调用 `createDefaultAxes()`,或者为该系列显式关联坐标轴。
另请参阅 removeSeries()、removeAllSeries()、createDefaultAxes() 和QAbstractSeries::attachAxis()。
QList<QAbstractAxis *> QChart::axes(Qt::Orientations orientation = Qt::Horizontal|Qt::Vertical, QAbstractSeries *series = nullptr) const
返回与数据系列series 关联的坐标轴,其方向由orientation 指定。如果未指定数据系列,则返回所有以指定方向添加到图表中的坐标轴。
另请参阅 addAxis() 和createDefaultAxes()。
QBrush QChart::backgroundBrush() const
获取用于绘制图表区域背景的画笔。
另请参阅 setBackgroundBrush()。
QPen QChart::backgroundPen() const
获取用于绘制图表区域背景的画笔。
另请参阅 setBackgroundPen()。
void QChart::createDefaultAxes()
根据已添加到图表中的数据系列为图表创建坐标轴。此前添加到图表中的任何坐标轴都将被删除。
注意: 必须在所有数据系列均已添加到图表后,才可调用此 函数。调用此函数后,由该函数创建的坐标轴将不会自动关联到后续添加到图表中的任何数据系列。 未关联坐标轴的系列默认会按比例缩放以利用图表的整个绘图区域,如果图表中还存在其他已正确关联坐标轴的系列,这可能会造成混淆。
| 系列类型 | 水平轴(X) | 纵坐标(Y) |
|---|---|---|
| QXYSeries | QValueAxis | QValueAxis |
| QBarSeries | QBar分类轴 | QValueAxis |
| QPieSeries | 无 | 无 |
如果图表中添加了多个由 QXYSeries 派生的系列,且未添加其他类型的系列,则仅会创建一对坐标轴。如果图表中添加了多种类型的系列,则每个系列都会拥有自己的一对坐标轴。
可以通过将该系列作为axes()函数调用的参数,从图表中获取该系列专用的坐标轴。QPieSeries不会创建任何坐标轴。
另请参阅 axes() 和QAbstractSeries::attachAxis()。
bool QChart::isZoomed()
如果任何数列具有缩放定义域,则返回true 。
QLegend *QChart::legend() const
返回图表的图例对象。所有权仍归图表所有。
QPointF QChart::mapToPosition(const QPointF &value, QAbstractSeries *series = nullptr)
返回图表上与值value 相对应的位置,该值属于由series 指定的数据系列。
QPointF QChart::mapToValue(const QPointF &position, QAbstractSeries *series = nullptr)
返回图表中由series 指定的序列中,位于position 指定位置处的值。
QBrush QChart::plotAreaBackgroundBrush() const
返回用于填充图表绘图区域背景的画笔。
另请参阅 plotArea()、plotAreaBackgroundVisible 、plotAreaBackgroundPen() 以及setPlotAreaBackgroundBrush()。
QPen QChart::plotAreaBackgroundPen() const
返回用于绘制图表绘图区域背景的画笔。
另请参阅 plotArea()、plotAreaBackgroundVisible 、plotAreaBackgroundBrush() 以及setPlotAreaBackgroundPen()。
void QChart::removeAllSeries()
移除并删除已添加到图表中的所有系列对象。
另请参阅 addSeries() 和removeSeries()。
void QChart::removeAxis(QAbstractAxis *axis)
从图表中移除坐标轴axis 。图表将释放对指定axis 对象的所有权。
另请参阅 addAxis()、createDefaultAxes() 和QAbstractSeries::detachAxis()。
void QChart::removeSeries(QAbstractSeries *series)
从图表中移除系列series 。图表将释放对指定series 对象的所有权。
另请参阅 addSeries() 和removeAllSeries()。
void QChart::scroll(qreal dx, qreal dy)
根据dx 和dy 中指定的距离滚动图表的可见区域。
对于极坐标图表,dx 表示沿角度轴的夹角,而非距离。
QList<QAbstractSeries *> QChart::series() const
返回所有已添加到图表中的数据系列。
另请参阅 addSeries()、removeSeries() 和removeAllSeries()。
void QChart::setBackgroundBrush(const QBrush &brush)
将用于绘制图表区域背景的画笔设置为brush 。
另请参阅 backgroundBrush()。
void QChart::setBackgroundPen(const QPen &pen)
将用于绘制图表区域背景的画笔设置为pen 。
另请参阅 backgroundPen()。
void QChart::setPlotAreaBackgroundBrush(const QBrush &brush)
将用于填充图表绘图区域背景的画笔设置为brush 。
另请参阅 plotArea()、plotAreaBackgroundVisible 、setPlotAreaBackgroundPen() 以及plotAreaBackgroundBrush()。
void QChart::setPlotAreaBackgroundPen(const QPen &pen)
将用于绘制图表绘图区域背景的画笔设置为pen 。
另请参阅 plotArea()、plotAreaBackgroundVisible 、setPlotAreaBackgroundBrush() 以及plotAreaBackgroundPen()。
void QChart::setTitleBrush(const QBrush &brush)
将用于绘制标题文本的画笔设置为brush 。
另请参阅 titleBrush()。
void QChart::setTitleFont(const QFont &font)
将绘制图表标题时使用的字体设置为font 。
另请参阅 titleFont()。
QBrush QChart::titleBrush() const
返回用于绘制标题文本的画笔。
另请参阅 setTitleBrush()。
QFont QChart::titleFont() const
获取用于绘制图表标题的字体。
另请参阅 setTitleFont()。
void QChart::zoom(qreal factor)
按自定义缩放因子factor 将视图放大。
放大因子大于 1.0 时,视图会放大;放大因子在 0.0 到 1.0 之间时,视图会缩小。
void QChart::zoomIn()
将视图放大两倍。
void QChart::zoomIn(const QRectF &rect)
将视图放大至最大级别,此时矩形rect 仍能完全显示。
注意:应用 缩放操作可能会修改关联坐标轴的属性,例如 QAbstractAxis::min 和 QAbstractAxis::max。
注意: 极坐标图不支持此功能 。
void QChart::zoomOut()
将视图缩小至原来的二分之一。
注意: 如果操作结果会导致对数轴范围无效,则此操作 将不执行。
void QChart::zoomReset()
将系列域重置为调用任何缩放方法之前的状态。
注意:这 还将重置在首次缩放操作与调用此方法之间指定的滚动和显式轴范围设置。如果尚未执行任何缩放操作,则此方法不执行任何操作。
© 2026 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.