/
echartstest.nim
58 lines (49 loc) · 1.47 KB
/
echartstest.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import karax / [kbase, vdom, kdom, vstyles, karax, karaxdsl, jdict, jstrutils, jjson]
type
Views = enum
Customers, Products
var
currentView = Customers
type
EChart* = ref object
proc echartsInit(n: Element): EChart {.importc: "echarts.init".}
proc setOption(x: EChart; option: JsonNode) {.importcpp.}
proc postRender(data: RouterData) =
if currentView == Products:
let myChart = echartsInit(kdom.getElementById("echartSection"))
# specify chart configuration item and data
let option = %*{
"title": {
"text": "ECharts entry example"
},
"tooltip": {},
"legend": {
"data": ["Sales"]
},
"xAxis": {
"data": ["shirt","cardign","chiffon shirt","pants","heels","socks"]
},
"yAxis": {},
"series": [{
"name": "Sales",
"type": "bar",
"data": [5, 20, 36, 10, 10, 20]
}]
}
myChart.setOption(option)
proc createDom(data: RouterData): VNode =
let hash = data.hashPart
if hash == cstring"#/Products": currentView = Products
else: currentView = Customers
result = buildHtml(tdiv):
ul(class = "tabs"):
for v in low(Views)..high(Views):
li:
a(href = "#/" & $v):
text cstring($v)
if currentView == Products:
tdiv(id = "echartSection", style = style((width, kstring"600px"), (height, kstring"400px")))
tdiv:
text "other section"
setRenderer createDom, "ROOT", postRender
setForeignNodeId "echartSection"