Added chart example #14

This commit is contained in:
Filippo Cucchetto 2018-03-02 23:20:57 +01:00
parent 61d2cd0f93
commit d71c19f217
6 changed files with 115 additions and 0 deletions

2
examples/charts/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
main
nimcache

View File

@ -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"

24
examples/charts/main.nim Normal file
View File

@ -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()

View File

@ -0,0 +1 @@
--path:"../../src"

30
examples/charts/main.qml Normal file
View File

@ -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"
}
}
}

View File

@ -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