Skip to content

Commit

Permalink
feat: 使用 base geometry 绘制漏斗图
Browse files Browse the repository at this point in the history
  • Loading branch information
aiyin.lzy committed Nov 17, 2020
1 parent 9d565e6 commit 5e41207
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 31 deletions.
7 changes: 7 additions & 0 deletions examples/funnel/basic/demo/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const funnelPlot = new Funnel('container', {
xField: 'stage',
yField: 'number',
legend: false,
tooltip: {
formatter: (datum) => {
return { name: 'abc', value: '123' };
},
},
});

funnelPlot.render();

console.log(funnelPlot.chart);
14 changes: 9 additions & 5 deletions src/plots/funnel/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ function defaultOptions(params: Params<FunnelOptions>): Params<FunnelOptions> {
if (dynamicHeight) {
additionalOption = {
tooltip: {
itemTpl:
'<li class="g2-tooltip-list-item" data-index={index}>' +
'<span style="background-color:{color};" class="g2-tooltip-marker"></span>' +
`<span class="g2-tooltip-name">{${xField}}</span>` +
`<span class="g2-tooltip-value" style="display: inline-block; float: right; margin-left: 30px;">{${yField}}</span></li>`,
// itemTpl:
// '<li class="g2-tooltip-list-item" data-index={index}>' +
// '<span style="background-color:{color};" class="g2-tooltip-marker"></span>' +
// `<span class="g2-tooltip-name">{${xField}}</span>` +
// `<span class="g2-tooltip-value" style="display: inline-block; float: right; margin-left: 30px;">{${yField}}</span></li>`,
formatter: (datum) => {
console.log(datum);
return { name: 'abc', value: '123' };
},
},
};
} else if (compareField) {
Expand Down
30 changes: 17 additions & 13 deletions src/plots/funnel/geometries/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,25 @@ export function conversionTagComponent(
if (index <= 0) return;
const lineCoordinateOption = getLineCoordinate(obj, index, data);

const lineOption = deepAssign({}, lineCoordinateOption, {
top: true,
text: {
content: isFunction(formatter) ? formatter(obj, data) : formatter,
offsetX: conversionTag.offsetX,
offsetY: conversionTag.offsetY,
position: 'end',
autoRotate: false,
style: {
...conversionTag.style,
textAlign: 'start',
textBaseline: 'middle',
const lineOption = deepAssign(
{},
{
top: true,
text: {
content: isFunction(formatter) ? formatter(obj, data) : formatter,
offsetX: conversionTag.offsetX,
offsetY: conversionTag.offsetY,
position: 'end',
autoRotate: false,
style: {
...conversionTag.style,
textAlign: 'start',
textBaseline: 'middle',
},
},
},
});
lineCoordinateOption
);
chart.annotation().line(lineOption);
});
}
Expand Down
5 changes: 4 additions & 1 deletion src/plots/funnel/geometries/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ function label(params: Params<FunnelOptions>): Params<FunnelOptions> {
chart.once('beforepaint', () => {
chart.views.forEach((view, index) => {
const geometry = findGeometry(view, 'interval');
console.log(geometry);
geometryLabel(geometry)(
label
? deepAssign({}, params, {
chart: view,
options: {
label: {
offset: 10,
Expand Down Expand Up @@ -136,8 +138,9 @@ function conversionTag(params: Params<FunnelOptions>): Params<FunnelOptions> {
return {
start: [datumIndex - 0.5, data[0][yField] * datum[FUNNEL_PERCENT]],
end: [datumIndex - 0.5, data[0][yField] * (datum[FUNNEL_PERCENT] + 0.05)],
// @ts-ignore
text: {
content: undefined,
// content: undefined,
offsetX: conversionTag !== false ? ratio * conversionTag.offsetX : 0,
style: {
textAlign: viewIndex === 0 ? 'end' : 'start',
Expand Down
27 changes: 18 additions & 9 deletions src/plots/funnel/geometries/dynamic-height.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { LineOption } from '@antv/g2/lib/interface';
import { flow, findGeometry } from '../../../utils';
import { Params } from '../../../core/adaptor';
import { FUNNEL_PERCENT, FUNNEL_TOTAL_PERCENT, PLOYGON_X, PLOYGON_Y } from '../constant';
import { geometry as baseGeometry } from '../../../adaptor/geometries/base';
import { getTooltipMapping } from '../../../utils/tooltip';
import { Datum } from '../../../types/common';
import { FunnelOptions } from '../types';
import { geometryLabel, conversionTagComponent } from './common';
Expand Down Expand Up @@ -80,17 +82,24 @@ function field(params: Params<FunnelOptions>): Params<FunnelOptions> {
*/
function geometry(params: Params<FunnelOptions>): Params<FunnelOptions> {
const { chart, options } = params;
const { xField, yField, color } = options;
const { xField, yField, color, tooltip } = options;

const { fields, formatter } = getTooltipMapping(tooltip, [xField, yField, FUNNEL_PERCENT]);
// 绘制漏斗图
chart
.polygon()
.position(`${PLOYGON_X}*${PLOYGON_Y}`)
.color(xField, color)
.tooltip(`${xField}*${yField}`, (xFieldValue, yFieldValue) => ({
[xField]: xFieldValue,
[yField]: yFieldValue,
}));
baseGeometry({
chart,
options: {
type: 'polygon',
xField: PLOYGON_X,
yField: PLOYGON_Y,
colorField: xField,
tooltipFields: fields,
mapping: {
tooltip: formatter,
color,
},
},
});
return params;
}

Expand Down
11 changes: 8 additions & 3 deletions src/plots/funnel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ export class Funnel extends Plot<FunnelOptions> {
fill: '#fff',
fontSize: 12,
},
callback: (xField, yField) => ({
content: `${xField} ${yField}`,
}),
callback: (xField, yField, percent) => {
return {
content: `${xField} ${yField}`,
};
},
},
tooltip: {
showTitle: false,
showMarkers: false,
shared: false,
formatter: (datum) => {
return { name: 'abc', value: '123' };
},
},
conversionTag: {
offsetX: 10,
Expand Down

0 comments on commit 5e41207

Please sign in to comment.