代码拉取完成,页面将自动刷新
同步操作将从 1ndu5try/Qtweiniu 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include"ChartView.h"
#include<QValueAxis>
#include<QLineSeries>
QVector<QPointF> vec;
//现程序未使用qchart 以下代码仅供参考
void ChartView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_lastPoint = event->pos();
m_isPress = true;
}
QChartView::mousePressEvent(event);
}
void ChartView::leaveEvent(QEvent *event)
{
thepoint->setVisible(false);
x_line->setVisible(false);
y_line->setVisible(false);
QChartView::leaveEvent(event);
}
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();
}
if(!serice){
serice = (QLineSeries*)this->chart()->series().at(0);
vec = serice->pointsVector();
}
if(!x_line){
x_line = new QGraphicsLineItem(this->chart());
}
if(!y_line){
y_line = new QGraphicsLineItem(this->chart());
}
if(!thepoint){
thepoint = new QGraphicsEllipseItem(this->chart());
}
x_line->setPen(QPen(Qt::gray));
y_line->setPen(QPen(Qt::gray));
x_line->setLine(event->x(),0,event->x(),this->height());
y_line->setLine(0,event->y(),this->width(),event->y());
x_line->show();
y_line->show();
//十字定位线的实现
const QPoint curPos = event->pos();
QPointF curVal = this->chart()->mapToValue(QPointF(curPos));
float str = QString::number(curVal.x(), 'f', 1).toFloat();
QPointF thetest = this->chart()->mapToPosition(vec[str*10 +150],serice);
//QString coordStr = QString("X = %1, Y = %2").arg(curVal.x()).arg(curVal.y());
QString coordStr = QString("X = %1, Y = %2 x1 =%3 y1 = %4").arg(curVal.x()).arg(curVal.y()).arg(thetest.x()).arg(thetest.y());
QLinearGradient gLg;
gLg.setStart (-80,-60);
gLg.setFinalStop (-20,40);
gLg.setColorAt (0,qRgb (140,0,140));
gLg.setColorAt (1,qRgb (140,0,140));
QPen pen(qRgb (140,0,141));
thepoint->setPen (pen);
thepoint->setBrush (gLg);
thepoint->setOpacity (0.9);
thepoint->setRect(thetest.x()-5,thetest.y()-5,10,10);
thepoint->show();
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());
}
QChartView::mouseMoveEvent(event);
}
void ChartView::mouseReleaseEvent(QMouseEvent *event)
{
m_isPress = false;
QChartView::mouseReleaseEvent(event);
}
//保存原始位置
ChartView::ChartView(QWidget *parent)
{
this->setParent(parent);
//serice = (QLineSeries*)this->chart()->series().at(0);
// vec = serice->pointsVector();
}
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();
}
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);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。