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

fix(legend): set actual item size #5326

Merged
merged 1 commit into from
Jul 18, 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified __tests__/integration/snapshots/static/cars2PointJitterY.png
Binary file modified __tests__/integration/snapshots/static/irisPointShapesDefaults.png
Binary file modified __tests__/integration/snapshots/static/titanic2PointPack.png
Binary file modified __tests__/integration/snapshots/static/titanicPointPack.png
Binary file modified __tests__/integration/snapshots/static/titanicPointPackNested.png
1 change: 0 additions & 1 deletion __tests__/plots/static/cars2-point-constant-color-size.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { G2Spec } from '../../../src';

// @todo Fix marker stroke of @antv/gui.
export function cars2PointConstantColorSize(): G2Spec {
return {
type: 'point',
Expand Down
7 changes: 5 additions & 2 deletions __tests__/plots/static/cars2-point-ordinal-size.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { G2Spec } from '../../../src';

// @todo Fix category item align in @antv/gui.
export function cars2PointOrdinalSize(): G2Spec {
return {
type: 'point',
Expand All @@ -16,6 +15,10 @@ export function cars2PointOrdinalSize(): G2Spec {
size: 'Origin',
shape: 'point',
},
scale: { x: { nice: true }, y: { nice: true } },
scale: {
x: { nice: true },
y: { nice: true },
size: { rangeMax: 20 },
},
};
}
48 changes: 20 additions & 28 deletions src/component/legendCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,6 @@ export type LegendCategoryOptions = {
[key: string]: any;
};

function createShape(
shape: string,
library: G2Library,
coordinate: Coordinate,
theme: G2Theme,
style: Record<string, any> = {},
) {
const marker =
(library[`shape.${shape}`]?.props?.defaultMarker as string) ||
last(shape.split('.'));
return () => useMarker(marker, style)(0, 0, 6);
}

function inferShape(scales: Scale[], markState: Map<G2Mark, G2MarkState>) {
const shapeScale = scaleOf(scales, 'shape');
const colorScale = scaleOf(scales, 'color');
Expand Down Expand Up @@ -93,28 +80,35 @@ function inferShape(scales: Scale[], markState: Map<G2Mark, G2MarkState>) {

function inferItemMarker(
options,
{ scales, library, coordinate, theme, markState }: GuideComponentContext,
context: GuideComponentContext,
): ((datum: any, i: number, data: any) => () => DisplayObject) | undefined {
const { scales, library, markState } = context;
const [namespace, shapes] = inferShape(scales, markState);
const create = (name, color) =>
createShape(name, library, coordinate, theme, {
color,
});

const { itemMarker, itemMarkerSize: size } = options;

const create = (name, d) => {
const marker =
(library[`shape.${name}`]?.props?.defaultMarker as string) ||
last(name.split('.'));
const radius = typeof size === 'function' ? size(d) : size;
return () => useMarker(marker, { color: d.color })(0, 0, radius);
};

const shapeOf = (i) => `${namespace}.${shapes[i]}`;

const { itemMarker } = options;
const shapeScale = scaleOf(scales, 'shape');
if (shapeScale && !itemMarker) return (d, i) => create(shapeOf(i), d.color);
if (shapeScale && !itemMarker) return (d, i) => create(shapeOf(i), d);
if (typeof itemMarker === 'function') {
return (d, i) => {
// @todo Fix this in GUI.
// It should pass primitive value rather object.
const node = itemMarker(d.id, i);
if (typeof node === 'string') return create(node, d.color);
if (typeof node === 'string') return create(node, d);
return node;
};
}
return (d, i) => create(itemMarker || shapeOf(i), d.color);
return (d, i) => create(itemMarker || shapeOf(i), d);
}

function inferItemMarkerOpacity(scales: Scale[]) {
Expand All @@ -129,19 +123,17 @@ function inferItemMarkerOpacity(scales: Scale[]) {
function inferItemMarkerSize(scales: Scale[], defaults: number) {
const scale = scaleOf(scales, 'size');
if (scale instanceof Identity) return scale.map(NaN) * 2;
if (scale instanceof Point) return ({ id }) => scale.map(id) * 2;
return defaults;
}

function inferCategoryStyle(options, context: GuideComponentContext) {
const { labelFormatter = (d) => `${d}` } = options;
const { scales, theme } = context;
const defaultSize = theme.legendCategory.itemMarkerSize;
const itemMarkerSize = inferItemMarkerSize(scales, defaultSize);
const baseStyle = {
itemMarker: inferItemMarker(options, context),
itemMarkerSize: inferItemMarkerSize(
scales,
theme.legendCategory.itemMarkerSize,
),
itemMarker: inferItemMarker({ ...options, itemMarkerSize }, context),
itemMarkerSize: itemMarkerSize,
itemMarkerOpacity: inferItemMarkerOpacity(scales),
};

Expand Down
13 changes: 1 addition & 12 deletions src/runtime/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,20 +757,9 @@ function computeCategoryLegendSize(
theme: G2Theme,
library: G2Library,
) {
const itemMakerSizeOf = () => {
const { itemMarkerSize } = component.style || {};
if (itemMarkerSize) return itemMarkerSize;
const { scales } = component;
const size = scales.find((d) => d.name === 'size');
if (!size) return itemMarkerSize;
return size.range[1] * 2;
};

const styleOf = () => {
const { legendCategory } = theme;
return deepMix({}, legendCategory, component.style, {
itemMarkerSize: itemMakerSizeOf(),
});
return deepMix({}, legendCategory, component.style);
};

const {
Expand Down