#include "chart.h"#include "ui_chart.h"chart::chart(QWidget *parent) : QMainWindow(parent), ui(new Ui::chart){ ui->setupUi(this); //qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); series = new QLineSeries(); series->append(momentInTime.toMSecsSinceEpoch(),0); m_chart = new QChart(); m_chart->addSeries(series); m_chart->legend()->hide(); m_chart->setTitle("Sunspots count (by Space Weather Prediction Center)"); //![3] //![4] axisX = new QDateTimeAxis; axisX->setTickCount(10); //axisX->setFormat("MMM yyyy"); axisX->setTitleText("Date"); m_chart->addAxis(axisX, Qt::AlignBottom); series->attachAxis(axisX); axisY = new QValueAxis; axisY->setLabelFormat("%i"); axisY->setTitleText("Sunspots count"); m_chart->addAxis(axisY, Qt::AlignLeft); series->attachAxis(axisY); //![4] //![5] chartView = new QChartView(m_chart); chartView->setRenderHint(QPainter::Antialiasing); ui->gridLayout->addWidget(chartView);}chart::~chart(){ delete ui;}void chart::on_pushButton_clicked(){ m_chart->removeSeries(series); series->append(momentInTime.toMSecsSinceEpoch(), ui->doubleSpinBox->value()); m_chart->addSeries(series);}
void MainWindow::on_pushButton_clicked(){ QDateTime minX = axisX->min(); QDateTime maxX = axisX->max(); qreal minY = axisY->min(); qreal maxY = axisY->max(); QDateTime x = QDateTime::currentDateTime(); qreal y = ui->doubleSpinBox->value(); axisX->setRange(minX, x > maxX ? x : maxX); axisY->setRange(minY, y > maxY ? y : maxY); series->append(x.toMSecsSinceEpoch(), y);}
connect(series, SIGNAL(pointAdded(int)), chartView, SLOT(update()));
C++ (Qt)#include <QApplication>#include <QMainWindow>#include <QChartView>#include <QChart>#include <QLineSeries>#include <QValueAxis>#include <QDateTimeAxis>#include <QDateTime>#include <QGraphicsSceneMouseEvent> using namespace QtCharts; const int SecsInHour = 3600; class MyChart : public QChart{protected: void mousePressEvent(QGraphicsSceneMouseEvent *event) override { event->accept(); } void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override { if (event->buttons().testFlag( Qt::LeftButton) ) { #if 1 scroll(event->lastPos().x() - event->pos().x(), 0); #else qint64 secs = SecsInHour / 2; if (event->pos().x() > event->lastPos().x()) secs = -secs; QDateTimeAxis * axis_x = qobject_cast<QDateTimeAxis *>(axisX()); QDateTime current = axis_x->min(); current = current.addSecs( secs ); axis_x->setRange(current, current.addDays(1)); #endif } }}; int main(int argc, char *argv[]){ QApplication application(argc, argv); QDateTime current(QDate::currentDate(), QTime()); QLineSeries *series = new QLineSeries; series->append(current.addSecs(SecsInHour * 1 ).toMSecsSinceEpoch() , 25); series->append(current.addSecs(SecsInHour * 2 ).toMSecsSinceEpoch() , 75); series->append(current.addSecs(SecsInHour * 3 ).toMSecsSinceEpoch() , 25); QDateTimeAxis *axisX = new QDateTimeAxis(); axisX->setRange(current, current.addDays(1)); axisX->setTickCount(12); axisX->setFormat("dd.MM hh:00"); QValueAxis *axisY = new QValueAxis(); axisY->setRange(0, 100); MyChart *chart = new MyChart(); chart->legend()->hide(); chart->addSeries(series); chart->setAxisX(axisX, series); chart->setAxisY(axisY, series); QChartView *chart_view = new QChartView(chart); QMainWindow main_window; main_window.setCentralWidget(chart_view); main_window.resize(1000, 600); main_window.show(); return application.exec();}