我有一个创建多个图表的功能,每个创建的图表都有缩放和平移的功能。 我希望在创建的任何图表上执行的操作都可以传播到所有其他图表。 基本上,我希望他们共享轴,类似功能以喜欢sharex和matplotlib中的sharey。 因此,当我缩放一个图表时,所有其他图表也会缩放,或者当我滚动一个图表时,所有其他图表也会滚动。
所有图表视图都添加到ui->chartlayout
中。
创建后如何将它们绑在一起?
创建不同图表的功能
void MainWindow::CreateChart()
{
//![1]
QtCharts::QLineSeries *series = new QtCharts::QLineSeries();
for (int i = 0; i < 500; i++) {
QPointF p((qreal) i, qSin(M_PI / 50 * i) * 100);
p.ry() += QRandomGenerator::global()->bounded(20);
*series << p;
//series->setPointLabelsVisible(true); // is false by default
//series->setPointLabelsColor(Qt::black);
//series->setPointLabelsFormat("@yPoint");
}
//![1]
Chart *chart = new Chart();
chart->addSeries(series);
chart->setTitle("Zoom in/out example");
chart->setAnimationOptions(QChart::SeriesAnimations);
chart->legend()->hide();
chart->createDefaultAxes();
chart->setAcceptHoverEvents(true);
ChartView *chartView = new ChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
ui->chartlayout->addWidget(chartView);
this->grabGesture(Qt::PanGesture);
this->grabGesture(Qt::PinchGesture);
connect(chartView,&ChartView::Out,this,&MainWindow::UpdateStatusBar);
}
chartview.h
#ifndef CHARTVIEW_H
#define CHARTVIEW_H
#include <QtCharts/QChartView>
#include <QtWidgets/QRubberBand>
QT_CHARTS_USE_NAMESPACE
//![1]
class ChartView : public QChartView
//![1]
{
Q_OBJECT
public:
ChartView(QChart *chart, QWidget *parent = 0);
//![2]
protected:
bool viewportEvent(QEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void wheelEvent(QWheelEvent *event);
//![2]
signals:
void Out(QString val);
private:
bool m_isTouching;
};
#endif // CHARTVIEW_H
chartview.cpp
#include "chartview.h"
#include <QtGui/QMouseEvent>
#include <QDebug>
#include <QValueAxis>
ChartView::ChartView(QChart *chart, QWidget *parent) :
QChartView(chart, parent),
m_isTouching(false)
{
setRubberBand(QChartView::RectangleRubberBand);
}
bool ChartView::viewportEvent(QEvent *event)
{
if (event->type() == QEvent::TouchBegin) {
// By default touch events are converted to mouse events. So
// after this event we will get a mouse event also but we want
// to handle touch events as gestures only. So we need this safeguard
// to block mouse events that are actually generated from touch.
m_isTouching = true;
// Turn off animations when handling gestures they
// will only slow us down.
chart()->setAnimationOptions(QChart::NoAnimation);
}
return QChartView::viewportEvent(event);
}
void ChartView::mousePressEvent(QMouseEvent *event)
{
if (m_isTouching)
return;
QChartView::mousePressEvent(event);
}
void ChartView::mouseMoveEvent(QMouseEvent *event)
{
if (m_isTouching)
return;
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
qreal x = (event->pos()).x();
qreal y = (event->pos()).y();
qreal xVal = chart()->mapToValue(event->pos()).x();
qreal yVal = chart()->mapToValue(event->pos()).y();
QString val = QString("X = %1, Y = %2)").arg(xVal).arg(yVal);
emit Out(val);
}
QChartView::mouseMoveEvent(event);
}
void ChartView::mouseReleaseEvent(QMouseEvent *event)
{
if (m_isTouching)
m_isTouching = false;
//if(event->button() == Qt::RightButton)
//{
// qDebug() << "my overriden event";
// return; //event doesn't go further
// }
// Because we disabled animations when touch event was detected
// we must put them back on.
chart()->setAnimationOptions(QChart::SeriesAnimations);
QChartView::mouseReleaseEvent(event);
}
//![1]
void ChartView::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Plus:
chart()->zoomIn();
break;
case Qt::Key_Minus:
chart()->zoomOut();
break;
case Qt::Key_Escape:
chart()->zoomReset();
break;
case Qt::Key_Left:
chart()->scroll(-100, 0);
break;
case Qt::Key_Right:
chart()->scroll(100, 0);
break;
case Qt::Key_Up:
chart()->scroll(0, 100);
break;
case Qt::Key_Down:
chart()->scroll(0, -100);
break;
default:
QGraphicsView::keyPressEvent(event);
break;
}
}
void ChartView::wheelEvent(QWheelEvent *event)
{
//qreal factor = event->angleDelta().y() > 0? 0.5: 2.0;
//chart()->zoom(factor);
//event->accept();
//QChartView::wheelEvent(event);
//QRectF r = QRectF(chart()->plotArea().left(),chart()->plotArea().top(),
// chart()->plotArea().width()/factor,chart()->plotArea().height()/factor);
//QPointF mousePos = mapFromGlobal(QCursor::pos());
//r.moveCenter(mousePos);
//chart()->zoomIn(r);
//QPointF delta = chart()->plotArea().center() -mousePos;
//chart()->scroll(delta.x(),-delta.y());
//QPoint numPixels = event->pixelDelta();
//QPoint numDegrees = event->angleDelta() / 8;
//if (!numPixels.isNull())
//{
// chart()->scroll(numPixels.x()*100,0);
//}
//else if (!numDegrees.isNull())
//{
// QPoint numSteps = numDegrees / 15;
// chart()->scroll(numSteps.x()*100,0);
//}
// event->accept();
}