vizmatic 0.1.18 → 0.1.19
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/dist/cli.js +62 -8
- package/dist/index.cjs +54 -5
- package/dist/index.d.cts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +53 -5
- package/docs/assets/examples/eval-dashboard_dark.png +0 -0
- package/docs/assets/examples/eval-dashboard_light.png +0 -0
- package/docs/assets/examples/rag-graph_dark.png +0 -0
- package/docs/assets/examples/rag-graph_light.png +0 -0
- package/docs/assets/examples/release-card_dark.png +0 -0
- package/docs/assets/examples/release-card_light.png +0 -0
- package/docs/assets/examples/spec-anatomy_dark.png +0 -0
- package/docs/assets/examples/spec-anatomy_light.png +0 -0
- package/docs/assets/examples/theme-background_dark.png +0 -0
- package/docs/assets/examples/theme-background_light.png +0 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -102,6 +102,7 @@ __export(index_exports, {
|
|
|
102
102
|
renderAnimatedGif: () => renderAnimatedGif,
|
|
103
103
|
renderToBuffer: () => renderToBuffer,
|
|
104
104
|
renderToPng: () => renderToPng,
|
|
105
|
+
renderToPngWithOutput: () => renderToPngWithOutput,
|
|
105
106
|
renderToSvg: () => renderToSvg,
|
|
106
107
|
styles: () => styles,
|
|
107
108
|
toneGradients: () => toneGradients,
|
|
@@ -633,6 +634,7 @@ function Panel({
|
|
|
633
634
|
width: "100%",
|
|
634
635
|
display: "flex",
|
|
635
636
|
flexDirection: "column",
|
|
637
|
+
gap,
|
|
636
638
|
...bodyStyle
|
|
637
639
|
}
|
|
638
640
|
}, children),
|
|
@@ -4048,16 +4050,16 @@ function BarChart({
|
|
|
4048
4050
|
yAxisLabel,
|
|
4049
4051
|
footer
|
|
4050
4052
|
}) {
|
|
4051
|
-
const margin = { top: 18, right:
|
|
4053
|
+
const margin = { top: 18, right: 38, bottom: 40, left: yAxisLabel ? 52 : 38 };
|
|
4052
4054
|
const plot = createPlotArea(width, height, margin);
|
|
4053
4055
|
const domain = chartDomain(data.map((item) => item.value), min, max);
|
|
4054
4056
|
const ticks = chartTicks(domain.min, domain.max, 4);
|
|
4055
|
-
const
|
|
4056
|
-
const barWidth = Math.max(
|
|
4057
|
+
const barSlot = plot.innerWidth / Math.max(data.length, 1);
|
|
4058
|
+
const barWidth = Math.max(8, Math.min(76, barSlot * 0.72));
|
|
4057
4059
|
const zeroY = yInPlot(0, domain, plot);
|
|
4058
4060
|
const baselineY = clamp(zeroY, plot.y, plot.bottom);
|
|
4059
4061
|
const barGeometries = data.map((item, index) => {
|
|
4060
|
-
const x = plot.x + index * (barWidth
|
|
4062
|
+
const x = plot.x + index * barSlot + (barSlot - barWidth) / 2;
|
|
4061
4063
|
const valueY = yInPlot(item.value, domain, plot);
|
|
4062
4064
|
const y = Math.min(valueY, baselineY);
|
|
4063
4065
|
const barHeight = Math.max(4, Math.abs(baselineY - valueY));
|
|
@@ -5172,10 +5174,35 @@ function sanitizeSvg(svg) {
|
|
|
5172
5174
|
}
|
|
5173
5175
|
var AUTOCROP_PADDING = 24;
|
|
5174
5176
|
var CROP_THRESHOLD_PERCENT = 10;
|
|
5177
|
+
function replaceSvgAttribute(attrs, name, value) {
|
|
5178
|
+
const pattern = new RegExp(`\\s${name}="[^"]*"`);
|
|
5179
|
+
if (pattern.test(attrs)) return attrs.replace(pattern, ` ${name}="${value}"`);
|
|
5180
|
+
return `${attrs} ${name}="${value}"`;
|
|
5181
|
+
}
|
|
5182
|
+
function cropSvgViewport(svg, region) {
|
|
5183
|
+
const x = Math.max(0, Math.floor(region.x));
|
|
5184
|
+
const y = Math.max(0, Math.floor(region.y));
|
|
5185
|
+
const width = Math.max(1, Math.ceil(region.width));
|
|
5186
|
+
const height = Math.max(1, Math.ceil(region.height));
|
|
5187
|
+
return svg.replace(/^<svg\b([^>]*)>/, (_match, attrs) => {
|
|
5188
|
+
let nextAttrs = attrs;
|
|
5189
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "width", String(width));
|
|
5190
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "height", String(height));
|
|
5191
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "viewBox", `${x} ${y} ${width} ${height}`);
|
|
5192
|
+
return `<svg${nextAttrs}>`;
|
|
5193
|
+
});
|
|
5194
|
+
}
|
|
5195
|
+
function cropSavesEnough(source, target) {
|
|
5196
|
+
if (source <= 0 || target >= source) return false;
|
|
5197
|
+
return (source - target) / source * 100 >= CROP_THRESHOLD_PERCENT;
|
|
5198
|
+
}
|
|
5175
5199
|
function resolveWatermark(options) {
|
|
5176
5200
|
return options.watermark ?? options.brand;
|
|
5177
5201
|
}
|
|
5178
5202
|
async function renderToPng(element, options, createFn, theme) {
|
|
5203
|
+
await renderToPngWithOutput(element, options, createFn, theme);
|
|
5204
|
+
}
|
|
5205
|
+
async function renderToPngWithOutput(element, options, createFn, theme) {
|
|
5179
5206
|
return withRenderContext(
|
|
5180
5207
|
{ background: options.background ?? "transparent" },
|
|
5181
5208
|
() => renderToPngInContext(element, options, createFn, theme)
|
|
@@ -5216,7 +5243,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5216
5243
|
`Canvas overflow detected (${options.width}\xD7${options.height}): ${overflow.message}`
|
|
5217
5244
|
);
|
|
5218
5245
|
}
|
|
5219
|
-
|
|
5246
|
+
let finalWidth = options.width;
|
|
5220
5247
|
let finalHeight = options.height;
|
|
5221
5248
|
let finalElement = element;
|
|
5222
5249
|
const heightSaved = options.height - bounds.height;
|
|
@@ -5259,6 +5286,26 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5259
5286
|
finalSvg = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
5260
5287
|
}
|
|
5261
5288
|
}
|
|
5289
|
+
if (options.crop !== false) {
|
|
5290
|
+
const viewportCheck = new Resvg(finalSvg, {
|
|
5291
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
5292
|
+
background: "rgba(0, 0, 0, 0)"
|
|
5293
|
+
}).render();
|
|
5294
|
+
const viewportPixels = new Uint8Array(viewportCheck.pixels.buffer);
|
|
5295
|
+
const viewportBg = detectBackgroundColor(viewportPixels);
|
|
5296
|
+
const viewportBounds = detectContentBounds(
|
|
5297
|
+
viewportPixels,
|
|
5298
|
+
finalWidth,
|
|
5299
|
+
viewportCheck.height,
|
|
5300
|
+
viewportBg,
|
|
5301
|
+
AUTOCROP_PADDING
|
|
5302
|
+
);
|
|
5303
|
+
if (cropSavesEnough(finalWidth, viewportBounds.width) || cropSavesEnough(viewportCheck.height, viewportBounds.height)) {
|
|
5304
|
+
finalSvg = cropSvgViewport(finalSvg, viewportBounds);
|
|
5305
|
+
finalWidth = viewportBounds.width;
|
|
5306
|
+
finalHeight = viewportBounds.height;
|
|
5307
|
+
}
|
|
5308
|
+
}
|
|
5262
5309
|
const resvg = new Resvg(finalSvg, {
|
|
5263
5310
|
fitTo: {
|
|
5264
5311
|
mode: "width",
|
|
@@ -5270,6 +5317,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5270
5317
|
const pngBuffer = pngData.asPng();
|
|
5271
5318
|
await mkdir(dirname(options.outputPath), { recursive: true });
|
|
5272
5319
|
await writeFile(options.outputPath, pngBuffer);
|
|
5320
|
+
return { width: finalWidth, height: finalHeight };
|
|
5273
5321
|
}
|
|
5274
5322
|
async function renderToBuffer(element, width, height, options = {}) {
|
|
5275
5323
|
return withRenderContext(
|
|
@@ -5976,12 +6024,12 @@ async function renderFrameToPng(mod, theme, options) {
|
|
|
5976
6024
|
let height = options.height;
|
|
5977
6025
|
for (let attempt = 0; attempt < AUTO_SIZE_ATTEMPTS; attempt += 1) {
|
|
5978
6026
|
try {
|
|
5979
|
-
await
|
|
6027
|
+
const output = await renderToPngWithOutput(frameElement(mod, theme), {
|
|
5980
6028
|
...options,
|
|
5981
6029
|
width,
|
|
5982
6030
|
height
|
|
5983
6031
|
}, mod.create, theme);
|
|
5984
|
-
return { width, height };
|
|
6032
|
+
return { width, height, outputWidth: output.width, outputHeight: output.height };
|
|
5985
6033
|
} catch (error) {
|
|
5986
6034
|
const edges = overflowEdges(error);
|
|
5987
6035
|
const next = edges ? nextAutoSize(width, height, mod.autoSize, edges) : void 0;
|
|
@@ -5990,7 +6038,7 @@ async function renderFrameToPng(mod, theme, options) {
|
|
|
5990
6038
|
height = next.height;
|
|
5991
6039
|
}
|
|
5992
6040
|
}
|
|
5993
|
-
return { width, height };
|
|
6041
|
+
return { width, height, outputWidth: width, outputHeight: height };
|
|
5994
6042
|
}
|
|
5995
6043
|
async function renderCommand(argv) {
|
|
5996
6044
|
const args = parseRenderArgs(argv);
|
|
@@ -6007,6 +6055,8 @@ async function renderCommand(argv) {
|
|
|
6007
6055
|
const moduleWatermark = await resolveWatermarkAssets(mod.watermark ?? mod.brand);
|
|
6008
6056
|
let renderWidth = mod.width;
|
|
6009
6057
|
let renderHeight = mod.height;
|
|
6058
|
+
let outputWidth = renderWidth;
|
|
6059
|
+
let outputHeight = renderHeight;
|
|
6010
6060
|
for (const theme of args.themes) {
|
|
6011
6061
|
const outputName = `${name}_${theme}.png`;
|
|
6012
6062
|
const outputPath = join2(args.outDir, outputName);
|
|
@@ -6021,6 +6071,8 @@ async function renderCommand(argv) {
|
|
|
6021
6071
|
});
|
|
6022
6072
|
renderWidth = rendered.width;
|
|
6023
6073
|
renderHeight = rendered.height;
|
|
6074
|
+
outputWidth = rendered.outputWidth;
|
|
6075
|
+
outputHeight = rendered.outputHeight;
|
|
6024
6076
|
outputs.push(outputName);
|
|
6025
6077
|
console.log(`rendered ${relative(process.cwd(), outputPath)}`);
|
|
6026
6078
|
}
|
|
@@ -6029,6 +6081,8 @@ async function renderCommand(argv) {
|
|
|
6029
6081
|
source: relative(process.cwd(), file),
|
|
6030
6082
|
width: renderWidth,
|
|
6031
6083
|
height: renderHeight,
|
|
6084
|
+
outputWidth,
|
|
6085
|
+
outputHeight,
|
|
6032
6086
|
outputs
|
|
6033
6087
|
});
|
|
6034
6088
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -115,6 +115,7 @@ __export(index_exports, {
|
|
|
115
115
|
renderAnimatedGif: () => renderAnimatedGif,
|
|
116
116
|
renderToBuffer: () => renderToBuffer,
|
|
117
117
|
renderToPng: () => renderToPng,
|
|
118
|
+
renderToPngWithOutput: () => renderToPngWithOutput,
|
|
118
119
|
renderToSvg: () => renderToSvg,
|
|
119
120
|
styles: () => styles,
|
|
120
121
|
toneGradients: () => toneGradients,
|
|
@@ -647,6 +648,7 @@ function Panel({
|
|
|
647
648
|
width: "100%",
|
|
648
649
|
display: "flex",
|
|
649
650
|
flexDirection: "column",
|
|
651
|
+
gap,
|
|
650
652
|
...bodyStyle
|
|
651
653
|
}
|
|
652
654
|
}, children),
|
|
@@ -4062,16 +4064,16 @@ function BarChart({
|
|
|
4062
4064
|
yAxisLabel,
|
|
4063
4065
|
footer
|
|
4064
4066
|
}) {
|
|
4065
|
-
const margin = { top: 18, right:
|
|
4067
|
+
const margin = { top: 18, right: 38, bottom: 40, left: yAxisLabel ? 52 : 38 };
|
|
4066
4068
|
const plot = createPlotArea(width, height, margin);
|
|
4067
4069
|
const domain = chartDomain(data.map((item) => item.value), min, max);
|
|
4068
4070
|
const ticks = chartTicks(domain.min, domain.max, 4);
|
|
4069
|
-
const
|
|
4070
|
-
const barWidth = Math.max(
|
|
4071
|
+
const barSlot = plot.innerWidth / Math.max(data.length, 1);
|
|
4072
|
+
const barWidth = Math.max(8, Math.min(76, barSlot * 0.72));
|
|
4071
4073
|
const zeroY = yInPlot(0, domain, plot);
|
|
4072
4074
|
const baselineY = clamp(zeroY, plot.y, plot.bottom);
|
|
4073
4075
|
const barGeometries = data.map((item, index) => {
|
|
4074
|
-
const x = plot.x + index * (barWidth
|
|
4076
|
+
const x = plot.x + index * barSlot + (barSlot - barWidth) / 2;
|
|
4075
4077
|
const valueY = yInPlot(item.value, domain, plot);
|
|
4076
4078
|
const y = Math.min(valueY, baselineY);
|
|
4077
4079
|
const barHeight = Math.max(4, Math.abs(baselineY - valueY));
|
|
@@ -5187,10 +5189,35 @@ function sanitizeSvg(svg) {
|
|
|
5187
5189
|
}
|
|
5188
5190
|
var AUTOCROP_PADDING = 24;
|
|
5189
5191
|
var CROP_THRESHOLD_PERCENT = 10;
|
|
5192
|
+
function replaceSvgAttribute(attrs, name, value) {
|
|
5193
|
+
const pattern = new RegExp(`\\s${name}="[^"]*"`);
|
|
5194
|
+
if (pattern.test(attrs)) return attrs.replace(pattern, ` ${name}="${value}"`);
|
|
5195
|
+
return `${attrs} ${name}="${value}"`;
|
|
5196
|
+
}
|
|
5197
|
+
function cropSvgViewport(svg, region) {
|
|
5198
|
+
const x = Math.max(0, Math.floor(region.x));
|
|
5199
|
+
const y = Math.max(0, Math.floor(region.y));
|
|
5200
|
+
const width = Math.max(1, Math.ceil(region.width));
|
|
5201
|
+
const height = Math.max(1, Math.ceil(region.height));
|
|
5202
|
+
return svg.replace(/^<svg\b([^>]*)>/, (_match, attrs) => {
|
|
5203
|
+
let nextAttrs = attrs;
|
|
5204
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "width", String(width));
|
|
5205
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "height", String(height));
|
|
5206
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "viewBox", `${x} ${y} ${width} ${height}`);
|
|
5207
|
+
return `<svg${nextAttrs}>`;
|
|
5208
|
+
});
|
|
5209
|
+
}
|
|
5210
|
+
function cropSavesEnough(source, target) {
|
|
5211
|
+
if (source <= 0 || target >= source) return false;
|
|
5212
|
+
return (source - target) / source * 100 >= CROP_THRESHOLD_PERCENT;
|
|
5213
|
+
}
|
|
5190
5214
|
function resolveWatermark(options) {
|
|
5191
5215
|
return options.watermark ?? options.brand;
|
|
5192
5216
|
}
|
|
5193
5217
|
async function renderToPng(element, options, createFn, theme) {
|
|
5218
|
+
await renderToPngWithOutput(element, options, createFn, theme);
|
|
5219
|
+
}
|
|
5220
|
+
async function renderToPngWithOutput(element, options, createFn, theme) {
|
|
5194
5221
|
return withRenderContext(
|
|
5195
5222
|
{ background: options.background ?? "transparent" },
|
|
5196
5223
|
() => renderToPngInContext(element, options, createFn, theme)
|
|
@@ -5231,7 +5258,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5231
5258
|
`Canvas overflow detected (${options.width}\xD7${options.height}): ${overflow.message}`
|
|
5232
5259
|
);
|
|
5233
5260
|
}
|
|
5234
|
-
|
|
5261
|
+
let finalWidth = options.width;
|
|
5235
5262
|
let finalHeight = options.height;
|
|
5236
5263
|
let finalElement = element;
|
|
5237
5264
|
const heightSaved = options.height - bounds.height;
|
|
@@ -5274,6 +5301,26 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5274
5301
|
finalSvg = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
5275
5302
|
}
|
|
5276
5303
|
}
|
|
5304
|
+
if (options.crop !== false) {
|
|
5305
|
+
const viewportCheck = new import_resvg_js.Resvg(finalSvg, {
|
|
5306
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
5307
|
+
background: "rgba(0, 0, 0, 0)"
|
|
5308
|
+
}).render();
|
|
5309
|
+
const viewportPixels = new Uint8Array(viewportCheck.pixels.buffer);
|
|
5310
|
+
const viewportBg = detectBackgroundColor(viewportPixels);
|
|
5311
|
+
const viewportBounds = detectContentBounds(
|
|
5312
|
+
viewportPixels,
|
|
5313
|
+
finalWidth,
|
|
5314
|
+
viewportCheck.height,
|
|
5315
|
+
viewportBg,
|
|
5316
|
+
AUTOCROP_PADDING
|
|
5317
|
+
);
|
|
5318
|
+
if (cropSavesEnough(finalWidth, viewportBounds.width) || cropSavesEnough(viewportCheck.height, viewportBounds.height)) {
|
|
5319
|
+
finalSvg = cropSvgViewport(finalSvg, viewportBounds);
|
|
5320
|
+
finalWidth = viewportBounds.width;
|
|
5321
|
+
finalHeight = viewportBounds.height;
|
|
5322
|
+
}
|
|
5323
|
+
}
|
|
5277
5324
|
const resvg = new import_resvg_js.Resvg(finalSvg, {
|
|
5278
5325
|
fitTo: {
|
|
5279
5326
|
mode: "width",
|
|
@@ -5285,6 +5332,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5285
5332
|
const pngBuffer = pngData.asPng();
|
|
5286
5333
|
await (0, import_promises.mkdir)((0, import_path.dirname)(options.outputPath), { recursive: true });
|
|
5287
5334
|
await (0, import_promises.writeFile)(options.outputPath, pngBuffer);
|
|
5335
|
+
return { width: finalWidth, height: finalHeight };
|
|
5288
5336
|
}
|
|
5289
5337
|
async function renderToBuffer(element, width, height, options = {}) {
|
|
5290
5338
|
return withRenderContext(
|
|
@@ -5577,6 +5625,7 @@ async function renderAnimatedGif(scenes, options) {
|
|
|
5577
5625
|
renderAnimatedGif,
|
|
5578
5626
|
renderToBuffer,
|
|
5579
5627
|
renderToPng,
|
|
5628
|
+
renderToPngWithOutput,
|
|
5580
5629
|
renderToSvg,
|
|
5581
5630
|
styles,
|
|
5582
5631
|
toneGradients,
|
package/dist/index.d.cts
CHANGED
|
@@ -1314,6 +1314,12 @@ interface RenderOptions {
|
|
|
1314
1314
|
/** Root background for Vizmatic primitives. Defaults to alpha-transparent PNG/SVG output. */
|
|
1315
1315
|
background?: RenderBackground;
|
|
1316
1316
|
}
|
|
1317
|
+
interface RenderOutput {
|
|
1318
|
+
/** Final logical output width before scale is applied. */
|
|
1319
|
+
width: number;
|
|
1320
|
+
/** Final logical output height before scale is applied. */
|
|
1321
|
+
height: number;
|
|
1322
|
+
}
|
|
1317
1323
|
/**
|
|
1318
1324
|
* Render a JSX element to a PNG file, with auto-crop and optional watermark.
|
|
1319
1325
|
*
|
|
@@ -1330,6 +1336,7 @@ interface RenderOptions {
|
|
|
1330
1336
|
* @param theme - Theme mode for re-rendering (if createFn provided)
|
|
1331
1337
|
*/
|
|
1332
1338
|
declare function renderToPng(element: ReactNode, options: RenderOptions, createFn?: (theme: 'dark' | 'light') => ReactNode, theme?: 'dark' | 'light'): Promise<void>;
|
|
1339
|
+
declare function renderToPngWithOutput(element: ReactNode, options: RenderOptions, createFn?: (theme: 'dark' | 'light') => ReactNode, theme?: 'dark' | 'light'): Promise<RenderOutput>;
|
|
1333
1340
|
/**
|
|
1334
1341
|
* Render a JSX element to a PNG buffer (useful for testing).
|
|
1335
1342
|
*/
|
|
@@ -1439,4 +1446,4 @@ interface OverflowResult {
|
|
|
1439
1446
|
*/
|
|
1440
1447
|
declare function detectOverflow(pixels: Uint8Array, width: number, height: number, bgColor: string, edgeTolerance?: number, minDensity?: number, alphaThreshold?: number): OverflowResult;
|
|
1441
1448
|
|
|
1442
|
-
export { type AnimatedScene, type AnimationOptions, Arrow, ArrowMarkerDef, AxisPlot, type AxisPlotPath, Badge, BadgePill, BarChart, type BarChartDatum, Box, CalloutCard, Canvas, Card, ChartFrame, type ChartLegendItem, type ChartMargins, type ChartValueFormat, CodeBlock, type CodeLineSpec, type ColorName, Column, Comparison, type ComparisonSideSpec, Connector, type CropRegion, DashedLine, DataTable, DetailList, DotPoint, EquationCard, Flow, FlowArrow, type FlowStageSpec, GradientChip, GraphDiagram, type GraphDiagramEdge, type GraphDiagramNode, Grid, Heatmap, type IllustrationBuilder, type IntervalDatum, IntervalPlot, KeyValueList, type KeyValueRow, LayeredNetwork, type LayeredNetworkLayer, Legend, LineChart, type LineChartSeries, MathText, Matrix, MetricCard, MiniBarChart, type MiniBarDatum, type OverflowResult, Panel, Pipeline, type PlotArea, ProgressList, ProgressRow, type ProgressRowSpec, QuadrantChart, type QuadrantRegion, type RenderBackground, type RenderOptions, Row, ScatterPlot, type ScatterPoint, Scene, Stack, StackedBar, type StackedBarSegment, type StatusKind, StatusList, StatusRow, type StatusRowSpec, StepCard, SvgFrame, SvgMathText, SvgPoint, TextLabel, type ThemeColors, type ThemeMode, Tile, TileGrid, type TileSpec, TiledMatrix, type TiledMatrixRegion, TitleBar, type ToneName, ToneStrip, type TransitionType, ValuePill, VectorArrow, VectorSegment, Watermark, type WatermarkElementProps, type WatermarkImageOptions, type WatermarkInput, type WatermarkOptions, type WatermarkPosition, WindowFrame, canvas, colors, createPlotArea, cropPixels, defineIllustration, detectBackgroundColor, detectContentBounds, detectOverflow, formatMathText, getColor, getFonts, getGradient, getStyles, getThemeColors, getToneColor, getToneGradient, gradients, heatColor, loadAdditionalAsset, normalizeWatermark, renderAnimatedGif, renderToBuffer, renderToPng, renderToSvg, styles, toneGradients, typography, wrapWithBrand, wrapWithWatermark };
|
|
1449
|
+
export { type AnimatedScene, type AnimationOptions, Arrow, ArrowMarkerDef, AxisPlot, type AxisPlotPath, Badge, BadgePill, BarChart, type BarChartDatum, Box, CalloutCard, Canvas, Card, ChartFrame, type ChartLegendItem, type ChartMargins, type ChartValueFormat, CodeBlock, type CodeLineSpec, type ColorName, Column, Comparison, type ComparisonSideSpec, Connector, type CropRegion, DashedLine, DataTable, DetailList, DotPoint, EquationCard, Flow, FlowArrow, type FlowStageSpec, GradientChip, GraphDiagram, type GraphDiagramEdge, type GraphDiagramNode, Grid, Heatmap, type IllustrationBuilder, type IntervalDatum, IntervalPlot, KeyValueList, type KeyValueRow, LayeredNetwork, type LayeredNetworkLayer, Legend, LineChart, type LineChartSeries, MathText, Matrix, MetricCard, MiniBarChart, type MiniBarDatum, type OverflowResult, Panel, Pipeline, type PlotArea, ProgressList, ProgressRow, type ProgressRowSpec, QuadrantChart, type QuadrantRegion, type RenderBackground, type RenderOptions, type RenderOutput, Row, ScatterPlot, type ScatterPoint, Scene, Stack, StackedBar, type StackedBarSegment, type StatusKind, StatusList, StatusRow, type StatusRowSpec, StepCard, SvgFrame, SvgMathText, SvgPoint, TextLabel, type ThemeColors, type ThemeMode, Tile, TileGrid, type TileSpec, TiledMatrix, type TiledMatrixRegion, TitleBar, type ToneName, ToneStrip, type TransitionType, ValuePill, VectorArrow, VectorSegment, Watermark, type WatermarkElementProps, type WatermarkImageOptions, type WatermarkInput, type WatermarkOptions, type WatermarkPosition, WindowFrame, canvas, colors, createPlotArea, cropPixels, defineIllustration, detectBackgroundColor, detectContentBounds, detectOverflow, formatMathText, getColor, getFonts, getGradient, getStyles, getThemeColors, getToneColor, getToneGradient, gradients, heatColor, loadAdditionalAsset, normalizeWatermark, renderAnimatedGif, renderToBuffer, renderToPng, renderToPngWithOutput, renderToSvg, styles, toneGradients, typography, wrapWithBrand, wrapWithWatermark };
|
package/dist/index.d.ts
CHANGED
|
@@ -1314,6 +1314,12 @@ interface RenderOptions {
|
|
|
1314
1314
|
/** Root background for Vizmatic primitives. Defaults to alpha-transparent PNG/SVG output. */
|
|
1315
1315
|
background?: RenderBackground;
|
|
1316
1316
|
}
|
|
1317
|
+
interface RenderOutput {
|
|
1318
|
+
/** Final logical output width before scale is applied. */
|
|
1319
|
+
width: number;
|
|
1320
|
+
/** Final logical output height before scale is applied. */
|
|
1321
|
+
height: number;
|
|
1322
|
+
}
|
|
1317
1323
|
/**
|
|
1318
1324
|
* Render a JSX element to a PNG file, with auto-crop and optional watermark.
|
|
1319
1325
|
*
|
|
@@ -1330,6 +1336,7 @@ interface RenderOptions {
|
|
|
1330
1336
|
* @param theme - Theme mode for re-rendering (if createFn provided)
|
|
1331
1337
|
*/
|
|
1332
1338
|
declare function renderToPng(element: ReactNode, options: RenderOptions, createFn?: (theme: 'dark' | 'light') => ReactNode, theme?: 'dark' | 'light'): Promise<void>;
|
|
1339
|
+
declare function renderToPngWithOutput(element: ReactNode, options: RenderOptions, createFn?: (theme: 'dark' | 'light') => ReactNode, theme?: 'dark' | 'light'): Promise<RenderOutput>;
|
|
1333
1340
|
/**
|
|
1334
1341
|
* Render a JSX element to a PNG buffer (useful for testing).
|
|
1335
1342
|
*/
|
|
@@ -1439,4 +1446,4 @@ interface OverflowResult {
|
|
|
1439
1446
|
*/
|
|
1440
1447
|
declare function detectOverflow(pixels: Uint8Array, width: number, height: number, bgColor: string, edgeTolerance?: number, minDensity?: number, alphaThreshold?: number): OverflowResult;
|
|
1441
1448
|
|
|
1442
|
-
export { type AnimatedScene, type AnimationOptions, Arrow, ArrowMarkerDef, AxisPlot, type AxisPlotPath, Badge, BadgePill, BarChart, type BarChartDatum, Box, CalloutCard, Canvas, Card, ChartFrame, type ChartLegendItem, type ChartMargins, type ChartValueFormat, CodeBlock, type CodeLineSpec, type ColorName, Column, Comparison, type ComparisonSideSpec, Connector, type CropRegion, DashedLine, DataTable, DetailList, DotPoint, EquationCard, Flow, FlowArrow, type FlowStageSpec, GradientChip, GraphDiagram, type GraphDiagramEdge, type GraphDiagramNode, Grid, Heatmap, type IllustrationBuilder, type IntervalDatum, IntervalPlot, KeyValueList, type KeyValueRow, LayeredNetwork, type LayeredNetworkLayer, Legend, LineChart, type LineChartSeries, MathText, Matrix, MetricCard, MiniBarChart, type MiniBarDatum, type OverflowResult, Panel, Pipeline, type PlotArea, ProgressList, ProgressRow, type ProgressRowSpec, QuadrantChart, type QuadrantRegion, type RenderBackground, type RenderOptions, Row, ScatterPlot, type ScatterPoint, Scene, Stack, StackedBar, type StackedBarSegment, type StatusKind, StatusList, StatusRow, type StatusRowSpec, StepCard, SvgFrame, SvgMathText, SvgPoint, TextLabel, type ThemeColors, type ThemeMode, Tile, TileGrid, type TileSpec, TiledMatrix, type TiledMatrixRegion, TitleBar, type ToneName, ToneStrip, type TransitionType, ValuePill, VectorArrow, VectorSegment, Watermark, type WatermarkElementProps, type WatermarkImageOptions, type WatermarkInput, type WatermarkOptions, type WatermarkPosition, WindowFrame, canvas, colors, createPlotArea, cropPixels, defineIllustration, detectBackgroundColor, detectContentBounds, detectOverflow, formatMathText, getColor, getFonts, getGradient, getStyles, getThemeColors, getToneColor, getToneGradient, gradients, heatColor, loadAdditionalAsset, normalizeWatermark, renderAnimatedGif, renderToBuffer, renderToPng, renderToSvg, styles, toneGradients, typography, wrapWithBrand, wrapWithWatermark };
|
|
1449
|
+
export { type AnimatedScene, type AnimationOptions, Arrow, ArrowMarkerDef, AxisPlot, type AxisPlotPath, Badge, BadgePill, BarChart, type BarChartDatum, Box, CalloutCard, Canvas, Card, ChartFrame, type ChartLegendItem, type ChartMargins, type ChartValueFormat, CodeBlock, type CodeLineSpec, type ColorName, Column, Comparison, type ComparisonSideSpec, Connector, type CropRegion, DashedLine, DataTable, DetailList, DotPoint, EquationCard, Flow, FlowArrow, type FlowStageSpec, GradientChip, GraphDiagram, type GraphDiagramEdge, type GraphDiagramNode, Grid, Heatmap, type IllustrationBuilder, type IntervalDatum, IntervalPlot, KeyValueList, type KeyValueRow, LayeredNetwork, type LayeredNetworkLayer, Legend, LineChart, type LineChartSeries, MathText, Matrix, MetricCard, MiniBarChart, type MiniBarDatum, type OverflowResult, Panel, Pipeline, type PlotArea, ProgressList, ProgressRow, type ProgressRowSpec, QuadrantChart, type QuadrantRegion, type RenderBackground, type RenderOptions, type RenderOutput, Row, ScatterPlot, type ScatterPoint, Scene, Stack, StackedBar, type StackedBarSegment, type StatusKind, StatusList, StatusRow, type StatusRowSpec, StepCard, SvgFrame, SvgMathText, SvgPoint, TextLabel, type ThemeColors, type ThemeMode, Tile, TileGrid, type TileSpec, TiledMatrix, type TiledMatrixRegion, TitleBar, type ToneName, ToneStrip, type TransitionType, ValuePill, VectorArrow, VectorSegment, Watermark, type WatermarkElementProps, type WatermarkImageOptions, type WatermarkInput, type WatermarkOptions, type WatermarkPosition, WindowFrame, canvas, colors, createPlotArea, cropPixels, defineIllustration, detectBackgroundColor, detectContentBounds, detectOverflow, formatMathText, getColor, getFonts, getGradient, getStyles, getThemeColors, getToneColor, getToneGradient, gradients, heatColor, loadAdditionalAsset, normalizeWatermark, renderAnimatedGif, renderToBuffer, renderToPng, renderToPngWithOutput, renderToSvg, styles, toneGradients, typography, wrapWithBrand, wrapWithWatermark };
|
package/dist/index.js
CHANGED
|
@@ -521,6 +521,7 @@ function Panel({
|
|
|
521
521
|
width: "100%",
|
|
522
522
|
display: "flex",
|
|
523
523
|
flexDirection: "column",
|
|
524
|
+
gap,
|
|
524
525
|
...bodyStyle
|
|
525
526
|
}
|
|
526
527
|
}, children),
|
|
@@ -3936,16 +3937,16 @@ function BarChart({
|
|
|
3936
3937
|
yAxisLabel,
|
|
3937
3938
|
footer
|
|
3938
3939
|
}) {
|
|
3939
|
-
const margin = { top: 18, right:
|
|
3940
|
+
const margin = { top: 18, right: 38, bottom: 40, left: yAxisLabel ? 52 : 38 };
|
|
3940
3941
|
const plot = createPlotArea(width, height, margin);
|
|
3941
3942
|
const domain = chartDomain(data.map((item) => item.value), min, max);
|
|
3942
3943
|
const ticks = chartTicks(domain.min, domain.max, 4);
|
|
3943
|
-
const
|
|
3944
|
-
const barWidth = Math.max(
|
|
3944
|
+
const barSlot = plot.innerWidth / Math.max(data.length, 1);
|
|
3945
|
+
const barWidth = Math.max(8, Math.min(76, barSlot * 0.72));
|
|
3945
3946
|
const zeroY = yInPlot(0, domain, plot);
|
|
3946
3947
|
const baselineY = clamp(zeroY, plot.y, plot.bottom);
|
|
3947
3948
|
const barGeometries = data.map((item, index) => {
|
|
3948
|
-
const x = plot.x + index * (barWidth
|
|
3949
|
+
const x = plot.x + index * barSlot + (barSlot - barWidth) / 2;
|
|
3949
3950
|
const valueY = yInPlot(item.value, domain, plot);
|
|
3950
3951
|
const y = Math.min(valueY, baselineY);
|
|
3951
3952
|
const barHeight = Math.max(4, Math.abs(baselineY - valueY));
|
|
@@ -5060,10 +5061,35 @@ function sanitizeSvg(svg) {
|
|
|
5060
5061
|
}
|
|
5061
5062
|
var AUTOCROP_PADDING = 24;
|
|
5062
5063
|
var CROP_THRESHOLD_PERCENT = 10;
|
|
5064
|
+
function replaceSvgAttribute(attrs, name, value) {
|
|
5065
|
+
const pattern = new RegExp(`\\s${name}="[^"]*"`);
|
|
5066
|
+
if (pattern.test(attrs)) return attrs.replace(pattern, ` ${name}="${value}"`);
|
|
5067
|
+
return `${attrs} ${name}="${value}"`;
|
|
5068
|
+
}
|
|
5069
|
+
function cropSvgViewport(svg, region) {
|
|
5070
|
+
const x = Math.max(0, Math.floor(region.x));
|
|
5071
|
+
const y = Math.max(0, Math.floor(region.y));
|
|
5072
|
+
const width = Math.max(1, Math.ceil(region.width));
|
|
5073
|
+
const height = Math.max(1, Math.ceil(region.height));
|
|
5074
|
+
return svg.replace(/^<svg\b([^>]*)>/, (_match, attrs) => {
|
|
5075
|
+
let nextAttrs = attrs;
|
|
5076
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "width", String(width));
|
|
5077
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "height", String(height));
|
|
5078
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "viewBox", `${x} ${y} ${width} ${height}`);
|
|
5079
|
+
return `<svg${nextAttrs}>`;
|
|
5080
|
+
});
|
|
5081
|
+
}
|
|
5082
|
+
function cropSavesEnough(source, target) {
|
|
5083
|
+
if (source <= 0 || target >= source) return false;
|
|
5084
|
+
return (source - target) / source * 100 >= CROP_THRESHOLD_PERCENT;
|
|
5085
|
+
}
|
|
5063
5086
|
function resolveWatermark(options) {
|
|
5064
5087
|
return options.watermark ?? options.brand;
|
|
5065
5088
|
}
|
|
5066
5089
|
async function renderToPng(element, options, createFn, theme) {
|
|
5090
|
+
await renderToPngWithOutput(element, options, createFn, theme);
|
|
5091
|
+
}
|
|
5092
|
+
async function renderToPngWithOutput(element, options, createFn, theme) {
|
|
5067
5093
|
return withRenderContext(
|
|
5068
5094
|
{ background: options.background ?? "transparent" },
|
|
5069
5095
|
() => renderToPngInContext(element, options, createFn, theme)
|
|
@@ -5104,7 +5130,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5104
5130
|
`Canvas overflow detected (${options.width}\xD7${options.height}): ${overflow.message}`
|
|
5105
5131
|
);
|
|
5106
5132
|
}
|
|
5107
|
-
|
|
5133
|
+
let finalWidth = options.width;
|
|
5108
5134
|
let finalHeight = options.height;
|
|
5109
5135
|
let finalElement = element;
|
|
5110
5136
|
const heightSaved = options.height - bounds.height;
|
|
@@ -5147,6 +5173,26 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5147
5173
|
finalSvg = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
5148
5174
|
}
|
|
5149
5175
|
}
|
|
5176
|
+
if (options.crop !== false) {
|
|
5177
|
+
const viewportCheck = new Resvg(finalSvg, {
|
|
5178
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
5179
|
+
background: "rgba(0, 0, 0, 0)"
|
|
5180
|
+
}).render();
|
|
5181
|
+
const viewportPixels = new Uint8Array(viewportCheck.pixels.buffer);
|
|
5182
|
+
const viewportBg = detectBackgroundColor(viewportPixels);
|
|
5183
|
+
const viewportBounds = detectContentBounds(
|
|
5184
|
+
viewportPixels,
|
|
5185
|
+
finalWidth,
|
|
5186
|
+
viewportCheck.height,
|
|
5187
|
+
viewportBg,
|
|
5188
|
+
AUTOCROP_PADDING
|
|
5189
|
+
);
|
|
5190
|
+
if (cropSavesEnough(finalWidth, viewportBounds.width) || cropSavesEnough(viewportCheck.height, viewportBounds.height)) {
|
|
5191
|
+
finalSvg = cropSvgViewport(finalSvg, viewportBounds);
|
|
5192
|
+
finalWidth = viewportBounds.width;
|
|
5193
|
+
finalHeight = viewportBounds.height;
|
|
5194
|
+
}
|
|
5195
|
+
}
|
|
5150
5196
|
const resvg = new Resvg(finalSvg, {
|
|
5151
5197
|
fitTo: {
|
|
5152
5198
|
mode: "width",
|
|
@@ -5158,6 +5204,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5158
5204
|
const pngBuffer = pngData.asPng();
|
|
5159
5205
|
await mkdir(dirname(options.outputPath), { recursive: true });
|
|
5160
5206
|
await writeFile(options.outputPath, pngBuffer);
|
|
5207
|
+
return { width: finalWidth, height: finalHeight };
|
|
5161
5208
|
}
|
|
5162
5209
|
async function renderToBuffer(element, width, height, options = {}) {
|
|
5163
5210
|
return withRenderContext(
|
|
@@ -5449,6 +5496,7 @@ export {
|
|
|
5449
5496
|
renderAnimatedGif,
|
|
5450
5497
|
renderToBuffer,
|
|
5451
5498
|
renderToPng,
|
|
5499
|
+
renderToPngWithOutput,
|
|
5452
5500
|
renderToSvg,
|
|
5453
5501
|
styles,
|
|
5454
5502
|
toneGradients,
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED