@opendata-ai/openchart-engine 7.2.2 → 7.2.3
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/index.js +31 -17
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +6 -0
- package/src/annotations/__tests__/compute.test.ts +19 -0
- package/src/annotations/resolve-refline.ts +6 -1
- package/src/layout/dimensions.ts +11 -3
- package/src/layout/metrics.ts +34 -20
package/dist/index.js
CHANGED
|
@@ -900,7 +900,12 @@ function resolveRefLineAnnotation(annotation, scales, chartArea, isDark) {
|
|
|
900
900
|
}
|
|
901
901
|
const labelDelta = applyOffset({ dx: baseDx, dy: baseDy }, annotation.labelOffset);
|
|
902
902
|
const defaultStroke2 = isDark ? DARK_REFLINE_STROKE : LIGHT_REFLINE_STROKE;
|
|
903
|
-
const style = makeAnnotationLabelStyle(
|
|
903
|
+
const style = makeAnnotationLabelStyle(
|
|
904
|
+
annotation.fontSize ?? 11,
|
|
905
|
+
annotation.fontWeight ?? 400,
|
|
906
|
+
annotation.stroke ?? defaultStroke2,
|
|
907
|
+
isDark
|
|
908
|
+
);
|
|
904
909
|
style.textAnchor = textAnchor;
|
|
905
910
|
label = {
|
|
906
911
|
text: annotation.label,
|
|
@@ -10304,19 +10309,23 @@ function measureLegendWrap(entries, maxWidth, labelStyle, maxRows, entryGap = EN
|
|
|
10304
10309
|
|
|
10305
10310
|
// src/layout/metrics.ts
|
|
10306
10311
|
import { estimateTextWidth as estimateTextWidth14 } from "@opendata-ai/openchart-core";
|
|
10307
|
-
var
|
|
10308
|
-
var
|
|
10312
|
+
var DEFAULT_LABEL_FONT_SIZE = 10;
|
|
10313
|
+
var DEFAULT_VALUE_FONT_SIZE = 22;
|
|
10309
10314
|
var LABEL_LINE_HEIGHT_RATIO = 1.4;
|
|
10310
10315
|
var VALUE_LINE_HEIGHT_RATIO = 1.15;
|
|
10311
10316
|
var INTER_ROW_GAP = 4;
|
|
10312
10317
|
var TOP_GAP = 16;
|
|
10313
10318
|
var BOTTOM_GAP = 20;
|
|
10319
|
+
var DEFAULT_METRIC_FONT_SIZES = {
|
|
10320
|
+
label: DEFAULT_LABEL_FONT_SIZE,
|
|
10321
|
+
value: DEFAULT_VALUE_FONT_SIZE
|
|
10322
|
+
};
|
|
10314
10323
|
var MIN_BAR_WIDTH2 = 480;
|
|
10315
10324
|
var MIN_CHART_HEIGHT = 150;
|
|
10316
10325
|
var CELL_INNER_PAD = 8;
|
|
10317
|
-
function metricBarHeight() {
|
|
10318
|
-
const labelLine =
|
|
10319
|
-
const valueLine =
|
|
10326
|
+
function metricBarHeight(fonts = DEFAULT_METRIC_FONT_SIZES) {
|
|
10327
|
+
const labelLine = fonts.label * LABEL_LINE_HEIGHT_RATIO;
|
|
10328
|
+
const valueLine = fonts.value * VALUE_LINE_HEIGHT_RATIO;
|
|
10320
10329
|
return TOP_GAP + labelLine + INTER_ROW_GAP + valueLine + BOTTOM_GAP;
|
|
10321
10330
|
}
|
|
10322
10331
|
function valueRunText(metric) {
|
|
@@ -10325,19 +10334,19 @@ function valueRunText(metric) {
|
|
|
10325
10334
|
if (metric.secondary) parts.push(metric.secondary);
|
|
10326
10335
|
return parts.join(" ");
|
|
10327
10336
|
}
|
|
10328
|
-
function computeMetricBar(metrics, metricsTopY, metricsArea, remainingChartHeight, measureText) {
|
|
10337
|
+
function computeMetricBar(metrics, metricsTopY, metricsArea, remainingChartHeight, measureText, fonts = DEFAULT_METRIC_FONT_SIZES) {
|
|
10329
10338
|
if (!metrics || metrics.length === 0) return void 0;
|
|
10330
10339
|
if (metricsArea.width < MIN_BAR_WIDTH2) return void 0;
|
|
10331
10340
|
if (remainingChartHeight < MIN_CHART_HEIGHT) return void 0;
|
|
10332
10341
|
const cellWidth = metricsArea.width / metrics.length;
|
|
10333
10342
|
for (const metric of metrics) {
|
|
10334
10343
|
const text = valueRunText(metric);
|
|
10335
|
-
const measured = measureText ? measureText(text,
|
|
10344
|
+
const measured = measureText ? measureText(text, fonts.value, 510).width : estimateTextWidth14(text, fonts.value, 510);
|
|
10336
10345
|
if (measured > cellWidth - CELL_INNER_PAD) return void 0;
|
|
10337
10346
|
}
|
|
10338
|
-
const labelLine =
|
|
10339
|
-
const labelY = metricsTopY + TOP_GAP +
|
|
10340
|
-
const valueY = metricsTopY + TOP_GAP + labelLine + INTER_ROW_GAP +
|
|
10347
|
+
const labelLine = fonts.label * LABEL_LINE_HEIGHT_RATIO;
|
|
10348
|
+
const labelY = metricsTopY + TOP_GAP + fonts.label;
|
|
10349
|
+
const valueY = metricsTopY + TOP_GAP + labelLine + INTER_ROW_GAP + fonts.value;
|
|
10341
10350
|
const cells = metrics.map((metric, i) => ({
|
|
10342
10351
|
x: metricsArea.x + i * cellWidth,
|
|
10343
10352
|
cellWidth,
|
|
@@ -10348,12 +10357,15 @@ function computeMetricBar(metrics, metricsTopY, metricsArea, remainingChartHeigh
|
|
|
10348
10357
|
}));
|
|
10349
10358
|
return {
|
|
10350
10359
|
y: metricsTopY,
|
|
10351
|
-
height: metricBarHeight(),
|
|
10360
|
+
height: metricBarHeight(fonts),
|
|
10352
10361
|
cells
|
|
10353
10362
|
};
|
|
10354
10363
|
}
|
|
10355
10364
|
|
|
10356
10365
|
// src/layout/dimensions.ts
|
|
10366
|
+
function metricFonts(theme) {
|
|
10367
|
+
return { label: theme.fonts.sizes.metricLabel, value: theme.fonts.sizes.metricValue };
|
|
10368
|
+
}
|
|
10357
10369
|
function chromeToInput(chrome) {
|
|
10358
10370
|
return {
|
|
10359
10371
|
eyebrow: chrome.eyebrow,
|
|
@@ -10485,7 +10497,7 @@ function computeDimensions(spec, options, legendLayout, theme, strategy, waterma
|
|
|
10485
10497
|
const topAxisGap = isRadial && chrome.topHeight === 0 ? 0 : axisMargin + inlineTickOverhang;
|
|
10486
10498
|
const topPad = width < NARROW_VIEWPORT_MAX ? padding + TOP_PAD_EXTRA_NARROW : padding;
|
|
10487
10499
|
const wantsMetrics = !!spec.metrics && spec.metrics.length > 0 && chromeMode !== "hidden";
|
|
10488
|
-
const tentativeMetricsHeight = wantsMetrics ? metricBarHeight() : 0;
|
|
10500
|
+
const tentativeMetricsHeight = wantsMetrics ? metricBarHeight(metricFonts(theme)) : 0;
|
|
10489
10501
|
const margins = {
|
|
10490
10502
|
top: topPad + chrome.topHeight + tentativeMetricsHeight,
|
|
10491
10503
|
right: hPad + (isRadial ? hPad : axisMargin),
|
|
@@ -10719,7 +10731,8 @@ function computeDimensions(spec, options, legendLayout, theme, strategy, waterma
|
|
|
10719
10731
|
fallbackMetricsTopY,
|
|
10720
10732
|
fallbackMetricsArea,
|
|
10721
10733
|
chartArea.height,
|
|
10722
|
-
options.measureText
|
|
10734
|
+
options.measureText,
|
|
10735
|
+
theme
|
|
10723
10736
|
) : void 0;
|
|
10724
10737
|
if (wantsMetrics && !fallbackMetrics) {
|
|
10725
10738
|
margins.top = Math.max(0, margins.top - tentativeMetricsHeight);
|
|
@@ -10742,7 +10755,7 @@ function computeDimensions(spec, options, legendLayout, theme, strategy, waterma
|
|
|
10742
10755
|
}
|
|
10743
10756
|
const metricsTopY = topPad + chrome.topHeight;
|
|
10744
10757
|
const metricsArea = { x: hPad, width: Math.max(0, width - hPad * 2) };
|
|
10745
|
-
const resolvedMetrics = wantsMetrics ? resolveMetrics(spec, metricsTopY, metricsArea, chartArea.height, options.measureText) : void 0;
|
|
10758
|
+
const resolvedMetrics = wantsMetrics ? resolveMetrics(spec, metricsTopY, metricsArea, chartArea.height, options.measureText, theme) : void 0;
|
|
10746
10759
|
if (wantsMetrics && !resolvedMetrics) {
|
|
10747
10760
|
margins.top = Math.max(0, margins.top - tentativeMetricsHeight);
|
|
10748
10761
|
chartArea = {
|
|
@@ -10761,13 +10774,14 @@ function computeDimensions(spec, options, legendLayout, theme, strategy, waterma
|
|
|
10761
10774
|
xAxisHeight
|
|
10762
10775
|
};
|
|
10763
10776
|
}
|
|
10764
|
-
function resolveMetrics(spec, metricsTopY, metricsArea, remainingChartHeight, measureText) {
|
|
10777
|
+
function resolveMetrics(spec, metricsTopY, metricsArea, remainingChartHeight, measureText, theme) {
|
|
10765
10778
|
return computeMetricBar(
|
|
10766
10779
|
spec.metrics,
|
|
10767
10780
|
metricsTopY,
|
|
10768
10781
|
metricsArea,
|
|
10769
10782
|
remainingChartHeight,
|
|
10770
|
-
measureText
|
|
10783
|
+
measureText,
|
|
10784
|
+
metricFonts(theme)
|
|
10771
10785
|
);
|
|
10772
10786
|
}
|
|
10773
10787
|
|