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 QtQuick 2.7
import QtCharts 2.2 import QtCharts 2.2
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Window 2.3 import QtQuick.Window 2.3
Window { Window {
@ -9,22 +11,44 @@ Window {
Component.onCompleted: visible = true Component.onCompleted: visible = true
ChartView { ColumnLayout {
id: view anchors.fill: parent
anchors.fill: parent ChartView {
id: view
VXYModelMapper { Layout.fillHeight: true
id: mapper Layout.fillWidth: true
model: myListModel
series: lineSeries
xColumn: 0
yColumn: 1
}
LineSeries { VXYModelMapper {
id: lineSeries id: mapper
name: "LineSeries" model: myListModel
} series: lineSeries
xColumn: 0
yColumn: 1
}
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 type
Point = object Point = object
@ -9,6 +9,8 @@ QtObject:
type type
MyListModel* = ref object of QAbstractListModel MyListModel* = ref object of QAbstractListModel
points*: seq[Point] points*: seq[Point]
maxX: int
maxY: int
proc delete(self: MyListModel) = proc delete(self: MyListModel) =
self.QAbstractListModel.delete self.QAbstractListModel.delete
@ -16,33 +18,60 @@ QtObject:
proc setup(self: MyListModel) = proc setup(self: MyListModel) =
self.QAbstractListModel.setup 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 = method rowCount(self: MyListModel, index: QModelIndex = nil): int =
if index == nil or not index.isValid: return self.points.len
return self.points.len
return 0
method columnCount(self: MyListModel, index: QModelIndex = nil): int = method columnCount(self: MyListModel, index: QModelIndex = nil): int =
if index == nil or not index.isValid: return 2
return 2
return 0
method data(self: MyListModel, index: QModelIndex, role: int): QVariant = method data(self: MyListModel, index: QModelIndex, role: int): QVariant =
result = nil result = nil
if not index.isValid: if not index.isValid:
return return
if index.row < 0 or index.row >= self.points.len: if role == 0:
return let point = self.points[index.row]
if index.column < 0 or index.column >= 2: if index.column == 0:
return return newQVariant(point.x)
let point = self.points[index.row] elif index.column == 1:
if index.column == 0: return newQVariant(point.y)
return newQVariant(point.x)
elif index.column == 1: proc getMaxY(self: MyListModel): int {.slot.} =
return newQVariant(point.y) return self.maxY
else:
return 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()