BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / demo_chart_cards.rs

demo_chart_cards.rs

refactor(app): extract app-base crate
metis Daniel Däschle · pushed by metis · 6 weeks ago
16e522c2 History
demo_chart_cards.rs 5.2 KiB Download Raw
1use serde_json::{Value, json};
2
3const FRUIT: [(&str, u32); 8] = [
4 ("Pear", 59),
5 ("Banana", 292),
6 ("Apple", 143),
7 ("Peach", 98),
8 ("Kiwi", 179),
9 ("Grapefruit", 50),
10 ("Orange", 212),
11 ("Cantaloupe", 68),
12];
13
14const PRODUCTS: [(&str, [u32; 8]); 4] = [
15 ("Outlook", [24, 27, 18, 30, 20, 35, 40, 45]),
16 ("Teams", [9, 100, 22, 40, 30, 45, 50, 55]),
17 ("Office", [10, 20, 30, 40, 50, 60, 70, 80]),
18 ("Windows", [10, 20, 30, 40, 50, 60, 70, 80]),
19];
20
21const TREND: [(&str, [u32; 6]); 5] = [
22 ("Outlook", [99, 6, 63, 64, 63, 78]),
23 ("Teams", [12, 82, 12, 33, 1, 80]),
24 ("Office", [66, 93, 65, 13, 90, 48]),
25 ("Windows", [9, 19, 0, 61, 21, 72]),
26 ("Exchange", [35, 11, 91, 97, 97, 45]),
27];
28
29pub fn cards() -> Vec<Value> {
30 vec![
31 card(vec![slice_chart("Chart.Donut", "Fruit sales")]),
32 card(vec![slice_chart("Chart.Pie", "Fruit sales")]),
33 card(vec![
34 text("Basic"),
35 json!({"type": "Chart.Gauge", "value": 50, "subLabel": "Risk score", "segments": [
36 {"legend": "Low risk", "size": 33, "color": "good"},
37 {"legend": "Medium risk", "size": 34, "color": "warning"},
38 {"legend": "High risk", "size": 33, "color": "attention"}
39 ]}),
40 text("Single value"),
41 json!({"type": "Chart.Gauge", "value": 35, "valueFormat": "fraction", "segments": [
42 {"legend": "Used", "size": 35},
43 {"legend": "Unused", "size": 65, "color": "neutral"}
44 ]}),
45 ]),
46 card(vec![json!({
47 "type": "Chart.Line", "title": "Daily usage", "xAxisTitle": "Days", "yAxisTitle": "Sales",
48 "colorSet": "categorical", "data": series_data(&TREND)
49 })]),
50 card(vec![json!({
51 "type": "Chart.VerticalBar", "title": "Fruit sales", "xAxisTitle": "Fruit", "yAxisTitle": "Crates",
52 "showBarValues": true, "colorSet": "categorical", "data": xy_data(&FRUIT)
53 })]),
54 card(vec![
55 json!({
56 "type": "Chart.VerticalBar.Grouped", "title": "Grouped", "xAxisTitle": "Days", "yAxisTitle": "Sales",
57 "colorSet": "diverging", "data": series_data(&PRODUCTS)
58 }),
59 json!({
60 "type": "Chart.VerticalBar.Grouped", "title": "Stacked", "stacked": true, "xAxisTitle": "Days",
61 "yAxisTitle": "Sales", "data": series_data(&PRODUCTS)
62 }),
63 ]),
64 card(vec![
65 text("AbsoluteWithAxis"),
66 json!({
67 "type": "Chart.HorizontalBar", "title": "Fruit sales", "xAxisTitle": "Fruit", "yAxisTitle": "Crates",
68 "colorSet": "diverging", "data": xy_data(&FRUIT[..6])
69 }),
70 text("AbsoluteNoAxis"),
71 json!({
72 "type": "Chart.HorizontalBar", "title": "Fruit sales", "displayMode": "AbsoluteNoAxis",
73 "data": xy_data(&FRUIT[..6])
74 }),
75 text("PartToWhole"),
76 json!({"type": "Chart.HorizontalBar", "title": "Learning goal", "displayMode": "PartToWhole",
77 "color": "categoricalPurple", "data": [
78 {"x": "Yes, I have defined my day of learning goal", "y": 15},
79 {"x": "No, I haven't yet had time to do it", "y": 24},
80 {"x": "I am not interested in learning", "y": 2}
81 ]}),
82 ]),
83 card(vec![json!({
84 "type": "Chart.HorizontalBar.Stacked", "title": "Sample", "showBarValues": true, "data": [
85 stacked_row("Outlook", [24, 27, 18]),
86 stacked_row("Teams", [9, 100, 22]),
87 stacked_row("Office", [40, 12, 60])
88 ]
89 })]),
90 ]
91}
92
93fn card(body: Vec<Value>) -> Value {
94 json!({"type": "AdaptiveCard", "version": "1.5", "body": body})
95}
96
97fn text(label: &str) -> Value {
98 json!({"type": "TextBlock", "text": label, "size": "medium", "weight": "bolder", "separator": true, "spacing": "large"})
99}
100
101fn slice_chart(chart_type: &str, title: &str) -> Value {
102 let data: Vec<Value> = FRUIT
103 .iter()
104 .map(|(legend, value)| json!({"legend": legend, "value": value}))
105 .collect();
106 json!({"type": chart_type, "title": title, "colorSet": "categorical", "data": data})
107}
108
109fn xy_data(points: &[(&str, u32)]) -> Vec<Value> {
110 points
111 .iter()
112 .map(|(label, value)| json!({"x": label, "y": value}))
113 .collect()
114}
115
116fn series_data<const COUNT: usize>(series: &[(&str, [u32; COUNT])]) -> Vec<Value> {
117 series
118 .iter()
119 .map(|(legend, values)| {
120 let points: Vec<Value> = values
121 .iter()
122 .enumerate()
123 .map(|(index, value)| json!({"x": format!("2023-05-{:02}", index + 1), "y": value}))
124 .collect();
125 json!({"legend": legend, "values": points})
126 })
127 .collect()
128}
129
130fn stacked_row(title: &str, values: [u32; 3]) -> Value {
131 let colors = ["good", "warning", "attention"];
132 let points: Vec<Value> = values
133 .iter()
134 .zip(colors)
135 .enumerate()
136 .map(|(index, (value, color))| {
137 json!({"legend": format!("2023-05-{:02}", index + 1), "value": value, "color": color})
138 })
139 .collect();
140 json!({"title": title, "data": points})
141}