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
49 changes: 49 additions & 0 deletions __tests__/bugs/issue-1757-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Line } from '../../src';
import { createDiv } from '../utils/dom';

describe('#1757', () => {
const data = [
{ year: '1991', value: 3 },
{ year: '1992', value: 4 },
{ year: '1993', value: 3.5 },
{ year: '1994', value: 5 },
{ year: '1995', value: 4.9 },
{ year: '1996', value: 6 },
{ year: '1997', value: 7 },
{ year: '1998', value: 9 },
{ year: '1999', value: 13 },
];
it('line line.tooltip.formatter', () => {
const line = new Line(createDiv('formatter'), {
data,
height: 500,
width: 600,
xField: 'year',
yField: 'value',
label: {},
point: {
size: 5,
shape: 'diamond',
style: {
fill: 'white',
stroke: '#2593fc',
lineWidth: 2,
},
},
tooltip: {
formatter: () => '123',
},
});

line.render();
// 初始化,默认激活最后一条数据
const geometry = line.chart.geometries[0];
const elements = geometry.elements;
const bbox = elements[elements.length - 1].getBBox();
line.chart.showTooltip({ x: bbox.maxX, y: bbox.maxY });
expect(document.getElementsByClassName('g2-tooltip-title')[0]).not.toBeUndefined();
// @ts-ignore
expect(document.getElementsByClassName('g2-tooltip-title')[0].style['margin-top']).toBe('12px');
expect(line.chart.geometries[1].elements[0].container.getChildByIndex(0).attr('stroke')).toBe('#2593fc');
});
});
4 changes: 2 additions & 2 deletions __tests__/unit/plots/column/percent-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('column percent', () => {
const elements = geometry.elements;
const bbox = elements[elements.length - 1].getBBox();
column.chart.showTooltip({ x: bbox.maxX, y: bbox.maxY });
expect(document.getElementsByTagName('h4')[0].innerHTML).toBe('2100');
expect(document.getElementsByClassName('g2-tooltip-title')[0].innerHTML).toBe('2100');
const {
// @ts-ignore
labelOption: { cfg },
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('column percent', () => {
const elements = geometry.elements;
const bbox = elements[elements.length - 1].getBBox();
column.chart.showTooltip({ x: bbox.maxX, y: bbox.maxY });
expect(document.getElementsByTagName('h4')[1]).toBeUndefined();
expect(document.getElementsByClassName('g2-tooltip-title')[1]).toBeUndefined();
expect(document.getElementsByClassName('tooltip-class')[0].innerHTML).toBe('123');
});
});
6 changes: 3 additions & 3 deletions src/utils/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function getTooltipTemplate(params: {
const { value, items, formatter, isPercent } = params;
const container = document.createElement('div');
container.className = 'g2-tooltip';
const title = `<h4>${value}</h4>`;
const title = `<div class="g2-tooltip-title" style="margin-top: 12px;margin-bottom: 12px;">${value}</div>`;
let listItem = '';
const formatterItem = (item) => {
if (formatter) {
Expand All @@ -53,8 +53,8 @@ export function getTooltipTemplate(params: {
return item?.value;
};
items.forEach((item) => {
listItem += `<li class="g2-tooltip-list-item" data-index={index} style="margin-bottom:4px;display:flex;">
<span style="background-color:${item.mappingData?.color};" class="g2-tooltip-marker"></span>
listItem += `<li class="g2-tooltip-list-item" data-index={index} style="margin-bottom:4px;display:flex;align-items: center;">
<span style="background-color:${item.mappingData?.color || item.color};" class="g2-tooltip-marker"></span>
<span style="display:inline-flex;flex:1;justify-content:space-between">
<span style="margin-right: 16px;">${item.name}:</span><span>${formatterItem(item)}</span>
</span>
Expand Down