在本页

CustomSeries QML Type

CustomSeries 类型允许显示定制的图表类型。更多

Import Statement: import QtGraphs
Since: Qt 6.11
In C++: QCustomSeries

属性

方法

详细说明

使用 CustomSeries 创建自定义图表。把它想象成一个散点图,让你可以访问每个元素的自定义数据。

CustomSeries 定义了一个委托,用于呈现添加到图表中的每个项目。每个项目都包括存储在QVariantMap 中的用户定义数据。项目在图表中的索引也会自动添加到数据图中。在 QML 上下文中,变量映射由 JavaScript 对象定义。该系列将此映射传递给根据委托创建的每个元素。委托决定如何使用数据。

要将数据映射到GraphsView 轴定义的呈现坐标,请使用mapXmapY 函数。

注: 目前,自定义系列中的单个元素之间不能共享信息。因此,您无法实现自定义线性系列。

下面的代码实现了所描述的自定义图形。在这种情况下,自定义数据包含两个值:上限值和下限值。然后在委托中定义这两个值,以影响元素的高度和 y 坐标。

import QtQuick
import QtGraphs

Window {
    width: 640
    height: 480
    visible: true

    GraphsView {
        anchors.fill: parent

        axisX: BarCategoryAxis {
            categories: ["ABC", "DEF"]
        }

        axisY: ValueAxis {
            max: 8
            tickInterval: 1
        }

        CustomSeries {
            id: custom

            delegate: Rectangle {
                property var data
                gradient: Gradient {
                    GradientStop { position: 0.0; color: "lightsteelblue" }
                    GradientStop { position: 1.0; color: "blue" }
                }
                border.width: 1
                x: custom.mapX(data.index + 0.5) - width * 0.5
                y: custom.mapY((data.upper + data.lower) * 0.5) - height * 0.5
                width: Math.abs(custom.mapX(1) - custom.mapX(0)) * 0.5
                height: Math.abs(custom.mapY(data.upper) - custom.mapY(data.lower))
            }

            CustomSeriesData {
                data: ({
                    upper: 5,
                    lower: 1
                })
            }

            CustomSeriesData {
                data: ({
                    upper: 6,
                    lower: 4
                })
            }
        }
    }
}

属性文档

delegate : Component

委托用于向 QML 插入与所提供的自定义数据相对应的元素。数据在CustomSeries 本身中定义,并通过 "data "属性提供给委托元素。

delegate: Rectangle {
        property var data
        Rectangle {
            color: "tan"
            border.color: "black"
            border.width: 4
            y: series.mapY(data.index + 1) - height / 2 + 1
            x: series.mapX(data.median) - width / 2
            height: 32
            width: Math.abs(series.mapX(data.upperQuartile) - series.mapX(data.lowerQuartile))
        }
    }
}

方法文档

void append()

在系列中添加一个空数据项。

void append(var data)

data 定义的数据项附加到系列中。

void clear()

清除所有数据项。

void insert(int index)

index 位置的数据系列中添加一个空数据项。

void insert(int index, var data)

data 定义的数据项附加到index 位置的序列中。

real mapX(real x)

返回x 轴空间坐标转换成的渲染空间坐标。

real mapY(real y)

返回y 轴空间坐标转换成的渲染空间坐标。

void remove(int index)

删除位置index 上的一个数据项。

© 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.