目录
1.简单折线
2.数学折线
3.可滑动折线
1.简单折线
//![1] //! 折现段坐标QLineSeries *series = new QLineSeries(); //![1]//![2] //! 添加点series->append(0, 6);series->append(2, 4);series->append(3, 8);series->append(7, 4);series->append(10, 5);*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2); //![2]//![3]QChart *chart = new QChart();chart->legend()->hide();chart->addSeries(series);//根据已添加到图表中的系列为图表创建轴。以前添加到图表中的任何轴都将被删除chart->createDefaultAxes();chart->setTitle("Simple line chart example"); //![3]//![4]QChartView *chartView = new QChartView(chart);chartView->setRenderHint(QPainter::Antialiasing); //![4]
2.数学折线
QChartView* cview = new QChartView(this);cview->setRenderHints(QPainter::Antialiasing);QChart* chart = new QChart();chart->setTitle("Simple line");cview->setChart(chart);setCentralWidget(cview);QLineSeries* seriesS = new QLineSeries;QLineSeries* seriesC = new QLineSeries;seriesS->setName("Sinθ");seriesC->setName("Cosθ");chart->addSeries(seriesS);chart->addSeries(seriesC);//添加数据绘制qreal y0,y1,t=0,intv=0.1;size_t count=100;for (size_t i=0;i<count;i++){y0 = qSin(t);y1 = qCos(t);seriesS->append(t,y0);seriesC->append(t,y1);t += intv;}QValueAxis* axixX = new QValueAxis;axixX->setRange(0,10);chart->setAxisX(axixX,seriesS);chart->setAxisX(axixX,seriesC);QValueAxis* axixY = new QValueAxis;axixY->setRange(-2,1);chart->setAxisY(axixY,seriesS);chart->setAxisY(axixY,seriesC);
3.可滑动折线
本质逻辑上与上面差不多,但是如果要放大/缩小等操作,需要重写一下QChartView函数,如果是触摸的需要重写QChart的bool sceneEvent(QEvent *event);
bool viewportEvent(QEvent *event);void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void keyPressEvent(QKeyEvent *event);
chart.h
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#ifndef CHART_H
#define CHART_H#include <QtCharts/QChart>QT_BEGIN_NAMESPACE
class QGestureEvent;
QT_END_NAMESPACEQT_CHARTS_USE_NAMESPACE//![1]
class Chart : public QChart
//![1]
{
public:explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);~Chart();protected:bool sceneEvent(QEvent *event);private:bool gestureEvent(QGestureEvent *event);private:};#endif // CHART_H
chart.cpp
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#include "chart.h"
#include <QtWidgets/QGesture>
#include <QtWidgets/QGraphicsScene>
#include <QtWidgets/QGraphicsView>Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags): QChart(QChart::ChartTypeCartesian, parent, wFlags)
{// Seems that QGraphicsView (QChartView) does not grab gestures.// They can only be grabbed here in the QGraphicsWidget (QChart).grabGesture(Qt::PanGesture);grabGesture(Qt::PinchGesture);
}Chart::~Chart()
{}//![1]
bool Chart::sceneEvent(QEvent *event)
{if (event->type() == QEvent::Gesture)return gestureEvent(static_cast<QGestureEvent *>(event));return QChart::event(event);
}bool Chart::gestureEvent(QGestureEvent *event)
{if (QGesture *gesture = event->gesture(Qt::PanGesture)) {QPanGesture *pan = static_cast<QPanGesture *>(gesture);QChart::scroll(-(pan->delta().x()), pan->delta().y());}if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)QChart::zoom(pinch->scaleFactor());}return true;
}
//![1]
chartview.h
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#ifndef CHARTVIEW_H
#define CHARTVIEW_H#include <QtCharts/QChartView>
#include <QtWidgets/QRubberBand>QT_CHARTS_USE_NAMESPACE//![1]
class ChartView : public QChartView
//![1]
{
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);
//![2]private:bool m_isTouching;
};#endif
chartview.cpp
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#include "chartview.h"
#include <QtGui/QMouseEvent>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;QChartView::mouseMoveEvent(event);
}void ChartView::mouseReleaseEvent(QMouseEvent *event)
{if (m_isTouching)m_isTouching = false;// 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;
//![1]case Qt::Key_Left:chart()->scroll(-10, 0);break;case Qt::Key_Right:chart()->scroll(10, 0);break;case Qt::Key_Up:chart()->scroll(0, 10);break;case Qt::Key_Down:chart()->scroll(0, -10);break;default:QGraphicsView::keyPressEvent(event);break;}
}
main.cpp
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#include "chart.h"
#include "chartview.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCore/QtMath>
#include <QtCore/QRandomGenerator>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>QT_CHARTS_USE_NAMESPACEint main(int argc, char *argv[])
{QApplication a(argc, argv);//![1]QLineSeries *series = new 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;}
//![1]Chart *chart = new Chart();chart->addSeries(series);chart->setTitle("Zoom in/out example");chart->setAnimationOptions(QChart::SeriesAnimations);chart->legend()->hide();chart->createDefaultAxes();ChartView *chartView = new ChartView(chart);chartView->setRenderHint(QPainter::Antialiasing);QMainWindow window;window.setCentralWidget(chartView);window.resize(400, 300);window.grabGesture(Qt::PanGesture);window.grabGesture(Qt::PinchGesture);window.show();return a.exec();
}