Further improved the charts example

This commit is contained in:
Filippo Cucchetto 2018-03-03 10:31:27 +01:00
parent d71c19f217
commit 570027c231
2 changed files with 90 additions and 37 deletions

View File

@ -1,5 +1,7 @@
import QtQuick 2.7
import QtCharts 2.2
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Window 2.3
Window {
@ -9,10 +11,14 @@ Window {
Component.onCompleted: visible = true
ColumnLayout {
anchors.fill: parent
ChartView {
id: view
anchors.fill: parent
Layout.fillHeight: true
Layout.fillWidth: true
VXYModelMapper {
id: mapper
@ -25,6 +31,24 @@ Window {
LineSeries {
id: lineSeries
name: "LineSeries"
axisX: ValueAxis {
min: 0
max: myListModel.maxX
}
axisY: ValueAxis {
min: 0
max: myListModel.maxY
}
}
}
RowLayout {
Layout.fillWidth: true
Button {
text: "Add random point"
onClicked: myListModel.addRandomPoint()
}
}
}
}

View File

@ -1,4 +1,4 @@
import NimQml, Tables
import NimQml, Tables, random
type
Point = object
@ -9,6 +9,8 @@ QtObject:
type
MyListModel* = ref object of QAbstractListModel
points*: seq[Point]
maxX: int
maxY: int
proc delete(self: MyListModel) =
self.QAbstractListModel.delete
@ -16,33 +18,60 @@ QtObject:
proc setup(self: MyListModel) =
self.QAbstractListModel.setup
proc newMyListModel*(): MyListModel =
new(result, delete)
result.points = @[Point(x: 10, y: 20), Point(x: 20, y: 30)]
result.setup
method rowCount(self: MyListModel, index: QModelIndex = nil): int =
if index == nil or not index.isValid:
return self.points.len
return 0
method columnCount(self: MyListModel, index: QModelIndex = nil): int =
if index == nil or not index.isValid:
return 2
return 0
method data(self: MyListModel, index: QModelIndex, role: int): QVariant =
result = nil
if not index.isValid:
return
if index.row < 0 or index.row >= self.points.len:
return
if index.column < 0 or index.column >= 2:
return
if role == 0:
let point = self.points[index.row]
if index.column == 0:
return newQVariant(point.x)
elif index.column == 1:
return newQVariant(point.y)
else:
return
proc getMaxY(self: MyListModel): int {.slot.} =
return self.maxY
proc maxYChanged(self: MyListModel, value: int) {.signal.}
QtProperty[int] maxY:
read = getMaxY
notify = maxYChanged
proc getMaxX(self: MyListModel): int {.slot.} =
return self.maxX
proc maxXChanged(self: MyListModel, value: int) {.signal.}
QtProperty[int] maxX:
read = getMaxX
notify = maxXChanged
proc addRandomPoint(self: MyListModel) {.slot.} =
let pos = self.points.len
self.beginInsertRows(newQModelIndex(), pos, pos)
let x = self.maxX + 1
let y = random(50)
if x > self.maxX:
self.maxX = x
self.maxXChanged(x)
if y > self.maxY:
self.maxY = y
self.maxYChanged(y)
echo "Adding " & $x & " " & $y
self.points.add(Point(x: x, y: y))
self.endInsertRows()
proc newMyListModel*(): MyListModel =
new(result, delete)
result.setup
result.points = @[]
result.maxX = 0
result.maxY = 50
result.addRandomPoint()