Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
idontknowwhatis committed Aug 11, 2016
0 parents commit 78b21b5
Show file tree
Hide file tree
Showing 7 changed files with 494 additions and 0 deletions.
43 changes: 43 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <thread>
#include <QtCharts/QVXYModelMapper>

#include "mydatamodel.h"


void point_generator_proc(MyDataModel* model)
{
for(double t=0 ; ; t+=1)
{
double v = (1 + sin(t/10.0)) / 2.0;

model->addNewPoint(std::make_pair(t, v));

std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}


int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QQmlApplicationEngine engine;
auto context = engine.rootContext();

MyDataModel* myDataModel = new MyDataModel();

QtCharts::QVXYModelMapper* mapper = new QtCharts::QVXYModelMapper();
mapper->setModel(myDataModel);
mapper->setXColumn(0);
mapper->setYColumn(1);

std::thread point_generator_thread(point_generator_proc, myDataModel);

context->setContextProperty("mapper", mapper);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

return app.exec();
}
47 changes: 47 additions & 0 deletions main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtCharts 2.0

ApplicationWindow {
visible: true
width: 600
height: 300
property int timeStep: 0

ChartView {
id: chartView
anchors.fill: parent

ValueAxis {
id: axisX
min: 0
max: 400
}

Component.onCompleted: {
mapper.series = series2
}

LineSeries {
id: series1
axisX: axisX
}

LineSeries {
id: series2
axisX: axisX
}
}

Timer {
interval: 100
repeat: true
running: true
onTriggered: {
timeStep++;
var y = (1+Math.cos(timeStep/10.0))/2.0;
series1.append(timeStep, y);
}
}
}
51 changes: 51 additions & 0 deletions mydatamodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "mydatamodel.h"

MyDataModel::MyDataModel()
{

}


int MyDataModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_data.size();
}


int MyDataModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 2;
}


QVariant MyDataModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(orientation)
Q_UNUSED(role)

if(section == 0)
return "x";
else
return "y";
}


QVariant MyDataModel::data(const QModelIndex &index, int role) const
{
Q_UNUSED(role)

if (index.column() == 0)
return m_data[index.row()].first;
else
return m_data[index.row()].second;
}


void MyDataModel::addNewPoint(std::pair<double, double> point)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount() + 1);
m_data.push_back(point);
endInsertRows();
}
18 changes: 18 additions & 0 deletions mydatamodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include <QAbstractTableModel>
#include <vector>

class MyDataModel : public QAbstractTableModel
{
Q_OBJECT
public:
MyDataModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

void addNewPoint(std::pair<double, double> point);
private:
std::vector<std::pair<double,double>> m_data;
};
12 changes: 12 additions & 0 deletions qtcharts_dynamic_series.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
TEMPLATE = app

QT += qml quick charts

RESOURCES += resources.qrc

SOURCES += main.cpp \
mydatamodel.cpp
OTHER_FILES += *.qml

HEADERS += \
mydatamodel.h
318 changes: 318 additions & 0 deletions qtcharts_dynamic_series.pro.user

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>

0 comments on commit 78b21b5

Please sign in to comment.