存储图表数据的模板类QCPDataContainer
一、QCPDataContainer 概述
QCPDataContainer 是 QCustomPlot 中用于存储图表数据的模板类,作为各种图表数据的基础容器,提供高效的数据管理和访问接口。
二、主要派生类
类名 | 关联图表类型 | 描述 |
---|---|---|
QCPGraphDataContainer | QCPGraph | 存储曲线图数据 |
QCPCurveDataContainer | QCPCurve | 存储参数曲线数据 |
QCPBarsDataContainer | QCPBars | 存储柱状图数据 |
QCPStatisticalBoxDataContainer | QCPStatisticalBox | 存储箱线图数据 |
三、核心属性
属性 | 类型 | 描述 |
---|---|---|
size | int | 数据点数量 |
isEmpty | bool | 是否为空容器 |
四、通用方法
1. 数据操作方法
方法 | 参数 | 返回值 | 描述 |
---|---|---|---|
add | const DataType &data | void | 添加单个数据点 |
add | const QVector<DataType> &data | void | 批量添加数据 |
set | const QVector<DataType> &data | void | 替换所有数据 |
remove | int index | void | 删除指定索引数据 |
removeBefore | double sortKey | void | 删除小于指定键值的数据 |
removeAfter | double sortKey | void | 删除大于指定键值的数据 |
clear | - | void | 清空所有数据 |
2. 数据访问方法
方法 | 参数 | 返回值 | 描述 |
---|---|---|---|
at | int index | const DataType& | 访问指定索引数据 |
operator[] | int index | const DataType& | 访问指定索引数据 |
begin | - | iterator | 返回开始迭代器 |
end | - | iterator | 返回结束迭代器 |
constBegin | - | const_iterator | 返回常量开始迭代器 |
constEnd | - | const_iterator | 返回常量结束迭代器 |
findBegin | double sortKey | iterator | 查找第一个≥key的数据 |
findEnd | double sortKey | iterator | 查找第一个>key的数据 |
3. 范围查询方法
方法 | 参数 | 返回值 | 描述 |
---|---|---|---|
keyRange | bool &foundRange | QCPRange | 获取键值范围 |
valueRange | bool &foundRange | QCPRange | 获取值范围 |
span | - | double | 获取键值跨度 |
五、QCPGraphDataContainer 专用方法
方法 | 描述 |
---|---|
add /set 重载 | 支持直接传入(key,value)对 |
valueRange 重载 | 支持指定键值范围查询 |
六、QCPCurveDataContainer 专用方法
方法 | 描述 |
---|---|
point | 通过索引获取QPointF |
indexToT | 索引转参数t值 |
七、基础使用示例
cpp
// 创建图形数据容器
QSharedPointer<QCPGraphDataContainer> data(new QCPGraphDataContainer);// 添加数据
data->add(QCPGraphData(1.0, 2.5));
data->add(QCPGraphData(2.0, 3.1));// 批量添加
QVector<QCPGraphData> points;
points << QCPGraphData(3.0, 4.2) << QCPGraphData(4.0, 5.8);
data->add(points);// 访问数据
double firstValue = data->at(0)->value;// 范围查询
bool found;
QCPRange keyRange = data->keyRange(found);// 关联到图形
customPlot->graph(0)->setData(data);
八、高级用法示例
1. 高效数据更新
cpp
// 获取可修改的引用
auto &dataMap = *customPlot->graph(0)->data().data();// 直接操作数据
dataMap.clear();
for (int i=0; i<1000; ++i) {dataMap.add(QCPGraphData(i, qSin(i/10.0)));
}// 通知更新
customPlot->graph(0)->data()->set(dataMap, false); // 不自动计算范围
customPlot->rescaleAxes();
2. 数据范围筛选
cpp
// 筛选x在[2.0, 5.0]范围内的数据
auto beginIt = customPlot->graph(0)->data()->findBegin(2.0);
auto endIt = customPlot->graph(0)->data()->findEnd(5.0);QVector<QCPGraphData> filteredData;
for (auto it=beginIt; it!=endIt; ++it) {filteredData.append(*it);
}
3. 性能优化技巧
cpp
// 预分配内存
data->reserve(10000); // 预分配10000个点的空间// 批量操作减少重绘
customPlot->setNotAntialiasedElements(QCP::aePlottables); // 临时关闭抗锯齿
// ...大数据操作...
customPlot->setAntialiasedElements(QCP::aePlottables); // 恢复
九、各派生类数据格式
1. QCPGraphDataContainer
cpp
struct QCPGraphData {double key; // x坐标double value; // y坐标
}
2. QCPCurveDataContainer
cpp
struct QCPCurveData {double t; // 参数double key; // x坐标double value; // y坐标
}
3. QCPBarsDataContainer
cpp
struct QCPBarsData {double key; // x坐标double value; // 柱高
}
QCPDataContainer 提供了高效灵活的数据管理能力,通过合理使用可以:
-
处理大规模数据集
-
实现动态数据更新
-
支持复杂数据操作
-
优化图表绘制性能