#include "ChartView.h"
#include <QApplication>
#include <QValueAxis>
ChartView::ChartView(QChart *chart, QWidget *parent)
: QChartView(chart, parent)
{
m_isPress = false;
m_ctrlPress = false;
m_alreadySaveRange = false;
m_coordItem = nullptr;
this->setDragMode(QGraphicsView::RubberBandDrag);
this->setMouseTracking(false);
setCursor(QCursor(Qt::PointingHandCursor)); //设置鼠标指针为手指形
}
ChartView::~ChartView()
{
}
void ChartView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_lastPoint = event->pos();
m_isPress = true;
}
}
void ChartView::mouseMoveEvent(QMouseEvent *event)
{
if (!m_coordItem)
{
m_coordItem = new QGraphicsSimpleTextItem(this->chart());
m_coordItem->setZValue(5);
m_coordItem->setPos(100, 60);
m_coordItem->show();
}
const QPoint curPos = event->pos();
QPointF curVal = this->chart()->mapToValue(QPointF(curPos));
QString coordStr = QString("X = %1, Y = %2").arg(curVal.x()).arg(curVal.y());
m_coordItem->setText(coordStr);
if (m_isPress)
{
QPoint offset = curPos - m_lastPoint;
m_lastPoint = curPos;
if (!m_alreadySaveRange)
{
this->saveAxisRange();
m_alreadySaveRange = true;
}
this->chart()->scroll(-offset.x(), offset.y());
}
}
void ChartView::mouseReleaseEvent(QMouseEvent *event)
{
m_isPress = false;
if (event->button() == Qt::RightButton)
{
if (m_alreadySaveRange)
{
this->chart()->axisX()->setRange(m_xMin, m_xMax);
this->chart()->axisY()->setRange(m_yMin, m_yMax);
}
}
}
void ChartView::wheelEvent(QWheelEvent *event)
{
const QPoint curPos = event->pos();
QPointF curVal = this->chart()->mapToValue(QPointF(curPos));
if (!m_alreadySaveRange)
{
this->saveAxisRange();
m_alreadySaveRange = true;
}
const double factor = 1.5;//缩放比例
if (m_ctrlPress)
{//Y轴
QValueAxis *axisY = dynamic_cast<QValueAxis*>(this->chart()->axisY());
const double yMin = axisY->min();
const double yMax = axisY->max();
const double yCentral = curVal.y();
double bottomOffset;
double topOffset;
if (event->delta() > 0)
{//放大
bottomOffset = 1.0 / factor * (yCentral - yMin);
topOffset = 1.0 / factor * (yMax - yCentral);
}
else
{//缩小
bottomOffset = 1.0 * factor * (yCentral - yMin);
topOffset = 1.0 * factor * (yMax - yCentral);
}
this->chart()->axisY()->setRange(yCentral - bottomOffset, yCentral + topOffset);
}
else
{//X轴
QValueAxis *axisX = dynamic_cast<QValueAxis*>(this->chart()->axisX());
const double xMin = axisX->min();
const double xMax = axisX->max();
const double xCentral = curVal.x();
double leftOffset;
double rightOffset;
if (event->delta() > 0)
{//放大
leftOffset = 1.0 / factor * (xCentral - xMin);
rightOffset = 1.0 / factor * (xMax - xCentral);
}
else
{//缩小
leftOffset = 1.0 * factor * (xCentral - xMin);
rightOffset = 1.0 * factor * (xMax - xCentral);
}
this->chart()->axisX()->setRange(xCentral - leftOffset, xCentral + rightOffset);
}
}
void ChartView::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Control)
{
m_ctrlPress = true;
}
}
void ChartView::keyReleaseEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Control)
{
m_ctrlPress = false;
}
}
void ChartView::saveAxisRange()
{
QValueAxis *axisX = dynamic_cast<QValueAxis*>(this->chart()->axisX());
m_xMin = axisX->min();
m_xMax = axisX->max();
QValueAxis *axisY = dynamic_cast<QValueAxis*>(this->chart()->axisY());
m_yMin = axisY->min();
m_yMax = axisY->max();
}
墨尘笔尖
- 粉丝: 372
- 资源: 9
最新资源
- 现场评定检查表——建筑外墙、屋面保温和建筑外墙装饰.docx
- 现场评定检查表--气体灭火系统.docx
- 消防第三方技术服务模拟验收抽查记录表.doc
- 现场评定检查表——总平面布局.docx
- 消防验收过程服务--现场记录表.doc
- 消防第三方技术服务现场交底监督记录表.doc
- 向日葵被控端绿色精简运行版
- 学生心理档案表.docx
- 验收确认单表格.docx
- 阳宅净宅表文.docx
- 医疗废弃物建设项目环境风险简单分析表.docx
- 原材料检测报告.docx
- 造林补助实施方案小班一览表、造林补助(新增部分)分行政村(国有林场)任务落实情况表.xls
- 造林补助(新增部分)分行政村(国有林场)任务落实情况表.docx
- 肢体残疾标准.docx
- 职工工伤与职业病致残等级分级表十级.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈