mirror of https://github.com/status-im/nimqml.git
Added chart example #14
This commit is contained in:
parent
61d2cd0f93
commit
d71c19f217
|
@ -0,0 +1,2 @@
|
|||
main
|
||||
nimcache
|
|
@ -0,0 +1,10 @@
|
|||
[Package]
|
||||
bin = "main"
|
||||
name = "charts"
|
||||
version = "0.1.0"
|
||||
author = "Filippo Cucchetto"
|
||||
description = "charts"
|
||||
license = "MIT"
|
||||
|
||||
[Deps]
|
||||
Requires: "nim >= 0.12.1, nimqml >= 0.5.0"
|
|
@ -0,0 +1,24 @@
|
|||
import NimQml, mylistmodel
|
||||
|
||||
proc mainProc() =
|
||||
echo "Starting"
|
||||
var app = newQApplication()
|
||||
defer: app.delete
|
||||
|
||||
var myListModel = newMyListModel();
|
||||
defer: myListModel.delete
|
||||
|
||||
var engine = newQQmlApplicationEngine()
|
||||
defer: engine.delete
|
||||
|
||||
var variant = newQVariant(myListModel)
|
||||
defer: variant.delete
|
||||
|
||||
engine.setRootContextProperty("myListModel", variant)
|
||||
engine.load("main.qml")
|
||||
|
||||
app.exec()
|
||||
|
||||
when isMainModule:
|
||||
mainProc()
|
||||
GC_fullcollect()
|
|
@ -0,0 +1 @@
|
|||
--path:"../../src"
|
|
@ -0,0 +1,30 @@
|
|||
import QtQuick 2.7
|
||||
import QtCharts 2.2
|
||||
import QtQuick.Window 2.3
|
||||
|
||||
Window {
|
||||
width: 400
|
||||
height: 300
|
||||
title: "Charts"
|
||||
|
||||
Component.onCompleted: visible = true
|
||||
|
||||
ChartView {
|
||||
id: view
|
||||
|
||||
anchors.fill: parent
|
||||
|
||||
VXYModelMapper {
|
||||
id: mapper
|
||||
model: myListModel
|
||||
series: lineSeries
|
||||
xColumn: 0
|
||||
yColumn: 1
|
||||
}
|
||||
|
||||
LineSeries {
|
||||
id: lineSeries
|
||||
name: "LineSeries"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
import NimQml, Tables
|
||||
|
||||
type
|
||||
Point = object
|
||||
x: int
|
||||
y: int
|
||||
|
||||
QtObject:
|
||||
type
|
||||
MyListModel* = ref object of QAbstractListModel
|
||||
points*: seq[Point]
|
||||
|
||||
proc delete(self: MyListModel) =
|
||||
self.QAbstractListModel.delete
|
||||
|
||||
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
|
||||
let point = self.points[index.row]
|
||||
if index.column == 0:
|
||||
return newQVariant(point.x)
|
||||
elif index.column == 1:
|
||||
return newQVariant(point.y)
|
||||
else:
|
||||
return
|
Loading…
Reference in New Issue