Skip to content

Commit

Permalink
feat(gauge): 仪表盘添加tooltip 文档 单测 和demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-qing-hai committed Mar 22, 2022
1 parent e80e185 commit 4bb3170
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
75 changes: 75 additions & 0 deletions __tests__/unit/plots/gauge/tooltip-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Gauge } from '../../../../src';
import { createDiv } from '../../../utils/dom';

describe('gauge', () => {
it('default tooltip', () => {
const gauge = new Gauge(createDiv(), {
width: 400,
height: 300,
percent: 0.75,
});

gauge.render();
// @ts-ignore
expect(gauge.chart.options.tooltip).toBe(false);
expect(gauge.chart.getComponents().find((co) => co.type === 'tooltip')).toBe(undefined);

gauge.update({
tooltip: {},
});
// @ts-ignore
expect(gauge.chart.options.tooltip).toMatchObject({
showMarkers: false,
showTitle: false,
containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list"></div></div>',
domStyles: {
'g2-tooltip': {
padding: '4px 8px',
fontSize: '10px',
},
},
});

gauge.destroy();
});

it('custom tooltip', () => {
const customContent = (x, data) => {
return `${(Number(data?.[0]?.value || 0) * 100).toFixed(2)}%`;
};

const gauge = new Gauge(createDiv(), {
width: 400,
height: 300,
percent: 0.75,
tooltip: {
containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list">2131231</div></div>',
domStyles: {
'g2-tooltip': {
padding: '40px',
fontSize: '40px',
},
},
customContent,
},
});

gauge.render();

// @ts-ignore
expect(gauge.chart.options.tooltip).toMatchObject({
showMarkers: false,
showTitle: false,
containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list">2131231</div></div>',
domStyles: {
'g2-tooltip': {
padding: '40px',
fontSize: '40px',
},
},
customContent,
});

gauge.destroy();
});
});
4 changes: 4 additions & 0 deletions docs/api/plots/gauge.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ They all have the following configuration items:
Metric central text component.

`markdown:docs/common/statistic.en.md`

#### tooltip

`markdown:docs/common/tooltip.eh.md`
3 changes: 3 additions & 0 deletions docs/api/plots/gauge.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,6 @@ order: 5

`markdown:docs/common/statistic.zh.md`

#### tooltip

`markdown:docs/common/tooltip.zh.md`
8 changes: 8 additions & 0 deletions examples/progress-plots/gauge/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@
"en": "Custom simple gauge indicator"
},
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/2RRg9bSnn2/380b6b20-aed3-44e2-9afe-5532b1f9daea.png"
},
{
"filename": "tooltip-gauge.ts",
"title": {
"zh": "仪表盘 自定义 tooltip",
"en": "Gauge Customize React Tooltip"
},
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/xDdGhZK84X/9746c5d8-4341-4c04-9528-87bcab55eedb.png"
}
]
}
53 changes: 53 additions & 0 deletions examples/progress-plots/gauge/demo/tooltip-gauge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Gauge } from '@antv/g2plot';

const gauge = new Gauge('container', {
tooltip: {
containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list"></div></div>',
domStyles: {
'g2-tooltip': {
padding: '4px 8px',
fontSize: '10px',
},
},
customContent: (x, data) => {
return `${(Number(data?.[0]?.value || 0) * 100).toFixed(2)}%`;
},
},
percent: 0.75,
range: {
color: '#30BF78',
},
indicator: {
pointer: {
style: {
stroke: '#D0D0D0',
},
},
pin: {
style: {
stroke: '#D0D0D0',
},
},
},
axis: {
label: {
formatter(v) {
return Number(v) * 100;
},
},
subTickLine: {
count: 3,
},
},
statistic: {
content: {
formatter: ({ percent }) => `Rate: ${(percent * 100).toFixed(0)}%`,
style: {
color: 'rgba(0,0,0,0.65)',
fontSize: 48,
},
},
},
});

gauge.render();

0 comments on commit 4bb3170

Please sign in to comment.