feat:直方图添加数据实时展示功能
This commit is contained in:
parent
94a23a2526
commit
4967df6628
|
|
@ -1,6 +1,103 @@
|
|||
#include "dpBarsChart.h"
|
||||
#include "dataManager.h"
|
||||
|
||||
CustomBars::CustomBars(QCPAxis* keyAxis, QCPAxis* valueAxis)
|
||||
: QCPBars(keyAxis, valueAxis),
|
||||
m_textAlignment(Qt::AlignCenter),
|
||||
m_spacing(5),
|
||||
m_font(QFont(QLatin1String("sans serif"), 12))
|
||||
{}
|
||||
|
||||
void CustomBars::setTextAlignment(Qt::Alignment alignment)
|
||||
{
|
||||
m_textAlignment = alignment;
|
||||
}
|
||||
|
||||
void CustomBars::setSpacing(double spacing)
|
||||
{
|
||||
m_spacing = spacing;
|
||||
}
|
||||
|
||||
void CustomBars::setFont(QFont font)
|
||||
{
|
||||
m_font = font;
|
||||
}
|
||||
|
||||
void CustomBars::draw(QCPPainter* painter)
|
||||
{
|
||||
///源码拷贝
|
||||
if (!mKeyAxis || !mValueAxis) { qDebug() << Q_FUNC_INFO << "invalid key or value axis"; return; }
|
||||
if (mDataContainer->isEmpty()) return;
|
||||
|
||||
QCPBarsDataContainer::const_iterator visibleBegin, visibleEnd;
|
||||
getVisibleDataBounds(visibleBegin, visibleEnd);
|
||||
|
||||
// loop over and draw segments of unselected/selected data:
|
||||
QList<QCPDataRange> selectedSegments, unselectedSegments, allSegments;
|
||||
getDataSegments(selectedSegments, unselectedSegments);
|
||||
allSegments << unselectedSegments << selectedSegments;
|
||||
for (int i=0; i<allSegments.size(); ++i)
|
||||
{
|
||||
bool isSelectedSegment = i >= unselectedSegments.size();
|
||||
QCPBarsDataContainer::const_iterator begin = visibleBegin;
|
||||
QCPBarsDataContainer::const_iterator end = visibleEnd;
|
||||
mDataContainer->limitIteratorsToDataRange(begin, end, allSegments.at(i));
|
||||
if (begin == end)
|
||||
continue;
|
||||
|
||||
for (QCPBarsDataContainer::const_iterator it=begin; it!=end; ++it)
|
||||
{
|
||||
// check data validity if flag set:
|
||||
#ifdef QCUSTOMPLOT_CHECK_DATA
|
||||
if (QCP::isInvalidData(it->key, it->value))
|
||||
qDebug() << Q_FUNC_INFO << "Data point at" << it->key << "of drawn range invalid." << "Plottable name:" << name();
|
||||
#endif
|
||||
// draw bar:
|
||||
if (isSelectedSegment && mSelectionDecorator)
|
||||
{
|
||||
mSelectionDecorator->applyBrush(painter);
|
||||
mSelectionDecorator->applyPen(painter);
|
||||
} else
|
||||
{
|
||||
painter->setBrush(mBrush);
|
||||
painter->setPen(mPen);
|
||||
}
|
||||
applyDefaultAntialiasingHint(painter);
|
||||
painter->drawPolygon(getBarRect(it->key, it->value));
|
||||
|
||||
///---自定义逻辑-start---
|
||||
//draw text
|
||||
QRectF barRect = getBarRect(it->key, it->value);
|
||||
painter->setFont(m_font);
|
||||
QString text = QString::number(it->value, 'g', 2); //获取当前value值,保留两位精度
|
||||
QRectF textRect = painter->fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip | m_textAlignment, text); //计算文字占用大小
|
||||
if(mKeyAxis.data()->orientation() == Qt::Horizontal) //水平轴为keyAxis
|
||||
{
|
||||
if(mKeyAxis.data()->axisType() == QCPAxis::atTop) //上轴,文字放到柱图下方
|
||||
textRect.moveTopLeft(barRect.bottomLeft() + QPointF(0, m_spacing));
|
||||
else //下轴,文字放到柱图上方
|
||||
textRect.moveBottomLeft(barRect.topLeft() - QPointF(0, m_spacing));
|
||||
textRect.setWidth(barRect.width());
|
||||
}
|
||||
else //垂直轴为keyAxis
|
||||
{
|
||||
if(mKeyAxis.data()->axisType() == QCPAxis::atLeft) //左轴,文字放到柱图右侧
|
||||
textRect.moveTopLeft(barRect.topRight() + QPointF(m_spacing, 0));
|
||||
else //右轴,文字放到柱图左侧
|
||||
textRect.moveTopRight(barRect.topLeft() - QPointF(m_spacing, 0));
|
||||
textRect.setWidth(barRect.height());
|
||||
}
|
||||
painter->drawText(textRect, Qt::TextDontClip | m_textAlignment, text);
|
||||
///---自定义逻辑-end---
|
||||
}
|
||||
}
|
||||
|
||||
///源码拷贝
|
||||
// draw other selection decoration that isn't just line/scatter pens and brushes:
|
||||
if (mSelectionDecorator)
|
||||
mSelectionDecorator->drawDecoration(painter, selection());
|
||||
}
|
||||
|
||||
dpBarsChart::dpBarsChart(QWidget* parent)
|
||||
:dpBaseChart(parent)
|
||||
{
|
||||
|
|
@ -139,7 +236,7 @@ void dpBarsChart::synchronizeConfigData(const configurationResults& cfg)
|
|||
bars.dataID = dataID;
|
||||
bars.dataType = dataType;
|
||||
bars.color = colorData.value<QColor>();
|
||||
QCPBars* qBars = new QCPBars(keyAxis, valueAxis);
|
||||
CustomBars* qBars = new CustomBars(keyAxis, valueAxis);
|
||||
qBars->setName(bars.name);
|
||||
qBars->setPen(bars.color); //边框颜色
|
||||
qBars->setBrush(bars.color); //填充颜色
|
||||
|
|
|
|||
|
|
@ -8,12 +8,34 @@
|
|||
** 柱状图展示面板,用来展示统计数据(如各类报警、事件的数量)
|
||||
** 采用QCustomPlot实现
|
||||
**
|
||||
** CustomBars
|
||||
** 自定义Bars,继承自QCPBars,可以展示实时数据等信息
|
||||
**
|
||||
******************************************************************************/
|
||||
|
||||
#include "dpBaseChart.h"
|
||||
|
||||
class QCustomPlot;
|
||||
class QCPBarsGroup;
|
||||
class CustomBars : public QCPBars
|
||||
{
|
||||
public:
|
||||
explicit CustomBars(QCPAxis* keyAxis, QCPAxis* valueAxis);
|
||||
|
||||
Qt::Alignment textAlignment() { return m_textAlignment; }
|
||||
double spacing() { return m_spacing; }
|
||||
QFont font() { return m_font; }
|
||||
|
||||
void setTextAlignment(Qt::Alignment);
|
||||
void setSpacing(double);
|
||||
void setFont(QFont);
|
||||
|
||||
protected:
|
||||
virtual void draw(QCPPainter* painter) override;
|
||||
|
||||
private:
|
||||
Qt::Alignment m_textAlignment; //文字对齐方式
|
||||
double m_spacing; //文字与柱状图之间的间距,单位为像素
|
||||
QFont m_font; //文字的字体
|
||||
};
|
||||
|
||||
class dpBarsChart : public dpBaseChart
|
||||
{
|
||||
|
|
@ -40,7 +62,7 @@ private:
|
|||
QString dataID;
|
||||
RealTimeDataType dataType;
|
||||
QVector<double> keys;
|
||||
QPointer<QCPBars> qBars;
|
||||
QPointer<CustomBars> qBars;
|
||||
|
||||
Bars()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -12,9 +12,6 @@
|
|||
|
||||
#include "dpBaseChart.h"
|
||||
|
||||
class QCustomPlot;
|
||||
class QCPRange;
|
||||
|
||||
class dpLineChart : public dpBaseChart
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
|
|||
Loading…
Reference in New Issue