qvac-charts 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist-lib/index.d.ts +3 -1
- package/dist-lib/index.js +2 -2
- package/dist-lib/lib/colors.d.ts +11 -0
- package/dist-lib/lib/colors.js +205 -0
- package/dist-lib/lib/render.d.ts +3 -2
- package/dist-lib/lib/render.js +3 -2
- package/dist-lib/lib/themes.d.ts +5 -0
- package/dist-lib/lib/themes.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,6 +47,17 @@ The package ships no logo. A host that wants its mark in the footer passes it to
|
|
|
47
47
|
chart.toSVG({ theme: 'dark', mark: { href: 'data:image/png;base64,…', width: 110, height: 48 } })
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
+
### Colors
|
|
51
|
+
|
|
52
|
+
`toSVG` takes an `accent` and a `palette` that replace the theme's. The accent colors the highlighted item, the callout and the deep end of a map's shading; the palette colors the other items or series, in order, and repeats when there are more of them than colors. An accent given alone leaves the theme's palette in place.
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
chart.toSVG({ theme: 'light', accent: 'blue' })
|
|
56
|
+
chart.toSVG({ theme: 'dark', accent: '#1d4ed8', palette: ['teal', 'tomato', 'gold'] })
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
A color is a CSS color name, read loosely (`'Light Blue'` is `lightblue`), or a 3- or 6-digit hex, with or without `#`, so a model can pass on what the user said. Anything else, and a palette that is empty or longer than 12, throws a `RangeError`. A color too close to the theme's background to read against it is darkened on the light theme and lightened on the dark one until it reaches a 3:1 contrast ratio. Without colors the chart is drawn exactly as before.
|
|
60
|
+
|
|
50
61
|
### Maps
|
|
51
62
|
|
|
52
63
|
The map data is its own entry, so a host that draws no maps does not load it. `mapsFor` takes the places a spec names and returns the world (about 120 KB) with the one continent that holds them (8 to 43 KB), loaded on demand:
|
package/dist-lib/index.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ export interface ChartOptions {
|
|
|
9
9
|
export interface SVGOptions {
|
|
10
10
|
theme?: ThemeName;
|
|
11
11
|
mark?: Mark;
|
|
12
|
+
accent?: string;
|
|
13
|
+
palette?: ReadonlyArray<string>;
|
|
12
14
|
}
|
|
13
15
|
export declare class Chart {
|
|
14
16
|
static schema: {
|
|
@@ -96,5 +98,5 @@ export declare class Chart {
|
|
|
96
98
|
readonly summary: string;
|
|
97
99
|
private normalized;
|
|
98
100
|
private constructor();
|
|
99
|
-
toSVG({ theme, mark }?: SVGOptions): string;
|
|
101
|
+
toSVG({ theme, mark, accent, palette }?: SVGOptions): string;
|
|
100
102
|
}
|
package/dist-lib/index.js
CHANGED
|
@@ -15,8 +15,8 @@ export class Chart {
|
|
|
15
15
|
this.type = normalized.type;
|
|
16
16
|
this.summary = summarize(normalized);
|
|
17
17
|
}
|
|
18
|
-
toSVG({ theme = 'dark', mark } = {}) {
|
|
19
|
-
return render(this.normalized, { theme, mark });
|
|
18
|
+
toSVG({ theme = 'dark', mark, accent, palette } = {}) {
|
|
19
|
+
return render(this.normalized, { theme, mark, accent, palette });
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
function summarize(chart) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Theme } from './themes.ts';
|
|
2
|
+
export declare const MAX_PALETTE = 12;
|
|
3
|
+
export declare const MIN_CONTRAST = 3;
|
|
4
|
+
export interface Overrides {
|
|
5
|
+
accent?: string | undefined;
|
|
6
|
+
palette?: ReadonlyArray<string> | undefined;
|
|
7
|
+
}
|
|
8
|
+
export declare function parseColor(input: unknown): string;
|
|
9
|
+
export declare function contrastRatio(a: string, b: string): number;
|
|
10
|
+
export declare function readable(color: string, bg: string): string;
|
|
11
|
+
export declare function withColors(t: Theme, { accent, palette }: Overrides): Theme;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { mix } from './themes.js';
|
|
2
|
+
const named = {
|
|
3
|
+
aliceblue: '#f0f8ff',
|
|
4
|
+
antiquewhite: '#faebd7',
|
|
5
|
+
aqua: '#00ffff',
|
|
6
|
+
aquamarine: '#7fffd4',
|
|
7
|
+
azure: '#f0ffff',
|
|
8
|
+
beige: '#f5f5dc',
|
|
9
|
+
bisque: '#ffe4c4',
|
|
10
|
+
black: '#000000',
|
|
11
|
+
blanchedalmond: '#ffebcd',
|
|
12
|
+
blue: '#0000ff',
|
|
13
|
+
blueviolet: '#8a2be2',
|
|
14
|
+
brown: '#a52a2a',
|
|
15
|
+
burlywood: '#deb887',
|
|
16
|
+
cadetblue: '#5f9ea0',
|
|
17
|
+
chartreuse: '#7fff00',
|
|
18
|
+
chocolate: '#d2691e',
|
|
19
|
+
coral: '#ff7f50',
|
|
20
|
+
cornflowerblue: '#6495ed',
|
|
21
|
+
cornsilk: '#fff8dc',
|
|
22
|
+
crimson: '#dc143c',
|
|
23
|
+
cyan: '#00ffff',
|
|
24
|
+
darkblue: '#00008b',
|
|
25
|
+
darkcyan: '#008b8b',
|
|
26
|
+
darkgoldenrod: '#b8860b',
|
|
27
|
+
darkgray: '#a9a9a9',
|
|
28
|
+
darkgreen: '#006400',
|
|
29
|
+
darkgrey: '#a9a9a9',
|
|
30
|
+
darkkhaki: '#bdb76b',
|
|
31
|
+
darkmagenta: '#8b008b',
|
|
32
|
+
darkolivegreen: '#556b2f',
|
|
33
|
+
darkorange: '#ff8c00',
|
|
34
|
+
darkorchid: '#9932cc',
|
|
35
|
+
darkred: '#8b0000',
|
|
36
|
+
darksalmon: '#e9967a',
|
|
37
|
+
darkseagreen: '#8fbc8f',
|
|
38
|
+
darkslateblue: '#483d8b',
|
|
39
|
+
darkslategray: '#2f4f4f',
|
|
40
|
+
darkslategrey: '#2f4f4f',
|
|
41
|
+
darkturquoise: '#00ced1',
|
|
42
|
+
darkviolet: '#9400d3',
|
|
43
|
+
deeppink: '#ff1493',
|
|
44
|
+
deepskyblue: '#00bfff',
|
|
45
|
+
dimgray: '#696969',
|
|
46
|
+
dimgrey: '#696969',
|
|
47
|
+
dodgerblue: '#1e90ff',
|
|
48
|
+
firebrick: '#b22222',
|
|
49
|
+
floralwhite: '#fffaf0',
|
|
50
|
+
forestgreen: '#228b22',
|
|
51
|
+
fuchsia: '#ff00ff',
|
|
52
|
+
gainsboro: '#dcdcdc',
|
|
53
|
+
ghostwhite: '#f8f8ff',
|
|
54
|
+
gold: '#ffd700',
|
|
55
|
+
goldenrod: '#daa520',
|
|
56
|
+
gray: '#808080',
|
|
57
|
+
green: '#008000',
|
|
58
|
+
greenyellow: '#adff2f',
|
|
59
|
+
grey: '#808080',
|
|
60
|
+
honeydew: '#f0fff0',
|
|
61
|
+
hotpink: '#ff69b4',
|
|
62
|
+
indianred: '#cd5c5c',
|
|
63
|
+
indigo: '#4b0082',
|
|
64
|
+
ivory: '#fffff0',
|
|
65
|
+
khaki: '#f0e68c',
|
|
66
|
+
lavender: '#e6e6fa',
|
|
67
|
+
lavenderblush: '#fff0f5',
|
|
68
|
+
lawngreen: '#7cfc00',
|
|
69
|
+
lemonchiffon: '#fffacd',
|
|
70
|
+
lightblue: '#add8e6',
|
|
71
|
+
lightcoral: '#f08080',
|
|
72
|
+
lightcyan: '#e0ffff',
|
|
73
|
+
lightgoldenrodyellow: '#fafad2',
|
|
74
|
+
lightgray: '#d3d3d3',
|
|
75
|
+
lightgreen: '#90ee90',
|
|
76
|
+
lightgrey: '#d3d3d3',
|
|
77
|
+
lightpink: '#ffb6c1',
|
|
78
|
+
lightsalmon: '#ffa07a',
|
|
79
|
+
lightseagreen: '#20b2aa',
|
|
80
|
+
lightskyblue: '#87cefa',
|
|
81
|
+
lightslategray: '#778899',
|
|
82
|
+
lightslategrey: '#778899',
|
|
83
|
+
lightsteelblue: '#b0c4de',
|
|
84
|
+
lightyellow: '#ffffe0',
|
|
85
|
+
lime: '#00ff00',
|
|
86
|
+
limegreen: '#32cd32',
|
|
87
|
+
linen: '#faf0e6',
|
|
88
|
+
magenta: '#ff00ff',
|
|
89
|
+
maroon: '#800000',
|
|
90
|
+
mediumaquamarine: '#66cdaa',
|
|
91
|
+
mediumblue: '#0000cd',
|
|
92
|
+
mediumorchid: '#ba55d3',
|
|
93
|
+
mediumpurple: '#9370db',
|
|
94
|
+
mediumseagreen: '#3cb371',
|
|
95
|
+
mediumslateblue: '#7b68ee',
|
|
96
|
+
mediumspringgreen: '#00fa9a',
|
|
97
|
+
mediumturquoise: '#48d1cc',
|
|
98
|
+
mediumvioletred: '#c71585',
|
|
99
|
+
midnightblue: '#191970',
|
|
100
|
+
mintcream: '#f5fffa',
|
|
101
|
+
mistyrose: '#ffe4e1',
|
|
102
|
+
moccasin: '#ffe4b5',
|
|
103
|
+
navajowhite: '#ffdead',
|
|
104
|
+
navy: '#000080',
|
|
105
|
+
oldlace: '#fdf5e6',
|
|
106
|
+
olive: '#808000',
|
|
107
|
+
olivedrab: '#6b8e23',
|
|
108
|
+
orange: '#ffa500',
|
|
109
|
+
orangered: '#ff4500',
|
|
110
|
+
orchid: '#da70d6',
|
|
111
|
+
palegoldenrod: '#eee8aa',
|
|
112
|
+
palegreen: '#98fb98',
|
|
113
|
+
paleturquoise: '#afeeee',
|
|
114
|
+
palevioletred: '#db7093',
|
|
115
|
+
papayawhip: '#ffefd5',
|
|
116
|
+
peachpuff: '#ffdab9',
|
|
117
|
+
peru: '#cd853f',
|
|
118
|
+
pink: '#ffc0cb',
|
|
119
|
+
plum: '#dda0dd',
|
|
120
|
+
powderblue: '#b0e0e6',
|
|
121
|
+
purple: '#800080',
|
|
122
|
+
rebeccapurple: '#663399',
|
|
123
|
+
red: '#ff0000',
|
|
124
|
+
rosybrown: '#bc8f8f',
|
|
125
|
+
royalblue: '#4169e1',
|
|
126
|
+
saddlebrown: '#8b4513',
|
|
127
|
+
salmon: '#fa8072',
|
|
128
|
+
sandybrown: '#f4a460',
|
|
129
|
+
seagreen: '#2e8b57',
|
|
130
|
+
seashell: '#fff5ee',
|
|
131
|
+
sienna: '#a0522d',
|
|
132
|
+
silver: '#c0c0c0',
|
|
133
|
+
skyblue: '#87ceeb',
|
|
134
|
+
slateblue: '#6a5acd',
|
|
135
|
+
slategray: '#708090',
|
|
136
|
+
slategrey: '#708090',
|
|
137
|
+
snow: '#fffafa',
|
|
138
|
+
springgreen: '#00ff7f',
|
|
139
|
+
steelblue: '#4682b4',
|
|
140
|
+
tan: '#d2b48c',
|
|
141
|
+
teal: '#008080',
|
|
142
|
+
thistle: '#d8bfd8',
|
|
143
|
+
tomato: '#ff6347',
|
|
144
|
+
turquoise: '#40e0d0',
|
|
145
|
+
violet: '#ee82ee',
|
|
146
|
+
wheat: '#f5deb3',
|
|
147
|
+
white: '#ffffff',
|
|
148
|
+
whitesmoke: '#f5f5f5',
|
|
149
|
+
yellow: '#ffff00',
|
|
150
|
+
yellowgreen: '#9acd32'
|
|
151
|
+
};
|
|
152
|
+
export const MAX_PALETTE = 12;
|
|
153
|
+
export const MIN_CONTRAST = 3;
|
|
154
|
+
export function parseColor(input) {
|
|
155
|
+
if (typeof input !== 'string') {
|
|
156
|
+
throw new RangeError(`A color is a name such as "blue" or a #hex such as "#1d4ed8", not ${typeof input}`);
|
|
157
|
+
}
|
|
158
|
+
const key = input
|
|
159
|
+
.trim()
|
|
160
|
+
.toLowerCase()
|
|
161
|
+
.replace(/[\s_-]+/g, '');
|
|
162
|
+
const digits = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/.exec(key)?.[1];
|
|
163
|
+
if (digits) {
|
|
164
|
+
return '#' + (digits.length === 3 ? digits.replace(/./g, (c) => c + c) : digits);
|
|
165
|
+
}
|
|
166
|
+
if (key in named) {
|
|
167
|
+
return named[key];
|
|
168
|
+
}
|
|
169
|
+
throw new RangeError(`Unknown color "${input}": use a name such as "blue" or a #hex such as "#1d4ed8"`);
|
|
170
|
+
}
|
|
171
|
+
const channel = (v) => {
|
|
172
|
+
const s = v / 255;
|
|
173
|
+
return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
174
|
+
};
|
|
175
|
+
const luminance = (hex) => [1, 3, 5].reduce((sum, i, k) => sum + channel(parseInt(hex.slice(i, i + 2), 16)) * [0.2126, 0.7152, 0.0722][k], 0);
|
|
176
|
+
export function contrastRatio(a, b) {
|
|
177
|
+
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
|
|
178
|
+
return (hi + 0.05) / (lo + 0.05);
|
|
179
|
+
}
|
|
180
|
+
export function readable(color, bg) {
|
|
181
|
+
const toward = luminance(bg) > 0.5 ? '#000000' : '#ffffff';
|
|
182
|
+
let out = color;
|
|
183
|
+
for (let f = 0.05; contrastRatio(out, bg) < MIN_CONTRAST && f <= 1; f += 0.05) {
|
|
184
|
+
out = mix(color, { to: toward, f });
|
|
185
|
+
}
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
export function withColors(t, { accent, palette }) {
|
|
189
|
+
if (accent === undefined && palette === undefined)
|
|
190
|
+
return t;
|
|
191
|
+
const fit = (c) => readable(parseColor(c), t.bg);
|
|
192
|
+
const lead = accent === undefined ? t.accent : fit(accent);
|
|
193
|
+
const rest = palette === undefined ? t.palette.slice(1) : listed(palette).map(fit);
|
|
194
|
+
const colors = [lead, ...cycled(rest, t.palette.length - 1)];
|
|
195
|
+
return { ...t, accent: lead, palette: colors };
|
|
196
|
+
}
|
|
197
|
+
function listed(palette) {
|
|
198
|
+
if (!Array.isArray(palette) || palette.length === 0 || palette.length > MAX_PALETTE) {
|
|
199
|
+
throw new RangeError(`A palette is a list of 1 to ${MAX_PALETTE} colors`);
|
|
200
|
+
}
|
|
201
|
+
return palette;
|
|
202
|
+
}
|
|
203
|
+
function cycled(list, min) {
|
|
204
|
+
return Array.from({ length: Math.max(min, list.length) }, (_, i) => list[i % list.length]);
|
|
205
|
+
}
|
package/dist-lib/lib/render.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { type Overrides } from './colors.ts';
|
|
1
2
|
import { type Mark } from './frame.ts';
|
|
2
3
|
import type { Chart } from './spec.ts';
|
|
3
4
|
import { type ThemeName } from './themes.ts';
|
|
4
|
-
export interface RenderOptions {
|
|
5
|
+
export interface RenderOptions extends Overrides {
|
|
5
6
|
theme: ThemeName;
|
|
6
7
|
mark?: Mark | undefined;
|
|
7
8
|
}
|
|
8
|
-
export declare function render(chart: Chart, { theme, mark }: RenderOptions): string;
|
|
9
|
+
export declare function render(chart: Chart, { theme, mark, accent, palette }: RenderOptions): string;
|
package/dist-lib/lib/render.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { charts } from './charts/index.js';
|
|
2
|
+
import { withColors } from './colors.js';
|
|
2
3
|
import { formatter } from './format.js';
|
|
3
4
|
import { frame, svg } from './frame.js';
|
|
4
5
|
import { fixed } from './svg.js';
|
|
5
6
|
import { themes } from './themes.js';
|
|
6
|
-
export function render(chart, { theme, mark }) {
|
|
7
|
-
const t = themes[theme];
|
|
7
|
+
export function render(chart, { theme, mark, accent, palette }) {
|
|
8
|
+
const t = withColors(themes[theme], { accent, palette });
|
|
8
9
|
const { out: head, box } = frame(chart, { t, mark });
|
|
9
10
|
const out = [];
|
|
10
11
|
charts[chart.type]({ out, spec: chart, t, b: box, fmt: formatter(chart) });
|
package/dist-lib/lib/themes.d.ts
CHANGED
|
@@ -25,4 +25,9 @@ export interface Ramp {
|
|
|
25
25
|
hi: number;
|
|
26
26
|
}
|
|
27
27
|
export declare function ramp(data: ReadonlyArray<Row>, t: Theme): Ramp;
|
|
28
|
+
export interface Blend {
|
|
29
|
+
to: string;
|
|
30
|
+
f: number;
|
|
31
|
+
}
|
|
32
|
+
export declare function mix(from: string, { to, f }: Blend): string;
|
|
28
33
|
export declare function contrast(fill: string, t: Theme): string;
|
package/dist-lib/lib/themes.js
CHANGED
|
@@ -54,7 +54,7 @@ export function ramp(data, t) {
|
|
|
54
54
|
const shade = (v) => mix(t.bg, { to: t.accent, f: 0.28 + 0.72 * (hi === lo ? 1 : (v - lo) / (hi - lo)) });
|
|
55
55
|
return { shade, lo, hi };
|
|
56
56
|
}
|
|
57
|
-
function mix(from, { to, f }) {
|
|
57
|
+
export function mix(from, { to, f }) {
|
|
58
58
|
const [x, y] = [from, to].map((h) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16)));
|
|
59
59
|
return ('#' +
|
|
60
60
|
x
|