PowerMaster/dataPanel/dpBaseChart.h

121 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef DPBASECHART_H
#define DPBASECHART_H
#include "dpBaseWidget.h"
#include "util/Chart/qcustomplot.h"
class dpBaseChart : public dpBaseWidget
{
Q_OBJECT
public:
dpBaseChart(QWidget *parent = nullptr);
~dpBaseChart();
void setTimeRange(TimeUnit) {}
void setDateTime(const QDateTime&) {}
void viewHistoricalData(const QDateTime&) {}
void synchronizeConfigData(const configurationResults&) {}
protected:
struct ChartStyle
{
QColor bgColor; //背景颜色
QColor axisColor; //坐标轴颜色
QColor labelColor; //坐标轴标签颜色
QFont labelFont; //坐标轴标签字体
QColor tickColor; //刻度颜色
QColor tickLabelColor; //刻度label颜色
QFont tickLabelFont; //刻度label字体
QPen gridPen; //网格线画笔
};
struct AxisConfig //坐标轴配置信息
{
QString name; //坐标轴名称,例如:电压
QString unit; //坐标轴单位例如V
RealTimeDataType dataType;
};
struct Axis //坐标轴配置信息
{
AxisConfig _cfg;
QCPAxis* qAxis;
bool bIsDefaultAxis; //一个plot包含两个默认坐标轴axis和axis2
ChartStyle style;
Axis()
{
qAxis = nullptr;
bIsDefaultAxis = false;
}
~Axis()
{
//执行plot->axisRect()->removeAxis(axis)时axis会delete掉
/*if(qAxis && !bIsDefaultAxis) //默认坐标轴由所属plot管理
{
delete qAxis;
qAxis = nullptr;
}*/
}
void setQCPAxis(QCPAxis* axis, bool isDefualtAxis)
{
qAxis = axis;
bIsDefaultAxis = isDefualtAxis;
if(qAxis)
{
setAxisLabel();
qAxis->setSubTicks(false);
//颜色
qAxis->setBasePen(style.axisColor);
//label
qAxis->setLabelColor(style.labelColor);
qAxis->setLabelFont(style.labelFont);
//刻度颜色
qAxis->setTickPen(QPen(style.tickColor));
//刻度Label颜色
qAxis->setTickLabelColor(style.tickLabelColor);
qAxis->setTickLabelFont(style.tickLabelFont);
qAxis->setTickLabelColor(style.tickLabelColor);
}
}
void applyConfig(const AxisConfig& cfg)
{
_cfg.name = cfg.name;
_cfg.unit = cfg.unit;
_cfg.dataType = cfg.dataType;
setAxisLabel(); //更新轴的名称
}
void setStyle(const ChartStyle chartStyle)
{
style = chartStyle;
}
private:
void setAxisLabel()
{
if(qAxis)
{
QString labelText = "";
if(_cfg.unit.isEmpty())
labelText = _cfg.name;
else
labelText = QString("%1(%2)").arg(_cfg.name,_cfg.unit);
//因为QCPAxis左右不同位置时lable的方向不一致且没有提供设置方法在这里通过字符串反转的方式来矫正
// if(qAxis->axisType() == QCPAxis::atLeft)
// std::reverse(labelText.begin(), labelText.end());
qAxis->setLabel(labelText);
}
}
};
};
#endif