mirror of https://github.com/status-im/nimqml.git
Further improved the charts example
This commit is contained in:
parent
d71c19f217
commit
570027c231
|
@ -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,22 +11,44 @@ Window {
|
|||
|
||||
Component.onCompleted: visible = true
|
||||
|
||||
ChartView {
|
||||
id: view
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
|
||||
anchors.fill: parent
|
||||
ChartView {
|
||||
id: view
|
||||
|
||||
VXYModelMapper {
|
||||
id: mapper
|
||||
model: myListModel
|
||||
series: lineSeries
|
||||
xColumn: 0
|
||||
yColumn: 1
|
||||
}
|
||||
Layout.fillHeight: true
|
||||
Layout.fillWidth: true
|
||||
|
||||
LineSeries {
|
||||
id: lineSeries
|
||||
name: "LineSeries"
|
||||
}
|
||||
VXYModelMapper {
|
||||
id: mapper
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
return self.points.len
|
||||
|
||||
method columnCount(self: MyListModel, index: QModelIndex = nil): int =
|
||||
if index == nil or not index.isValid:
|
||||
return 2
|
||||
return 0
|
||||
return 2
|
||||
|
||||
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
|
||||
let point = self.points[index.row]
|
||||
if index.column == 0:
|
||||
return newQVariant(point.x)
|
||||
elif index.column == 1:
|
||||
return newQVariant(point.y)
|
||||
else:
|
||||
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)
|
||||
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue