Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add venn plots #2179

Merged
merged 2 commits into from
Nov 20, 2023
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
5 changes: 5 additions & 0 deletions packages/plots/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import WordCloud from './wordCloud';
import Treemap from './treemap';
import Violin from './violin';
import BidirectionalBar from './bidirectional-bar';
import Venn from './venn';


export type { AreaConfig } from './area';
export type { BarConfig } from './bar';
Expand All @@ -46,6 +48,8 @@ export type { WordCloudConfig } from './wordCloud';
export type { TreemapConfig } from './treemap';
export type { ViolinConfig } from './violin';
export type { BidirectionalBarConfig } from './bidirectional-bar';
export type { VennConfig } from './venn';


export {
Base,
Expand All @@ -72,4 +76,5 @@ export {
Treemap,
Violin,
BidirectionalBar,
Venn
};
10 changes: 10 additions & 0 deletions packages/plots/src/components/venn/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { VennOptions } from '../../core';
import { CommonConfig } from '../../interface';
import { BaseChart } from '../base';

export type VennConfig = CommonConfig<VennOptions>;

const VennChart = (props: VennConfig) => <BaseChart {...props} chartType="Venn" />;

export default VennChart;
4 changes: 4 additions & 0 deletions packages/plots/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export type { WordCloudOptions } from './plots/wordCloud';
export type { TreemapOptions } from './plots/treemap';
export type { ViolinOptions } from './plots/violin';
export type { BidirectionalBarOptions } from './plots/bidirectional-bar';
export type { VennOptions } from './plots/venn';

export * from './types';

import { Base } from './plots/base';
Expand Down Expand Up @@ -56,6 +58,7 @@ import { WordCloud } from './plots/wordCloud';
import { Treemap } from './plots/treemap';
import { Violin } from './plots/violin';
import { BidirectionalBar } from './plots/bidirectional-bar';
import { Venn } from './plots/venn';

export const Plots = {
Base,
Expand Down Expand Up @@ -86,4 +89,5 @@ export const Plots = {
Treemap,
Violin,
BidirectionalBar,
Venn,
};
39 changes: 39 additions & 0 deletions packages/plots/src/core/plots/venn/adaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { flow, isArray, omit, remove, set, transformOptions } from '../../utils';
import type { Adaptor } from '../../types';
import { DefaultTransformKey, type VennOptions } from './type';

type Params = Adaptor<VennOptions>;

/**
* @param chart
* @param options
*/
export function adaptor(params: Params) {
/**
* 图表差异化处理
*/
const init = (params: Params) => {
const { options } = params;
const { data, setsField, sizeField } = options;
if (isArray(data)) {
set(options, 'data', {
type: 'inline',
value: data,
transform: [
{
type: 'venn',
sets: setsField,
size: sizeField,
as: [DefaultTransformKey.color, DefaultTransformKey.d],
},
],
});
set(options, 'colorField', setsField);
set(options, ['children', '0', 'encode', 'd'], DefaultTransformKey.d);
}
set(params, 'options', omit(options, ['sizeField', 'setsField']));
return params;
};

return flow(init, transformOptions)(params);
}
40 changes: 40 additions & 0 deletions packages/plots/src/core/plots/venn/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Plot } from '../../base';
import type { Adaptor } from '../../types';
import { adaptor } from './adaptor';
import { DefaultTransformKey, VennOptions } from './type';

export type { VennOptions };

export class Venn extends Plot<VennOptions> {
/** 图表类型 */
public type = 'venn';

/**
* 获取 韦恩图 默认配置项
* 供外部使用
*/
static getDefaultOptions(): Partial<VennOptions> {
return {
type: 'view',
children: [{ type: 'path' }],
legend: {
color: { itemMarker: 'circle' },
},
encode: { color: DefaultTransformKey.color, d: DefaultTransformKey.d },
};
}

/**
* 获取 韦恩图 默认配置
*/
protected getDefaultOptions() {
return Venn.getDefaultOptions();
}

/**
* 韦恩图适配器
*/
protected getSchemaAdaptor(): (params: Adaptor<VennOptions>) => void {
return adaptor;
}
}
8 changes: 8 additions & 0 deletions packages/plots/src/core/plots/venn/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { BaseOptions, Options } from '../../types/common';

export type VennOptions = Options & BaseOptions;

export enum DefaultTransformKey {
color = 'key',
d = 'path',
}
35 changes: 35 additions & 0 deletions site/examples/statistics/venn/demo/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Venn } from '@ant-design/plots';
import React from 'react';
import ReactDOM from 'react-dom';

const DemoVenn = () => {
const config = {
data: [
{ sets: ['A'], size: 12, label: 'A' },
{ sets: ['B'], size: 12, label: 'B' },
{ sets: ['C'], size: 12, label: 'C' },
{ sets: ['A', 'B'], size: 2, label: 'A&B' },
{ sets: ['A', 'C'], size: 2, label: 'A&C' },
{ sets: ['B', 'C'], size: 2, label: 'B&C' },
{ sets: ['A', 'B', 'C'], size: 1 },
],
setsField: 'sets',
sizeField: 'size',
style: { fillOpacity: 0.85 },
label: {
position: 'inside',
text: (d) => d.label || '',
},
tooltip: {
title: false,
items: [
(d) => {
return { name: d.key, value: d.size };
},
],
},
};
return <Venn {...config} />;
};

ReactDOM.render(<DemoVenn />, document.getElementById('container'));
42 changes: 42 additions & 0 deletions site/examples/statistics/venn/demo/custom-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Venn } from '@ant-design/plots';
import React from 'react';
import ReactDOM from 'react-dom';

const DemoVenn = () => {
const config = {
data: [
{ sets: ['A'], size: 12, label: 'A' },
{ sets: ['B'], size: 12, label: 'B' },
{ sets: ['C'], size: 12, label: 'C' },
{ sets: ['A', 'B'], size: 2, label: 'A&B' },
{ sets: ['A', 'C'], size: 2, label: 'A&C' },
{ sets: ['B', 'C'], size: 2, label: 'B&C' },
{ sets: ['A', 'B', 'C'], size: 1 },
],
setsField: 'sets',
sizeField: 'size',
label: {
position: 'inside',
text: (d) => d.label || '',
},
tooltip: {
title: false,
items: [
(d) => {
return { name: d.key, value: d.size };
},
],
},
style: {
fillOpacity: 0.85,
fill: (datum, index, data) => {
console.log(data);
const { size } = datum;
if (size <= 2) return '#f4bb51';
},
},
};
return <Venn {...config} />;
};

ReactDOM.render(<DemoVenn />, document.getElementById('container'));
51 changes: 51 additions & 0 deletions site/examples/statistics/venn/demo/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Venn } from '@ant-design/plots';
import React from 'react';
import ReactDOM from 'react-dom';

const DemoVenn = () => {
const config = {
data: {
type: 'fetch',
value: 'https://gw.alipayobjects.com/os/bmw-prod/c4c17fe9-0a93-4255-bc1e-1ff84966d24a.json',
transform: [
{
type: 'venn',
sets: 'sets',
size: 'size',
as: ['key', 'path'],
},
],
},
setsField: 'sets',
sizeField: 'size',
style: { fillOpacity: 0.85 },
scale: {
color: {
range: ['#9DF5CA', '#61DDAA', '#42C090'],
},
},
label: {
position: 'inside',
style: {
lineHeight: 20,
},
text: (datum) => {
return `${datum.size}`;
},
},
interaction: {
tooltip: {
// render 回调方法返回一个innerHTML 或者 DOM
render: (event, { title, items }) => {
return `<div>
<h3 style="padding:0;margin:0">title:${title}</h3>
<ul>${items.map((d) => `<li><span style="color: ${d.color}">${d.name}</span> ${d.value}</li>`)}</ul>
</div>`;
},
},
},
};
return <Venn {...config} />;
};

ReactDOM.render(<DemoVenn />, document.getElementById('container'));
49 changes: 49 additions & 0 deletions site/examples/statistics/venn/demo/interaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Venn } from '@ant-design/plots';
import React from 'react';
import ReactDOM from 'react-dom';

const DemoVenn = () => {
const config = {
data: [
{ sets: ['A'], size: 12, label: 'A' },
{ sets: ['B'], size: 12, label: 'B' },
{ sets: ['C'], size: 12, label: 'C' },
{ sets: ['A', 'B'], size: 2, label: 'A&B' },
{ sets: ['A', 'C'], size: 2, label: 'A&C' },
{ sets: ['B', 'C'], size: 2, label: 'B&C' },
{ sets: ['A', 'B', 'C'], size: 1 },
],
setsField: 'sets',
sizeField: 'size',
style: { fillOpacity: 0.85 },
label: {
position: 'inside',
text: (d) => d.label || '',
},
tooltip: {
title: false,
items: [
(d) => {
return { name: d.key, value: d.size };
},
],
},
state: {
active: {
fillOpacity: 0.8,
stroke: 'red',
lineWidth: 1,
},
inactive: {
fillOpacity: 0.2,
lineWidth: 0,
},
},
interaction: {
elementHighlight: true,
},
};
return <Venn {...config} />;
};

ReactDOM.render(<DemoVenn />, document.getElementById('container'));
28 changes: 28 additions & 0 deletions site/examples/statistics/venn/demo/label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Venn } from '@ant-design/plots';
import React from 'react';
import ReactDOM from 'react-dom';

const DemoVenn = () => {
const config = {
data: [
{ sets: ['A'], size: 12, label: 'A' },
{ sets: ['B'], size: 12, label: 'B' },
{ sets: ['C'], size: 12, label: 'C' },
{ sets: ['A', 'B'], size: 2, label: 'A&B' },
{ sets: ['A', 'C'], size: 2, label: 'A&C' },
{ sets: ['B', 'C'], size: 2, label: 'B&C' },
{ sets: ['A', 'B', 'C'], size: 1 },
],
setsField: 'sets',
sizeField: 'size',
style: { fillOpacity: 0.85 },
label: {
position: 'inside',
text: (d) => d.label || '',
transform: [{ type: 'contrastReverse' }],
}
};
return <Venn {...config} />;
};

ReactDOM.render(<DemoVenn />, document.getElementById('container'));
Loading