Skip to content

Commit

Permalink
feat: label size sync to key size
Browse files Browse the repository at this point in the history
  • Loading branch information
yvonneyx committed Oct 14, 2024
1 parent 03e82a4 commit f2057ff
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
13 changes: 6 additions & 7 deletions packages/g6/__tests__/demos/case-unicorns-investors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ export const caseUnicornsInvestors: TestCase = async (context) => {
},
state: {
inactive: {
fill: '#E0E0E0',
fillOpacity: 1,
fillOpacity: 0.3,
icon: false,
label: false,
labelBackground: false,
},
},
},
Expand All @@ -40,11 +38,9 @@ export const caseUnicornsInvestors: TestCase = async (context) => {
label: false,
labelText: (d) => d.data?.type,
labelBackground: true,
lineWidth: 1,
},
state: {
active: {
lineWidth: 3,
label: true,
},
inactive: {
Expand All @@ -63,7 +59,7 @@ export const caseUnicornsInvestors: TestCase = async (context) => {
{
type: 'map-node-size',
scale: 'linear',
maxSize: 80,
maxSize: 60,
minSize: 20,
},
],
Expand All @@ -90,7 +86,10 @@ export const caseUnicornsInvestors: TestCase = async (context) => {
type: 'fix-element-size',
enable: true,
},
'auto-adapt-label',
{
type: 'auto-adapt-label',
syncToKeySize: { maxFontSize: 16, minFontSize: 12 },
},
],
plugins: [
{
Expand Down
19 changes: 18 additions & 1 deletion packages/g6/src/behaviors/auto-adapt-label.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AABB } from '@antv/g';
import { groupBy, isFunction, throttle } from '@antv/util';
import { groupBy, isBoolean, isFunction, throttle } from '@antv/util';
import { GraphEvent } from '../constants';
import type { RuntimeContext } from '../runtime/types';
import type { Combo, Edge, Element, ID, IEvent, Node, NodeCentralityOptions, Padding } from '../types';
Expand Down Expand Up @@ -55,6 +55,13 @@ export interface AutoAdaptLabelOptions extends BaseBehaviorOptions {
* @defaultValue 0
*/
padding?: Padding;
/**
* <zh/> 是否根据 key 的大小同步调整标签的大小
*
* <en/> Whether to adjust the size of the label according to the size of the key
* @defaultValue true
*/
syncToKeySize?: boolean | { maxFontSize: number; minFontSize: number };
/**
* <zh/> 节流时间
*
Expand All @@ -79,6 +86,7 @@ export class AutoAdaptLabel extends BaseBehavior<AutoAdaptLabelOptions> {
throttle: 100,
padding: 0,
nodeSorter: { type: 'degree' },
syncToKeySize: true,
};

constructor(context: RuntimeContext, options: AutoAdaptLabelOptions) {
Expand Down Expand Up @@ -212,6 +220,15 @@ export class AutoAdaptLabel extends BaseBehavior<AutoAdaptLabelOptions> {
private showLabel = (element: Element) => {
const label = element.getShape('label');
if (label) setVisibility(label, 'visible');
if (this.options.syncToKeySize) {
const { size: sizeArr, labelFontSize } = element.attributes;
const size = Array.isArray(sizeArr) ? Math.min(...sizeArr) : sizeArr;
const { maxFontSize, minFontSize } = !isBoolean(this.options.syncToKeySize)
? this.options.syncToKeySize
: { maxFontSize: Infinity, minFontSize: labelFontSize };
const fontSize = Math.min(maxFontSize, Math.max(size / 2, minFontSize));
element.update({ labelFontSize: fontSize, labelLineHeight: fontSize });
}
element.toFront();
this.hiddenElements.delete(element.id);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/site/examples/feature/default/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"zh": "信息密度",
"en": "Information Density"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*HfH8SodrHo4AAAAAAAAAAAAADmJ7AQ/original"
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*KaoETa-gBDIAAAAAAAAAAAAADmJ7AQ/original"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20528,6 +20528,7 @@ const size = (node) => Math.max(...node.style.size);

const graph = new Graph({
data,
autoFit: 'view',
node: {
style: {
fillOpacity: 1,
Expand All @@ -20551,11 +20552,9 @@ const graph = new Graph({
label: false,
labelText: (d) => d.data?.type,
labelBackground: true,
lineWidth: 1,
},
state: {
active: {
lineWidth: 3,
label: true,
},
inactive: {
Expand Down Expand Up @@ -20601,7 +20600,10 @@ const graph = new Graph({
type: 'fix-element-size',
enable: true,
},
'auto-adapt-label',
{
type: 'auto-adapt-label',
syncToKeySize: { maxFontSize: 16, minFontSize: 12 },
},
],
animation: false,
});
Expand Down

0 comments on commit f2057ff

Please sign in to comment.