Last active
October 27, 2020 19:30
-
-
Save danielbuechele/81140003b9e0a82f9b6ee1e31ab3b3ea to your computer and use it in GitHub Desktop.
Covid Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!config.runsInWidget) { | |
return; | |
} | |
const url = `https://api.coronavirus.data.gov.uk/v1/data?filters=areaName=United%2520Kingdom;areaType=overview&structure=%7B%22date%22:%22date%22,%22value%22:%22newCasesByPublishDate%22%7D`; | |
const req = new Request(url); | |
const res = await req.loadJSON(); | |
const chart = { | |
type: "line", | |
options: { | |
elements: { point: { radius: 0 } }, | |
scales: { | |
xAxes: [{ gridLines: { display: false }, ticks: { display: false } }], | |
yAxes: [{ gridLines: { display: false }, ticks: { display: false } }], | |
}, | |
legend: { display: false }, | |
}, | |
data: { | |
labels: [0, 0, 0, 0, 0, 0, 0], | |
datasets: [ | |
{ | |
data: res.data | |
.slice(0, 7) | |
.map((d) => d.value) | |
.reverse(), | |
fill: false, | |
borderColor: "white", | |
lineTension: 0.3, | |
borderWidth: 10, | |
}, | |
{ | |
data: res.data | |
.slice(7, 14) | |
.map((d) => d.value) | |
.reverse(), | |
fill: false, | |
borderColor: "rgba(255, 255, 255, 0.3)", | |
lineTension: 0.3, | |
borderWidth: 10, | |
}, | |
], | |
}, | |
}; | |
const i = await new Request( | |
"https://quickchart.io/chart?c=" + encodeURIComponent(JSON.stringify(chart)) | |
); | |
const img = await i.loadImage(); | |
const currentValue = res.data[0].value; | |
const prevValue = res.data[7].value; | |
const date = new Date(res.data[0].date); | |
let w = new ListWidget(); | |
w.backgroundColor = new Color("#000"); | |
let titleTxt = w.addText(currentValue.toLocaleString()); | |
titleTxt.textColor = Color.white(); | |
titleTxt.font = Font.boldSystemFont(22); | |
let relativeDate = date.toLocaleDateString(); | |
if (date.toDateString() === new Date().toDateString()) { | |
relativeDate = "Today"; | |
} else if ( | |
date.toDateString() === new Date(new Date() - 86400000).toDateString() | |
) { | |
relativeDate = "Yesterday"; | |
} | |
let subTxt = w.addText( | |
`${currentValue > prevValue ? "↗" : "↘"} ${Math.abs( | |
currentValue - prevValue | |
).toLocaleString()} ${relativeDate}` | |
); | |
subTxt.textColor = Color.white(); | |
subTxt.textOpacity = 0.5; | |
subTxt.font = Font.systemFont(12); | |
w.addSpacer(5); | |
let image = w.addImage(img); | |
Script.setWidget(w); | |
Script.complete(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment