-
Notifications
You must be signed in to change notification settings - Fork 0
/
19_tabulator_tbls.qmd
121 lines (103 loc) · 3.2 KB
/
19_tabulator_tbls.qmd
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
title: "Tabulator tables"
format:
html:
css: https://unpkg.com/tabulator-tables@5/dist/css/tabulator.min.css
theme: flatly
font: Georgia
self-contained: true
page-layout: custom
mode: visual
---
```{ojs}
//| echo: false
// https://observablehq.com/@observablehq/input-table
dat = d3.csvParse(`region,area,turnover,plan_turnover,gp,plan_gp
region1,area1,1330000.01,1000000,1330000.01,1000000
region1,area2,3000000,2900000,1330000.01,1000000
region1,area3,5000000,4500000,1330000.01,1000000
region1,area4,7000000,8000000,1330000.01,1000000
region2,area5,7000000,8000000,1330000.01,1000000
`, d3.autoType)
Tabulator = require("tabulator-tables@5") //also css in the YAML header
myCurrCzkObj = ({
formatter: function(cell, formatterParams, onRendered){
return cell.getValue().toLocaleString('cs-CZ', { maximumFractionDigits: 0}) //, style: 'currency', currency: 'CZK'
},
bottomCalcFormatter: "money",
bottomCalc: function(values, data, calcParams) {
if (values && values.length) {
var total = values.reduce((sum, x) => +sum + +x);
return total
}
},
bottomCalcFormatterParams:
{
decimal: ",",
thousand: " ",
symbol: "CZK ",
precision: 0
},
variableHeight:true
})
myGrpTxtObj = ({
formatter: function(values, data, calcParams) {
return '---';
},
bottomCalc: function(values, data, calcParams) {
return '---';
},
bottomCalcFormatter: function(values, data, calcParams) {
return '---';
}
})
myPctObj = ({
formatter: function(cell, formatterParams, onRendered){
return (cell.getValue()[0]*100/cell.getValue()[1]).toFixed(2) + "%"; //return the contents of the cell;
},
bottomCalc: function(values, data, calcParams) {
if (values && values.length) {
const first = values.map(x => x[0]).reduce((sum, x) => +sum + +x);
const second = values.map(x => x[1]).reduce((sum, x) => +sum + +x);
return (first*100/second).toFixed(2) + "%";
}
},
variableHeight:true
})
```
```{ojs}
//| echo: false
html`<style> h1 {position: sticky; top: 0;} </style>`
{
const data = dat
const table = new Tabulator(document.createElement("DIV"), {
data,
groupBy:["region"],
columnCalcs:"both", //show column calculations at top and bottom of table and in groups
groupStartOpen:true,
groupClosedShowCalcs:true, //show column calculations when a group is closed
columns:[
{title:"Region", field:"region", ...myGrpTxtObj},
{title:"Area", field:"area"},
{title:"Turnover", field:"turnover", ...myCurrCzkObj},
{title:"Plan Turnover", field:"plan_turnover", ...myCurrCzkObj},
{
title:"Plnění Turnover (%)",
field: "plneni_turnover_pct",
mutator: (value, data, type, params, component) => [data["turnover"], data["plan_turnover"]],
...myPctObj
},
{title:"Gross Profit", field:"gp", ...myCurrCzkObj},
{title:"Plan Gross Profit", field:"plan_gp", ...myCurrCzkObj},
{
title:"Plnění Gross Profit (%)",
field: "plneni_gp_pct",
mutator: (value, data, type, params, component) => [data["gp"], data["plan_gp"]],
...myPctObj
}
]
});
table.element.value = data;
return table.element;
}
```