-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLarge.js
119 lines (103 loc) · 3.61 KB
/
Large.js
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
const Data = importModule("Data");
const data = await Data.fetchData();
const readingMaxValue = Math.max(...data.map(el => el.value)).toFixed(2);
const readingDeviation = (readingMaxValue / 4).toFixed(2);
const readingTotal = data
.map(el => el.value)
.reduce((prev, curr) => prev + curr, 0)
.toFixed(2);
const costTotal = data
.map(el => el.costEstimate)
.reduce((prev, curr) => prev + curr, 0)
.toFixed(2);
function drawChart(data, maximum, deviation) {
const drawContext = new DrawContext();
drawContext.size = new Size(850, 350);
drawContext.opaque = false;
drawContext.respectScreenScale = true;
drawContext.setTextAlignedRight();
drawContext.setFont(Font.mediumRoundedSystemFont(14));
drawContext.setTextColor(Color.gray());
const bottomPadding = 20;
const leadingPadding = 62;
const verticalAxis = new Path();
for (let i = 0; i <= 4; i++) {
const yPoint =
drawContext.size.height -
(((drawContext.size.height - bottomPadding * 2) / 4) * i +
bottomPadding);
verticalAxis.move(new Point(leadingPadding, yPoint));
verticalAxis.addLine(new Point(drawContext.size.width, yPoint));
const point = (deviation * i).toFixed(2);
drawContext.drawTextInRect(
String(point),
new Rect(0, yPoint - 12, 60, 20),
);
}
drawContext.setTextAlignedCenter();
const availableHeight = drawContext.size.height - bottomPadding * 2;
const spacing = 4;
const barWidth = 20;
let textHeight = 20;
const textWidth = barWidth;
data.forEach((element, index) => {
const path = new Path();
const x = index * spacing + index * barWidth + leadingPadding;
const heightFactor = element.value / maximum;
const barHeight = heightFactor * availableHeight;
const rect = new Rect(
x,
drawContext.size.height - barHeight - bottomPadding,
barWidth,
barHeight,
);
path.addRoundedRect(rect, 4, 4);
drawContext.addPath(path);
const currTime = new Date();
const currDate = currTime.getDate();
if (element.date === currDate) {
drawContext.setFillColor(Color.blue());
} else if (element.value.toFixed(2) === maximum && element.date !== 1) {
drawContext.setFillColor(Color.red());
} else {
drawContext.setFillColor(Color.green());
}
drawContext.fillPath();
const textRect = new Rect(
x,
drawContext.size.height - bottomPadding,
textWidth,
textHeight,
);
const label = element.date;
drawContext.drawTextInRect(`${label}`, textRect);
});
return drawContext.getImage();
}
function createHStack(stack) {
const HStack = stack.addStack();
HStack.layoutHorizontally();
HStack.centerAlignContent();
return HStack;
}
function createVStack(stack) {
const VStack = stack.addStack();
VStack.layoutVertically();
VStack.centerAlignContent();
return VStack;
}
const widget = new ListWidget();
const largeStack = widget.addStack();
const largeChart = drawChart(data, readingMaxValue, readingDeviation);
largeStack.layoutVertically();
largeStack.centerAlignContent();
largeStack.addImage(largeChart);
const horCell = createHStack(largeStack);
const verCell1 = createVStack(horCell);
const verCell2 = createVStack(horCell);
verCell1.addText("Total Usage");
verCell1.addText(`${readingTotal} kW`);
verCell2.addText("Total Cost");
verCell2.addText(`${costTotal} JPY`);
Script.setWidget(widget);
Script.complete();