@chartpng/render 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +165 -2
- package/SECURITY.md +34 -0
- package/THIRD_PARTY_NOTICES.md +253 -0
- package/bin/chartpng-render.js +2 -0
- package/dist/packages/render/cli.d.ts +1 -0
- package/dist/packages/render/cli.js +88 -0
- package/dist/packages/render/index.d.ts +61 -0
- package/dist/packages/render/index.js +79 -0
- package/dist/src/fonts/plex.d.ts +2 -0
- package/dist/src/fonts/plex.js +9 -0
- package/dist/src/lib/errors.d.ts +4 -0
- package/dist/src/lib/errors.js +9 -0
- package/dist/src/lib/maker.d.ts +90 -0
- package/dist/src/lib/maker.js +297 -0
- package/dist/src/lib/presets.d.ts +61 -0
- package/dist/src/lib/presets.js +576 -0
- package/dist/src/lib/render.d.ts +38 -0
- package/dist/src/lib/render.js +738 -0
- package/dist/src/lib/repair.d.ts +8 -0
- package/dist/src/lib/repair.js +598 -0
- package/dist/src/lib/style.d.ts +23 -0
- package/dist/src/lib/style.js +110 -0
- package/dist/src/lib/table-chart.d.ts +133 -0
- package/dist/src/lib/table-chart.js +624 -0
- package/dist/src/lib/theme.d.ts +37 -0
- package/dist/src/lib/theme.js +288 -0
- package/package.json +51 -3
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// theme=, palette= and preset= as request fields: the same settings as options.plugins.chartpng.theme,
|
|
2
|
+
// .palette and .preset, for callers that would rather not edit the config (a model writing a URL,
|
|
3
|
+
// a spreadsheet formula). The config wins: a setting it makes is never replaced by the request's.
|
|
4
|
+
// The output presets (email, slack) also decide the size, pixel ratio and background of the image.
|
|
5
|
+
import { readPresets } from './presets.js';
|
|
6
|
+
import { paletteFor, readPalette } from './theme.js';
|
|
7
|
+
// Defaults an output preset gives to what the request did not send. Conventions, not limits of
|
|
8
|
+
// the clients: email shows 600 px wide drawn at twice that (the layout of the charts-in-email guide),
|
|
9
|
+
// slack the 800 x 400 of the charts-in-slack guide's image block. Slack's block documents no size.
|
|
10
|
+
export const OUTPUT_DEFAULTS = {
|
|
11
|
+
email: { width: 600, height: 360, dpr: 2 },
|
|
12
|
+
slack: { width: 800, height: 400, dpr: 2 },
|
|
13
|
+
};
|
|
14
|
+
// The smallest type an output preset leaves on the image, in CSS pixels.
|
|
15
|
+
export const OUTPUT_MIN_FONT = 11;
|
|
16
|
+
const isObject = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
|
|
17
|
+
// The raw request values as text. A JSON body may send theme: false, and palette or preset as a list.
|
|
18
|
+
export function styleFields(raw) {
|
|
19
|
+
const fields = {};
|
|
20
|
+
for (const name of ['theme', 'palette', 'preset']) {
|
|
21
|
+
const value = raw[name];
|
|
22
|
+
if (value == null || value === '')
|
|
23
|
+
continue;
|
|
24
|
+
if (typeof value === 'string')
|
|
25
|
+
fields[name] = value;
|
|
26
|
+
else if (name === 'theme' && value === false)
|
|
27
|
+
fields.theme = 'false';
|
|
28
|
+
else
|
|
29
|
+
fields[name] = Array.isArray(value) && name !== 'theme' ? value.join(',') : JSON.stringify(value);
|
|
30
|
+
}
|
|
31
|
+
return fields;
|
|
32
|
+
}
|
|
33
|
+
// Refuses a theme, palette or preset that does not exist or is not well formed.
|
|
34
|
+
export function checkStyle(fields) {
|
|
35
|
+
if (fields.theme !== undefined && fields.theme !== 'false')
|
|
36
|
+
paletteFor(fields.theme, '#ffffff');
|
|
37
|
+
if (fields.palette !== undefined)
|
|
38
|
+
readPalette(fields.palette);
|
|
39
|
+
if (fields.preset !== undefined)
|
|
40
|
+
readPresets(fields.preset);
|
|
41
|
+
}
|
|
42
|
+
const settingsOf = (config) => {
|
|
43
|
+
const settings = isObject(config.options) && isObject(config.options.plugins) ? config.options.plugins.chartpng : undefined;
|
|
44
|
+
return isObject(settings) ? settings : {};
|
|
45
|
+
};
|
|
46
|
+
// The output preset in force: the config's, else the request's. A preset setting in the config
|
|
47
|
+
// that is not valid is left for the drawing to refuse, as it always has been.
|
|
48
|
+
export function outputPreset(config, fields) {
|
|
49
|
+
let own;
|
|
50
|
+
try {
|
|
51
|
+
own = readPresets(settingsOf(config).preset).output;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
return own ?? (fields.preset === undefined ? undefined : readPresets(fields.preset).output);
|
|
57
|
+
}
|
|
58
|
+
// The config with the request's settings filled in under options.plugins.chartpng wherever the
|
|
59
|
+
// config made none. A report preset and an output preset are separate settings: the config's
|
|
60
|
+
// kpi leaves the request's email in force. The config given is not changed.
|
|
61
|
+
export function withStyle(config, fields) {
|
|
62
|
+
if (fields.theme === undefined && fields.palette === undefined && fields.preset === undefined)
|
|
63
|
+
return config;
|
|
64
|
+
const options = isObject(config.options) ? config.options : {};
|
|
65
|
+
const plugins = isObject(options.plugins) ? options.plugins : {};
|
|
66
|
+
const settings = settingsOf(config);
|
|
67
|
+
const next = { ...settings };
|
|
68
|
+
if (fields.theme !== undefined && settings.theme == null)
|
|
69
|
+
next.theme = fields.theme === 'false' ? false : fields.theme;
|
|
70
|
+
if (fields.palette !== undefined && settings.palette == null)
|
|
71
|
+
next.palette = readPalette(fields.palette);
|
|
72
|
+
if (fields.preset !== undefined) {
|
|
73
|
+
const own = readPresets(settings.preset);
|
|
74
|
+
const asked = readPresets(fields.preset);
|
|
75
|
+
next.preset = [own.report ?? asked.report, own.output ?? asked.output].filter(Boolean).join(',');
|
|
76
|
+
}
|
|
77
|
+
return { ...config, options: { ...options, plugins: { ...plugins, chartpng: next } } };
|
|
78
|
+
}
|
|
79
|
+
// False for what draws with transparency: transparent, an empty value (the canvas ignores it),
|
|
80
|
+
// #rgba and #rrggbbaa with alpha below 1, and rgba(), hsla() or "/ alpha" forms with alpha below 1.
|
|
81
|
+
export function isOpaque(background) {
|
|
82
|
+
const bg = background.trim().toLowerCase();
|
|
83
|
+
if (bg === '' || bg === 'transparent')
|
|
84
|
+
return false;
|
|
85
|
+
if (/^#[0-9a-f]{4}$/.test(bg))
|
|
86
|
+
return bg[4] === 'f';
|
|
87
|
+
if (/^#[0-9a-f]{8}$/.test(bg))
|
|
88
|
+
return bg.slice(7) === 'ff';
|
|
89
|
+
// Three components, then the alpha: rgb(0,0,0) has no alpha, its last 0 is the blue channel.
|
|
90
|
+
const alpha = /^(?:rgb|hsl)a?\(\s*[^,\s/]+[\s,]+[^,\s/]+[\s,]+[^,\s/]+\s*[,/]\s*([0-9.]+)(%?)\s*\)$/.exec(bg);
|
|
91
|
+
return !alpha || Number(alpha[1]) / (alpha[2] ? 100 : 1) >= 1;
|
|
92
|
+
}
|
|
93
|
+
const isPlain = (v) => !!v && typeof v === 'object' && Object.getPrototypeOf(v) === Object.prototype;
|
|
94
|
+
// The options with every font size under the floor raised to it. A copy: the options given are
|
|
95
|
+
// not changed, and anything that is not a plain object or array is kept as it is.
|
|
96
|
+
export function floorFonts(value, min = OUTPUT_MIN_FONT) {
|
|
97
|
+
if (Array.isArray(value))
|
|
98
|
+
return value.map((item) => floorFonts(item, min));
|
|
99
|
+
if (!isPlain(value))
|
|
100
|
+
return value;
|
|
101
|
+
const out = {};
|
|
102
|
+
for (const [key, child] of Object.entries(value)) {
|
|
103
|
+
if (key === '__proto__')
|
|
104
|
+
continue; // Dropped: as an own key it would break Chart.js.
|
|
105
|
+
const size = isPlain(child) ? child.size : undefined;
|
|
106
|
+
const small = key === 'font' && (typeof size === 'number' || (typeof size === 'string' && size.trim() !== '')) && Number(size) < min;
|
|
107
|
+
out[key] = floorFonts(small ? { ...child, size: min } : child, min);
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { MakerError, MakerResult } from './maker.js';
|
|
2
|
+
export declare const TABLE_MAX_ROWS = 50;
|
|
3
|
+
export declare const TABLE_MAX_SERIES = 6;
|
|
4
|
+
export declare const TABLE_WIDTH = 800;
|
|
5
|
+
export declare const TABLE_HEIGHT = 450;
|
|
6
|
+
export declare const TABLE_DPR = 2;
|
|
7
|
+
export declare const TABLE_MAX_URL = 3500;
|
|
8
|
+
export declare const TABLE_MAX_CHARS = 200000;
|
|
9
|
+
export type DecimalChoice = 'auto' | 'dot' | 'comma';
|
|
10
|
+
export type Delimiter = 'tab' | 'semicolon' | 'comma' | 'none';
|
|
11
|
+
export interface ParseOptions {
|
|
12
|
+
decimal?: DecimalChoice;
|
|
13
|
+
transpose?: boolean;
|
|
14
|
+
header?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface TableIssue {
|
|
17
|
+
name: string;
|
|
18
|
+
row: number;
|
|
19
|
+
sample: string;
|
|
20
|
+
}
|
|
21
|
+
export interface TableSeries {
|
|
22
|
+
name: string;
|
|
23
|
+
values: (number | null)[];
|
|
24
|
+
}
|
|
25
|
+
export interface Table {
|
|
26
|
+
labels: string[];
|
|
27
|
+
series: TableSeries[];
|
|
28
|
+
}
|
|
29
|
+
export type ParseResult = {
|
|
30
|
+
ok: true;
|
|
31
|
+
table: Table;
|
|
32
|
+
notes: string[];
|
|
33
|
+
delimiter: Delimiter;
|
|
34
|
+
decimal: 'dot' | 'comma';
|
|
35
|
+
header: boolean;
|
|
36
|
+
skipped: TableIssue[];
|
|
37
|
+
gaps: TableIssue[];
|
|
38
|
+
} | {
|
|
39
|
+
ok: false;
|
|
40
|
+
error: string;
|
|
41
|
+
reason: TableRefusal;
|
|
42
|
+
notes: string[];
|
|
43
|
+
skipped?: TableIssue[];
|
|
44
|
+
};
|
|
45
|
+
export type TableRefusal = 'empty' | 'too_long' | 'no_numbers' | 'rows' | 'series';
|
|
46
|
+
export declare function detectDelimiter(text: string): Delimiter;
|
|
47
|
+
export declare function splitCells(text: string, delimiter: string | null): {
|
|
48
|
+
rows: string[][];
|
|
49
|
+
unterminated: boolean;
|
|
50
|
+
};
|
|
51
|
+
type Cell = {
|
|
52
|
+
kind: 'empty';
|
|
53
|
+
} | {
|
|
54
|
+
kind: 'gap';
|
|
55
|
+
} | {
|
|
56
|
+
kind: 'text';
|
|
57
|
+
} | {
|
|
58
|
+
kind: 'number';
|
|
59
|
+
value: number;
|
|
60
|
+
currency: boolean;
|
|
61
|
+
percent: boolean;
|
|
62
|
+
};
|
|
63
|
+
export declare function readCell(raw: string, mark: 'dot' | 'comma'): Cell;
|
|
64
|
+
export declare function parseTable(input: string, options?: ParseOptions): ParseResult;
|
|
65
|
+
export type TableChartType = 'bar' | 'horizontal' | 'line' | 'pie' | 'doughnut' | 'ranking';
|
|
66
|
+
export declare const TABLE_CHART_TYPES: {
|
|
67
|
+
id: TableChartType;
|
|
68
|
+
label: string;
|
|
69
|
+
}[];
|
|
70
|
+
export interface TableChartSettings {
|
|
71
|
+
type: TableChartType;
|
|
72
|
+
title: string;
|
|
73
|
+
}
|
|
74
|
+
export interface LongChart {
|
|
75
|
+
length: number;
|
|
76
|
+
path: string;
|
|
77
|
+
width: number;
|
|
78
|
+
height: number;
|
|
79
|
+
alt: string;
|
|
80
|
+
config: Record<string, unknown>;
|
|
81
|
+
}
|
|
82
|
+
export type TableChartResult = Extract<MakerResult, {
|
|
83
|
+
ok: true;
|
|
84
|
+
}> | {
|
|
85
|
+
ok: false;
|
|
86
|
+
errors: MakerError[];
|
|
87
|
+
long?: LongChart;
|
|
88
|
+
};
|
|
89
|
+
export declare function chartNotes(table: Table, type: TableChartType): string[];
|
|
90
|
+
export declare function tableChartUrl(table: Table, settings: TableChartSettings): TableChartResult;
|
|
91
|
+
export declare function tableSnippets(url: string, alt: string, width: number, height: number): {
|
|
92
|
+
markdown: string;
|
|
93
|
+
html: string;
|
|
94
|
+
sheets: string;
|
|
95
|
+
};
|
|
96
|
+
export declare const TABLE_EXAMPLE: string;
|
|
97
|
+
export declare const TABLE_EXAMPLE_SETTINGS: TableChartSettings;
|
|
98
|
+
export declare function tableExample(): Extract<TableChartResult, {
|
|
99
|
+
ok: true;
|
|
100
|
+
}>;
|
|
101
|
+
export type GridType = 'bar' | 'line' | 'pie';
|
|
102
|
+
export declare const GRID_TYPES: {
|
|
103
|
+
id: GridType;
|
|
104
|
+
label: string;
|
|
105
|
+
}[];
|
|
106
|
+
export declare const GRID_EXAMPLE = "Day\tSignups\nMon\t12\nTue\t19\nWed\t7\nThu\t15\nFri\t22";
|
|
107
|
+
export type GridResult = {
|
|
108
|
+
ok: true;
|
|
109
|
+
config: Record<string, unknown>;
|
|
110
|
+
rows: number;
|
|
111
|
+
series: number;
|
|
112
|
+
notes: string[];
|
|
113
|
+
} | {
|
|
114
|
+
ok: false;
|
|
115
|
+
error: string;
|
|
116
|
+
};
|
|
117
|
+
export declare function splitTable(text: string): string[][];
|
|
118
|
+
export declare function tableToConfig(text: string, type?: GridType): GridResult;
|
|
119
|
+
export type ChartUrlRead = {
|
|
120
|
+
kind: 'table';
|
|
121
|
+
text: string;
|
|
122
|
+
type: TableChartType;
|
|
123
|
+
title: string;
|
|
124
|
+
left: string[];
|
|
125
|
+
} | {
|
|
126
|
+
kind: 'sheet';
|
|
127
|
+
path: string;
|
|
128
|
+
} | {
|
|
129
|
+
kind: 'error';
|
|
130
|
+
message: string;
|
|
131
|
+
};
|
|
132
|
+
export declare function tableFromChartUrl(text: string, hosts: string[]): ChartUrlRead | null;
|
|
133
|
+
export {};
|