加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ChartView.cpp 4.87 KB
一键复制 编辑 原始数据 按行查看 历史
unknown 提交于 2022-08-04 19:07 . tijao
#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);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化