QCustomPlot之盒须图(十六)

盒须图的组成如下图所示:

盒须图

在QCustomPlot中创建一个盒须图所需的类:QCPStatisticalBox,为盒须图添加数据主要使用的是addData接口,接口定义如下:
void addData(double key, double minimum, double lowerQuartile, double median, double upperQuartile, double maximum, const QVector<double> &outliers=QVector<double>());

参数 含义
key 在key轴的位置
minimum 下边缘
lowerQuartile 下四分位数
median 中位数,同时也是在value轴的位置
upperQuartile 上四分位数
maximum 上边缘
outliers 异常值

盒须图风格

函数 含义
setWidth 设置盒子宽度,宽度大小是按key轴的坐标确定的
setWhiskerWidth 设置边缘线宽度,宽度大小是按key轴的坐标确定的
setWhiskerPen 设置边缘线到四分数的线段的画笔风格
setWhiskerBarPen 设置边缘线画笔风格
setWhiskerAntialiased 盒须(下边缘到四分位数的线以及下边缘线)是否是反锯齿
setMedianPen 中位数画笔风格
setOutlierStyle 异常值风格

盒须图的使用

void MainWindow::setupStatisticalDemo(QCustomPlot *customPlot)
{
    QCPStatisticalBox *statistical = new QCPStatisticalBox(customPlot->xAxis, customPlot->yAxis);
    QBrush boxBrush(QColor(60, 60, 255, 100));
    boxBrush.setStyle(Qt::Dense6Pattern); // 使它看起来很老派
    statistical->setBrush(boxBrush);

    statistical->addData(1, 1.1, 1.9, 2.25, 2.7, 4.2);
    statistical->addData(2, 0.8, 1.6, 2.2, 3.2, 4.9, QVector<double>() << 0.7 << 0.34 << 0.45 << 6.2 << 5.84); // 添加一些异常值
    statistical->addData(3, 0.2, 0.7, 1.1, 1.6, 2.9);

    // 设置轴风格
    customPlot->xAxis->setSubTicks(false);
    customPlot->xAxis->setTickLength(0, 4);
    customPlot->xAxis->setTickLabelRotation(20);
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    textTicker->addTick(1, "Sample 1");
    textTicker->addTick(2, "Sample 2");
    textTicker->addTick(3, "Control Group");
    customPlot->xAxis->setTicker(textTicker);

    customPlot->yAxis->setLabel("O₂ Absorption [mg]");
    customPlot->rescaleAxes();
    customPlot->xAxis->scaleRange(1.7, customPlot->xAxis->range().center());
    customPlot->yAxis->setRange(0, 7);
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}
效果图
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容