Skip to content

Commit e47d9e5

Browse files
committed
Dash.BigNumber
closes #248
1 parent c4cd464 commit e47d9e5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/client/stdlib/dash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./dash/resize.js";
2+
export * from "./dash/bigNumber.js";

src/client/stdlib/dash/bigNumber.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2+
// @ts-ignore
3+
import * as d3 from "npm:d3";
4+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5+
// @ts-ignore
6+
import {html} from "npm:htl";
7+
8+
export function BigNumber(
9+
number,
10+
{
11+
href,
12+
target = "_blank",
13+
title,
14+
format = ",.2~f",
15+
trend,
16+
trendFormat = "+.1~%",
17+
trendColor = trend == null ? "" : trend > 0 ? "green" : trend < 0 ? "red" : "orange",
18+
trendArrow = trend == null ? "" : trend > 0 ? "↗︎" : trend < 0 ? "↘︎" : "→",
19+
trendTitle,
20+
plot
21+
}: {
22+
href?: string;
23+
target?: string;
24+
title?: string;
25+
format?: string | ((x: any) => string);
26+
trend?: number;
27+
trendFormat?: string | ((x: any) => string);
28+
trendColor?: string;
29+
trendArrow?: string;
30+
trendTitle?: string;
31+
plot?: Node;
32+
} = {}
33+
) {
34+
if (typeof format !== "function") format = typeof number === "string" ? String : d3.format(format);
35+
if (typeof trendFormat !== "function") trendFormat = d3.format(trendFormat);
36+
const div = html`<div style="display: flex; flex-direction: column; font-family: var(--sans-serif);">
37+
<div style="text-transform: uppercase; font-size: 12px;">${title}</div>
38+
<div style="display: flex; flex-wrap: wrap; column-gap: 10px; align-items: baseline;">
39+
<div style="font-size: 32px; font-weight: bold; line-height: 1;">${(format as (x: any) => string)(number)}</div>
40+
${
41+
trend == null
42+
? null
43+
: html`<div style="font-size: 14px; color: ${trendColor};" ${{title: trendTitle}}>${(
44+
trendFormat as (x: any) => string
45+
)(trend)} ${trendArrow}</div>`
46+
}
47+
</div>
48+
${plot}
49+
</div>`;
50+
return href == null ? div : html`<a href=${href} target=${target} style="color: inherit;">${div}</a>`;
51+
}

0 commit comments

Comments
 (0)