#include "XYAxis.h"
#include <cmath>
#include <QtMath>
#include <QDebug>
XYAxis::XYAxis(QObject *parent)
: QObject(parent)
{
}
void XYAxis::init(AxisPosition position, double minLimit, double maxLimit,
double minRange, double minValue, double maxValue)
{
this->thePosition = position;
this->minLimit = minLimit;
this->maxLimit = maxLimit;
this->minRange = minRange;
this->minValue = minValue;
this->maxValue = maxValue;
}
XYAxis::AxisPosition XYAxis::getAxisPosition() const
{
return thePosition;
}
void XYAxis::setAxisPosition(AxisPosition position)
{
if (thePosition != position)
{
thePosition = position;
emit axisChanged();
}
}
XYAxis::TickMode XYAxis::getTickMode() const
{
return theMode;
}
void XYAxis::setTickMode(TickMode mode)
{
if (theMode != mode)
{
theMode = mode;
calcAxis();
}
}
QRect XYAxis::getRect() const
{
return theRect;
}
void XYAxis::setRect(const QRect &rect)
{
if (theRect != rect && rect.isValid())
{
theRect = rect;
calcAxis();
}
}
int XYAxis::getDecimalPrecision() const
{
return decimalPrecision;
}
void XYAxis::setDecimalPrecision(int precison)
{
if (decimalPrecision != precison)
{
decimalPrecision = precison;
emit axisChanged();
}
}
double XYAxis::getFixedValueSpace() const
{
return fixedValueSpace;
}
void XYAxis::setFixedValueSpace(double value)
{
fixedValueSpace = value;
calcAxis();
}
int XYAxis::getRefPixelSpace() const
{
return refPixelSpace;
}
void XYAxis::setRefPixelSpace(int pixel)
{
refPixelSpace = pixel;
calcAxis();
}
QVector<int> XYAxis::getTickPos() const
{
return tickPos;
}
QVector<QString> XYAxis::getTickLabel() const
{
return tickLabel;
}
double XYAxis::getMinLimit() const
{
return minLimit;
}
double XYAxis::getMaxLimit() const
{
return maxLimit;
}
double XYAxis::getMinRange() const
{
return minRange;
}
double XYAxis::getMinValue() const
{
return minValue;
}
double XYAxis::getMaxValue() const
{
return maxValue;
}
double XYAxis::getUnit1PxToValue() const
{
return unit1PxToValue;
}
double XYAxis::getUnit1ValueToPx() const
{
return unit1ValueToPx;
}
double XYAxis::pxToValue(double px) const
{
return px * unit1PxToValue + minValue;
}
double XYAxis::valueToPx(double value) const
{
return (value - minValue) * unit1ValueToPx;
}
void XYAxis::draw(QPainter *painter)
{
painter->fillRect(theRect, QColor(0, 180, 200));
switch (this->getAxisPosition())
{
case AtRight:
//drawRight(painter);
break;
case AtLeft:
drawLeft(painter);
break;
case AtTop:
//drawTop(painter);
break;
case AtBottom:
drawBottom(painter);
break;
default:
break;
}
}
void XYAxis::drawLeft(QPainter *painter)
{
painter->save();
painter->drawLine(theRect.topRight(), theRect.bottomRight());
const int right_pos = theRect.right();
for (int i = 0; i < tickPos.count(); i++)
{
const int y_pos = tickPos.at(i);
painter->drawLine(QPoint(right_pos, y_pos),
QPoint(right_pos - 5, y_pos));
painter->drawText(right_pos - 5 - painter->fontMetrics().boundingRect(tickLabel.at(i)).width(),
y_pos + painter->fontMetrics().height() / 2,
tickLabel.at(i));
}
painter->restore();
}
void XYAxis::drawBottom(QPainter *painter)
{
painter->save();
painter->drawLine(theRect.topLeft(), theRect.topRight());
const int top_pos = theRect.top();
for (int i = 0; i < tickPos.count(); i++)
{
const int x_pos = tickPos.at(i);
painter->drawLine(QPoint(x_pos, top_pos),
QPoint(x_pos, top_pos + 5));
painter->drawText(x_pos - painter->fontMetrics().boundingRect(tickLabel.at(i)).width() / 2,
top_pos + 5 + painter->fontMetrics().height(),
tickLabel.at(i));
}
painter->restore();
}
void XYAxis::calcAxis()
{
if (minLimit >= maxLimit || theRect.isNull())
return;
if (minValue > maxValue)
{
std::swap(minValue, maxValue);
}
if (minLimit > minValue)
{
minValue = minLimit;
}
if (maxLimit < maxValue)
{
maxValue = maxLimit;
}
switch (this->getAxisPosition())
{
case AtBottom:
{
//横向x轴
calcSpace(theRect.width() - 1);
//计算刻度线
const double right_pos = theRect.right();
tickPos.clear();
tickLabel.clear();
const int precision = getTickPrecision();
//i是用刻度px算坐标位置;j是用刻度px算i对应的value
//条件i>pos-N是为了显示最大值那个刻度
for (double i = theRect.left() + pxStart, j = pxStart; i < right_pos + 2; i += pxSpace, j += pxSpace)
{
tickPos.push_back(std::round(i));
const double label_value = (minValue + (j)*unit1PxToValue);
QString label_text = QString::number(label_value, 'f', precision);
if (label_text == "-0")
{ //会有-0
label_text = "0";
}
tickLabel.push_back(label_text);
}
}
break;
case AtLeft:
{
//竖向y轴
calcSpace(theRect.height() - 1);
//计算刻度线
const double top_pos = theRect.top();
tickPos.clear();
tickLabel.clear();
const int precision = getTickPrecision();
//i是用刻度px算坐标位置;j是用刻度px算i对应的value
//条件i>pos-N是为了显示最大值那个刻度
for (double i = theRect.bottom() - pxStart, j = pxStart; i > top_pos - 2; i -= pxSpace, j += pxSpace)
{
tickPos.push_back(std::round(i));
const double label_value = (minValue + (j)*unit1PxToValue);
QString label_text = QString::number(label_value, 'f', precision);
if (label_text == "-0")
{ //会有-0
label_text = "0";
}
tickLabel.push_back(label_text);
}
}
break;
default:
break;
}
emit axisChanged();
}
void XYAxis::calcSpace(double axisLength)
{
//计算每单位值
//为什么算了两个互为倒数的数呢?因为浮点数精度问题
unit1PxToValue = (maxValue - minValue) / (axisLength);
unit1ValueToPx = (axisLength) / (maxValue - minValue);
//计算间隔和起点
//计算刻度间隔及刻度起点
switch (theMode)
{
case FixedValue:
//该模式ValueSpace固定不变;
valueSpace = fixedValueSpace;
pxSpace = calcPxSpace(unit1PxToValue, valueSpace);
pxStart = calcPxStart(unit1PxToValue, valueSpace, minValue, maxValue);
break;
case RefPixel:
valueSpace = calcValueSpace(unit1PxToValue, refPixelSpace);
pxSpace = calcPxSpace(unit1PxToValue, valueSpace);
pxStart = calcPxStart(unit1PxToValue, valueSpace, minValue, maxValue);
break;
default:
break;
}
}
double XYAxis::calcPxSpace(double unitP2V, double valueSpace) const
{
//这里与真0.0比较
if (unitP2V <= 0.0)
{
qWarning() << __FUNCTION__ << "unitP2V is too min" << unitP2V;
return 30.0;
}
return valueSpace / unitP2V;
}
double XYAxis::calcPxStart(double unitP2V, double valueSpace, double valueMin, double valueMax) const
{
Q_UNUSED(valueMax)
if (unitP2V <= 0.0 || valueSpace <= 0.0)
{
没有合适的资源?快使用搜索试试~ 我知道了~
GrapgicsView_employ,qtcharts使用
共58个文件
cpp:10个
obj:10个
tlog:10个
需积分: 5 0 下载量 3 浏览量
2022-09-14
17:32:23
上传
评论
收藏 5.38MB RAR 举报
温馨提示
GrapgicsView_employ,qtcharts使用
资源详情
资源评论
资源推荐
收起资源包目录
GrapgicsView_employ.rar (58个子文件)
GrapgicsView_employ
myitem.cpp 7KB
.vs
GrapgicsView_employ
v14
.suo 44KB
myview.cpp 925B
GrapgicsView_employ.sln 965B
GrapgicsView_employ.pro.user 23KB
mycharts.cpp 16KB
Win32
Debug
moc_xyaxis.obj 72KB
vc140.pdb 2.03MB
myview.obj 96KB
mainwindow.obj 266KB
GrapgicsView_employ.Build.CppClean.log 3KB
moc_myview.obj 84KB
qt.log 5KB
moc_mainwindow.obj 67KB
main.obj 87KB
xyaxis.obj 180KB
myitem.obj 129KB
Grapgics.12D171F1.tlog
CL.write.1.tlog 9KB
CL.read.1.tlog 295KB
custombuild.read.1.tlog 280B
GrapgicsView_employ.lastbuildstate 228B
CL.command.1.tlog 18KB
link.write.1.tlog 2KB
link.command.1.tlog 4KB
link.read.1.tlog 6KB
custombuild.write.1.tlog 308B
custombuild.command.1.tlog 768B
GrapgicsView_employ.write.1u.tlog 1KB
moc_mycharts.obj 89KB
GrapgicsView_employ.log 195B
mycharts.obj 144KB
GrapgicsView_employ.vcxproj 16KB
.qmake.stash 1KB
myitem.h 1KB
mycharts.h 3KB
main.cpp 551B
mainwindow.ui 4KB
GrapgicsView_employ.vcxproj.filters 3KB
GeneratedFiles
ui_mainwindow.h 5KB
Debug
moc_mainwindow.cpp 3KB
moc_xyaxis.cpp 7KB
moc_mycharts.cpp 3KB
moc_myview.cpp 3KB
debug
moc_predefs.h.cbt 59B
moc_predefs.h 234B
GrapgicsView_employ.exe 169KB
GrapgicsView_employ.pdb 3.11MB
GrapgicsView_employ.ilk 1.81MB
xyaxis.h 6KB
GrapgicsView_employ.pro.user.c88ac6b 23KB
mainwindow.cpp 2KB
GrapgicsView_employ.vcxproj.user 606B
mainwindow.h 429B
GrapgicsView_employ.pro 1KB
GrapgicsView_employ.VC.db 16.22MB
xyaxis.cpp 18KB
release
moc_predefs.h.cbt 61B
myview.h 695B
共 58 条
- 1
讳疾忌医丶
- 粉丝: 1w+
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0