Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/safe-tooltips-stay-contained.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@datafe-open/markdown-chart-echarts": patch
---

Force ECharts tooltip containment and rich-text rendering safety settings after
applying model-provided chart options.
57 changes: 41 additions & 16 deletions packages/echarts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,19 @@ function styleComponent(
return value === undefined || value === null ? cloneJson(defaults) : value;
}

function forceTooltipSafety(value: JsonValue): JsonValue {
const force = (entry: JsonValue): JsonValue => isJsonObject(entry)
? {
...entry,
appendToBody: false,
confine: true,
enterable: false,
renderMode: 'richText',
}
: entry;
return Array.isArray(value) ? value.map(force) : force(value);
}

function hasOwnPosition(
value: Record<string, JsonValue>,
position: 'top' | 'bottom',
Expand Down Expand Up @@ -818,7 +831,16 @@ function styleSeriesEntry(
itemStyle: { borderColor: tokens.seriesBorder, borderWidth: 2 },
});
}
return mergeObjectDefaults(defaults, series);
const styled = mergeObjectDefaults(defaults, series);
if (Object.prototype.hasOwnProperty.call(series, 'tooltip')) {
styled.tooltip = forceTooltipSafety(styleComponent(series.tooltip, {
appendToBody: false,
confine: true,
enterable: false,
renderMode: 'richText',
}));
}
return styled;
}

const ITEM_ONLY_SERIES_TYPES = new Set(['pie', 'funnel', 'gauge', 'radar', 'treemap']);
Expand Down Expand Up @@ -889,21 +911,24 @@ export function applyEChartsDefaultStyle(
subtextStyle: { color: tokens.subtext },
});
}
styled.tooltip = styleComponent(styled.tooltip, {
trigger: defaultTooltipTrigger(option),
confine: true,
enterable: false,
renderMode: 'richText',
backgroundColor: tokens.tooltipBg,
borderColor: tokens.splitLine,
borderWidth: 1,
padding: [8, 10],
textStyle: { color: tokens.titleText, fontSize: 12 },
axisPointer: {
lineStyle: { color: tokens.pointer, width: 1 },
crossStyle: { color: tokens.pointer, width: 1 },
},
});
styled.tooltip = forceTooltipSafety(
styleComponent(styled.tooltip, {
trigger: defaultTooltipTrigger(option),
appendToBody: false,
confine: true,
enterable: false,
renderMode: 'richText',
backgroundColor: tokens.tooltipBg,
borderColor: tokens.splitLine,
borderWidth: 1,
padding: [8, 10],
textStyle: { color: tokens.titleText, fontSize: 12 },
axisPointer: {
lineStyle: { color: tokens.pointer, width: 1 },
crossStyle: { color: tokens.pointer, width: 1 },
},
}),
);
if (shouldApplyDefaultLegend(option)) {
styled.legend = styleVerticallyPositionedComponent(styled.legend, {
type: 'scroll',
Expand Down
39 changes: 39 additions & 0 deletions packages/echarts/test/echarts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,45 @@ describe('createEChartsRenderer', () => {
});
});

it('forces tooltip safety invariants after applying explicit values', () => {
const styled = applyEChartsDefaultStyle({
tooltip: {
appendToBody: true,
confine: false,
enterable: true,
renderMode: 'html',
trigger: 'item',
},
series: [{
type: 'bar',
tooltip: {
appendToBody: true,
confine: false,
enterable: true,
renderMode: 'html',
trigger: 'axis',
},
}],
});

expect(styled.tooltip).toMatchObject({
appendToBody: false,
confine: true,
enterable: false,
renderMode: 'richText',
trigger: 'item',
});
expect(styled.series).toMatchObject([{
tooltip: {
appendToBody: false,
confine: true,
enterable: false,
renderMode: 'richText',
trigger: 'axis',
},
}]);
});

it.each(['pie', 'funnel'] as const)(
'keeps the default item legend for a single %s series',
(type) => {
Expand Down