diff --git a/__tests__/integration/snapshots/api/chart-auto-fit/step0.png b/__tests__/integration/snapshots/api/chart-auto-fit/step0.png index eb0524f7ae..05cff67a89 100644 Binary files a/__tests__/integration/snapshots/api/chart-auto-fit/step0.png and b/__tests__/integration/snapshots/api/chart-auto-fit/step0.png differ diff --git a/__tests__/integration/snapshots/api/chart-auto-fit/step1.png b/__tests__/integration/snapshots/api/chart-auto-fit/step1.png index 06d0fb6527..7edcf8dd8b 100644 Binary files a/__tests__/integration/snapshots/api/chart-auto-fit/step1.png and b/__tests__/integration/snapshots/api/chart-auto-fit/step1.png differ diff --git a/__tests__/integration/snapshots/api/chartChangeSize.png b/__tests__/integration/snapshots/api/chartChangeSize.png index a4019ffe0a..9f9f20930d 100644 Binary files a/__tests__/integration/snapshots/api/chartChangeSize.png and b/__tests__/integration/snapshots/api/chartChangeSize.png differ diff --git a/__tests__/integration/snapshots/api/chartRender.png b/__tests__/integration/snapshots/api/chartRender.png index 1666d070f1..05cff67a89 100644 Binary files a/__tests__/integration/snapshots/api/chartRender.png and b/__tests__/integration/snapshots/api/chartRender.png differ diff --git a/__tests__/integration/snapshots/api/markChangeData.png b/__tests__/integration/snapshots/api/markChangeData.png index 9fe73fdfb2..42e8aa08a4 100644 Binary files a/__tests__/integration/snapshots/api/markChangeData.png and b/__tests__/integration/snapshots/api/markChangeData.png differ diff --git a/__tests__/integration/snapshots/api/registerDataTransform.png b/__tests__/integration/snapshots/api/registerDataTransform.png index b1a1f50645..8d03b0e79b 100644 Binary files a/__tests__/integration/snapshots/api/registerDataTransform.png and b/__tests__/integration/snapshots/api/registerDataTransform.png differ diff --git a/__tests__/integration/snapshots/interaction/aapl-line-slider-filter/step0.png b/__tests__/integration/snapshots/interaction/aapl-line-slider-filter/step0.png index a3cd2b7036..2d78684635 100644 Binary files a/__tests__/integration/snapshots/interaction/aapl-line-slider-filter/step0.png and b/__tests__/integration/snapshots/interaction/aapl-line-slider-filter/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/aapl-line-slider-filter/step1.png b/__tests__/integration/snapshots/interaction/aapl-line-slider-filter/step1.png index a279ead76d..c1dcdfc893 100644 Binary files a/__tests__/integration/snapshots/interaction/aapl-line-slider-filter/step1.png and b/__tests__/integration/snapshots/interaction/aapl-line-slider-filter/step1.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-axis/step0.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-axis/step0.png index 53e6fc4284..d0d398deb4 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-axis/step0.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-axis/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-axis/step1.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-axis/step1.png index d5234b176c..088f1a7ca9 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-axis/step1.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-axis/step1.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step0.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step0.png index 22687fba0d..d6ab396ed0 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step0.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step1.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step1.png index 84dfd624ea..c1ea66b18e 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step1.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step1.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step2.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step2.png index 08119f4e4f..33d55c3d64 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step2.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-filter/step2.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-reverse/step0.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-reverse/step0.png index 70bc6d9cb5..da8f576560 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-reverse/step0.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-reverse/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-reverse/step1.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-reverse/step1.png index 7eb144e9f7..ba927e3386 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-reverse/step1.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-reverse/step1.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-x-filter/step0.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-x-filter/step0.png index 01459215e6..7c2c68024d 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-x-filter/step0.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-x-filter/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-x/step0.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-x/step0.png index d0ddc8da22..a66272edd8 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-x/step0.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-x/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-y-filter/step0.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-y-filter/step0.png index d3cba66fb3..4ff984ffe3 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-y-filter/step0.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-y-filter/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush-y/step0.png b/__tests__/integration/snapshots/interaction/penguins-point-brush-y/step0.png index fbdb408329..deef17caa4 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush-y/step0.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush-y/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush/step0.png b/__tests__/integration/snapshots/interaction/penguins-point-brush/step0.png index 17087ca20f..1354216614 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush/step0.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush/step1.png b/__tests__/integration/snapshots/interaction/penguins-point-brush/step1.png index ce5e704065..bf0df20e3d 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush/step1.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush/step1.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush/step2.png b/__tests__/integration/snapshots/interaction/penguins-point-brush/step2.png index 8cdc3c91d7..0e69a22d07 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush/step2.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush/step2.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-brush/step3.png b/__tests__/integration/snapshots/interaction/penguins-point-brush/step3.png index 5836d535bf..4061af5f50 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-brush/step3.png and b/__tests__/integration/snapshots/interaction/penguins-point-brush/step3.png differ diff --git a/__tests__/integration/snapshots/interaction/penguins-point-highlight/step0.png b/__tests__/integration/snapshots/interaction/penguins-point-highlight/step0.png index ad311c08d3..6da8aa6266 100644 Binary files a/__tests__/integration/snapshots/interaction/penguins-point-highlight/step0.png and b/__tests__/integration/snapshots/interaction/penguins-point-highlight/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-brush-filter/step0.png b/__tests__/integration/snapshots/interaction/profit-interval-brush-filter/step0.png index 8180588574..5723842bdf 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-brush-filter/step0.png and b/__tests__/integration/snapshots/interaction/profit-interval-brush-filter/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-brush-filter/step2.png b/__tests__/integration/snapshots/interaction/profit-interval-brush-filter/step2.png index cc4f1f1edf..ae3d5e3f63 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-brush-filter/step2.png and b/__tests__/integration/snapshots/interaction/profit-interval-brush-filter/step2.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step0.png b/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step0.png index 37f55341a5..b557538951 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step0.png and b/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step1.png b/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step1.png index bd66441709..233f3fa4c6 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step1.png and b/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step1.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step2.png b/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step2.png index 7b40a55a2d..cb257b3702 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step2.png and b/__tests__/integration/snapshots/interaction/profit-interval-legend-filter-ordinal/step2.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step0.png b/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step0.png index c7b38d6d49..d33e0b3a29 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step0.png and b/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step1.png b/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step1.png index 2065ba434f..70b3af84a5 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step1.png and b/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step1.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step2.png b/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step2.png index 2065ba434f..70b3af84a5 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step2.png and b/__tests__/integration/snapshots/interaction/profit-interval-legend-highlight/step2.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-slider-filter/step0.png b/__tests__/integration/snapshots/interaction/profit-interval-slider-filter/step0.png index f5d42c16d5..5f5bb6e146 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-slider-filter/step0.png and b/__tests__/integration/snapshots/interaction/profit-interval-slider-filter/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/profit-interval-slider-filter/step1.png b/__tests__/integration/snapshots/interaction/profit-interval-slider-filter/step1.png index 8c93882b87..123d9f13aa 100644 Binary files a/__tests__/integration/snapshots/interaction/profit-interval-slider-filter/step1.png and b/__tests__/integration/snapshots/interaction/profit-interval-slider-filter/step1.png differ diff --git a/__tests__/integration/snapshots/interaction/state-ages-interval-highlight-group-background/step0.png b/__tests__/integration/snapshots/interaction/state-ages-interval-highlight-group-background/step0.png index 564375d65a..5673dd1d8b 100644 Binary files a/__tests__/integration/snapshots/interaction/state-ages-interval-highlight-group-background/step0.png and b/__tests__/integration/snapshots/interaction/state-ages-interval-highlight-group-background/step0.png differ diff --git a/__tests__/integration/snapshots/interaction/state-ages-interval-highlight-group-background/step2.png b/__tests__/integration/snapshots/interaction/state-ages-interval-highlight-group-background/step2.png index 564375d65a..5673dd1d8b 100644 Binary files a/__tests__/integration/snapshots/interaction/state-ages-interval-highlight-group-background/step2.png and b/__tests__/integration/snapshots/interaction/state-ages-interval-highlight-group-background/step2.png differ diff --git a/__tests__/integration/snapshots/static/agesIntervalRadialStacked.png b/__tests__/integration/snapshots/static/agesIntervalRadialStacked.png index fd89146084..740a72ff3e 100644 Binary files a/__tests__/integration/snapshots/static/agesIntervalRadialStacked.png and b/__tests__/integration/snapshots/static/agesIntervalRadialStacked.png differ diff --git a/__tests__/integration/snapshots/static/agesIntervalRadialStackedSorted.png b/__tests__/integration/snapshots/static/agesIntervalRadialStackedSorted.png index 308b11f064..eb3a679f37 100644 Binary files a/__tests__/integration/snapshots/static/agesIntervalRadialStackedSorted.png and b/__tests__/integration/snapshots/static/agesIntervalRadialStackedSorted.png differ diff --git a/__tests__/integration/snapshots/static/athletesRectBin.png b/__tests__/integration/snapshots/static/athletesRectBin.png index 9a93923cc3..ae71b36a74 100644 Binary files a/__tests__/integration/snapshots/static/athletesRectBin.png and b/__tests__/integration/snapshots/static/athletesRectBin.png differ diff --git a/__tests__/integration/snapshots/static/cars2PointJitterY.png b/__tests__/integration/snapshots/static/cars2PointJitterY.png index 2f801bb953..1f04036483 100644 Binary files a/__tests__/integration/snapshots/static/cars2PointJitterY.png and b/__tests__/integration/snapshots/static/cars2PointJitterY.png differ diff --git a/__tests__/integration/snapshots/static/flareTreemapCustom.png b/__tests__/integration/snapshots/static/flareTreemapCustom.png index c39da95892..ab5b96e940 100644 Binary files a/__tests__/integration/snapshots/static/flareTreemapCustom.png and b/__tests__/integration/snapshots/static/flareTreemapCustom.png differ diff --git a/__tests__/integration/snapshots/static/housePricePointShapes.png b/__tests__/integration/snapshots/static/housePricePointShapes.png index cb2ca52c9d..b8f4f8f9d2 100644 Binary files a/__tests__/integration/snapshots/static/housePricePointShapes.png and b/__tests__/integration/snapshots/static/housePricePointShapes.png differ diff --git a/__tests__/integration/snapshots/static/indicesLineChartScaleRelations.png b/__tests__/integration/snapshots/static/indicesLineChartScaleRelations.png index 5ff2dc27bd..9e60947a69 100644 Binary files a/__tests__/integration/snapshots/static/indicesLineChartScaleRelations.png and b/__tests__/integration/snapshots/static/indicesLineChartScaleRelations.png differ diff --git a/__tests__/integration/snapshots/static/ordersLineMissingField.png b/__tests__/integration/snapshots/static/ordersLineMissingField.png index ff667713ea..735d3ee311 100644 Binary files a/__tests__/integration/snapshots/static/ordersLineMissingField.png and b/__tests__/integration/snapshots/static/ordersLineMissingField.png differ diff --git a/__tests__/integration/snapshots/static/penguinsPointFacetRect.png b/__tests__/integration/snapshots/static/penguinsPointFacetRect.png index 1295936c76..f12caedd80 100644 Binary files a/__tests__/integration/snapshots/static/penguinsPointFacetRect.png and b/__tests__/integration/snapshots/static/penguinsPointFacetRect.png differ diff --git a/__tests__/integration/snapshots/static/productIntervalStackedGrouped.png b/__tests__/integration/snapshots/static/productIntervalStackedGrouped.png index cac6b15dec..21746ee6fe 100644 Binary files a/__tests__/integration/snapshots/static/productIntervalStackedGrouped.png and b/__tests__/integration/snapshots/static/productIntervalStackedGrouped.png differ diff --git a/__tests__/integration/snapshots/static/profitIntervalRange.png b/__tests__/integration/snapshots/static/profitIntervalRange.png index d977aae3da..65db5ae800 100644 Binary files a/__tests__/integration/snapshots/static/profitIntervalRange.png and b/__tests__/integration/snapshots/static/profitIntervalRange.png differ diff --git a/__tests__/integration/snapshots/static/questionsIntervalDivergingStacked.png b/__tests__/integration/snapshots/static/questionsIntervalDivergingStacked.png index 812a48880e..5c48200858 100644 Binary files a/__tests__/integration/snapshots/static/questionsIntervalDivergingStacked.png and b/__tests__/integration/snapshots/static/questionsIntervalDivergingStacked.png differ diff --git a/__tests__/integration/snapshots/static/seattleWeatherIntervalAggregated.png b/__tests__/integration/snapshots/static/seattleWeatherIntervalAggregated.png index 094f76a49c..d68acc7765 100644 Binary files a/__tests__/integration/snapshots/static/seattleWeatherIntervalAggregated.png and b/__tests__/integration/snapshots/static/seattleWeatherIntervalAggregated.png differ diff --git a/__tests__/integration/snapshots/static/stateAgesIntervalGrouped.png b/__tests__/integration/snapshots/static/stateAgesIntervalGrouped.png index 657713fe9a..7c4bfba566 100644 Binary files a/__tests__/integration/snapshots/static/stateAgesIntervalGrouped.png and b/__tests__/integration/snapshots/static/stateAgesIntervalGrouped.png differ diff --git a/__tests__/integration/snapshots/static/stateAgesIntervalStacked.png b/__tests__/integration/snapshots/static/stateAgesIntervalStacked.png index 3d2979a642..f5cad10dcd 100644 Binary files a/__tests__/integration/snapshots/static/stateAgesIntervalStacked.png and b/__tests__/integration/snapshots/static/stateAgesIntervalStacked.png differ diff --git a/__tests__/integration/snapshots/static/stateAgesIntervalStackedCornered.png b/__tests__/integration/snapshots/static/stateAgesIntervalStackedCornered.png index 8bedf970a8..5ad91f8f81 100644 Binary files a/__tests__/integration/snapshots/static/stateAgesIntervalStackedCornered.png and b/__tests__/integration/snapshots/static/stateAgesIntervalStackedCornered.png differ diff --git a/__tests__/integration/snapshots/static/stateAgesIntervalStackedCorneredAll.png b/__tests__/integration/snapshots/static/stateAgesIntervalStackedCorneredAll.png index b1c89dde09..f1388e04b6 100644 Binary files a/__tests__/integration/snapshots/static/stateAgesIntervalStackedCorneredAll.png and b/__tests__/integration/snapshots/static/stateAgesIntervalStackedCorneredAll.png differ diff --git a/__tests__/integration/snapshots/static/stocksLineAggregateLabel.png b/__tests__/integration/snapshots/static/stocksLineAggregateLabel.png index b8b7b97a76..05f02cd492 100644 Binary files a/__tests__/integration/snapshots/static/stocksLineAggregateLabel.png and b/__tests__/integration/snapshots/static/stocksLineAggregateLabel.png differ diff --git a/__tests__/plots/static/athletes-rect-bin-item-marker.ts b/__tests__/plots/static/athletes-rect-bin-item-marker.ts deleted file mode 100644 index 78856ec23d..0000000000 --- a/__tests__/plots/static/athletes-rect-bin-item-marker.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Rect } from '@antv/g'; -import { G2Spec } from '../../../src'; - -export function athletesRectBinItemMarker(): G2Spec { - return { - type: 'rect', - data: { - type: 'fetch', - value: 'data/athletes.csv', - }, - transform: [{ type: 'bin', opacity: 'count' }], - encode: { - x: 'weight', - y: 'height', - color: 'sex', - }, - legend: { - color: { - itemMarker: 'square', - }, - }, - style: { - inset: 0.5, - }, - }; -} diff --git a/__tests__/plots/static/index.ts b/__tests__/plots/static/index.ts index 1f964ed07d..c4fe8b0024 100644 --- a/__tests__/plots/static/index.ts +++ b/__tests__/plots/static/index.ts @@ -126,7 +126,6 @@ export { alphabetIntervalSpaceLayer } from './alphabet-interval-space-layer'; export { unemployment2RectBinX } from './unemployment2-rect-binx'; export { athletesRectBinX } from './athletes-rect-binx'; export { athletesRectBin } from './athletes-rect-bin'; -export { athletesRectBinItemMarker } from './athletes-rect-bin-item-marker'; export { moviesPointBin } from './movies-point-bin'; export { moviesRectBin } from './movies-rect-bin'; export { moviesRectBinOpacity } from './movies-rect-bin-opacity'; diff --git a/src/component/legendCategory.ts b/src/component/legendCategory.ts index 0bdfe084d5..36732bd0ca 100644 --- a/src/component/legendCategory.ts +++ b/src/component/legendCategory.ts @@ -19,6 +19,7 @@ import { inferComponentLayout, titleContent, scaleOf, + domainOf, adaptor, } from './utils'; @@ -90,10 +91,10 @@ function inferItemMarker( const { itemMarker } = options; if (shapeScale && !itemMarker) { - const { range } = shapeScale.getOptions(); + const { domain } = shapeScale.getOptions(); return (d, i) => - createShape(range[i], library, coordinate, theme, { + createShape(shapeScale.map(domain[i]), library, coordinate, theme, { color: d.color, }); } @@ -115,6 +116,14 @@ function inferItemMarkerOpacity(scales: Scale[]) { return undefined; } +function inferItemMarkerSize(scales: Scale[]) { + const scale = scaleOf(scales, 'size'); + // only support constant size scale. + // size in category legend means the marker radius. + if (scale) return scale.map(NaN) * 2; + return 8; +} + function inferCategoryStyle( scales: Scale[], options: LegendCategoryOptions, @@ -126,6 +135,7 @@ function inferCategoryStyle( const baseStyle = { itemMarker: inferItemMarker(scales, options, library, coordinate, theme), + itemMarkerSize: inferItemMarkerSize(scales), itemMarkerOpacity: inferItemMarkerOpacity(scales), }; @@ -135,14 +145,15 @@ function inferCategoryStyle( : labelFormatter; // here must exists a color scale - const scale = scaleOf(scales, 'color'); + const colorScale = scaleOf(scales, 'color'); + const domain = domainOf(scales); return { ...baseStyle, - data: scale.getOptions().domain.map((d) => ({ + data: domain.map((d) => ({ id: d, label: finalLabelFormatter(d), - color: scale.map(d), + color: colorScale.map(d), })), }; } @@ -206,6 +217,7 @@ export const LegendCategory: GCC = (options) => { ...finalLayout, }, }); + layoutWrapper.appendChild( new Category({ className: 'legend-category', diff --git a/src/component/utils.ts b/src/component/utils.ts index a7ebff163c..6b59dd08cc 100644 --- a/src/component/utils.ts +++ b/src/component/utils.ts @@ -88,6 +88,13 @@ export function scaleOf(scales: Scale[], type: string): Scale | undefined { return scales.filter((s) => s.getOptions().name === type)?.[0]; } +export function domainOf(scales: Scale[]): any[] { + // to get a available scale's domain + return scales + .find((scale) => scale.getOptions().domain.length > 0) + .getOptions().domain; +} + export function adaptor(style: Record) { const newStyle: any = { style: {}, diff --git a/src/runtime/component.ts b/src/runtime/component.ts index 77414bee83..9f11450ed7 100644 --- a/src/runtime/component.ts +++ b/src/runtime/component.ts @@ -43,6 +43,7 @@ import { G2View, } from './types/options'; import { + ConstantScale, ContinuousScale, DiscreteScale, DistributionScale, @@ -153,8 +154,29 @@ function inferLegendComponentType( scales: G2ScaleOptions[], coordinates: G2CoordinateOptions[], ) { - const scalesByField = group(scales, (d) => d.field || d.name); + const acceptScales = scales.filter((scale) => + typeof scale.type === 'string' + ? ['shape', 'size', 'color', 'opacity'].includes(scale.name) + : true, + ); + // exclude the scales that all type are constant + const scalesByField = new Map( + Array.from( + group(acceptScales, (d) => d.field || '__internal_unset__'), + ).filter(([_, scales]) => + scales.some((scale) => scale.type !== 'constant'), + ), + ) as Map; + + if (scalesByField.size === 0) return []; + + /** + * + * @param arr + * @param main necessary channel + * @returns + */ const createStrategy = (arr: T[], main: T): T[][] => { const result = combine(arr); result.forEach((c) => c.unshift(main)); @@ -162,21 +184,44 @@ function inferLegendComponentType( return result.sort((a, b) => b.length - a.length); }; + const createCategoryStrategy = () => { + // category legend only support constant size + const color = [ + ['color', 'discrete'], + ['color', 'constant'], + ]; + const shape = [ + ['shape', 'discrete'], + ['shape', 'constant'], + ]; + const size = [['size', 'constant']]; + const opacity = [ + ['opacity', 'discrete'], + ['opacity', 'constant'], + ]; + + const stg: [string, string][][] = []; + for (const cr of color) { + for (const sp of shape) { + for (const sz of size) { + for (const op of opacity) { + if (![cr, sp, sz, op].every((d) => d[1] === 'constant')) { + stg.push( + ...(createStrategy([sp, sz, op], cr) as [string, string][][]), + ); + } + } + } + } + } + // refactor above code + + return stg.sort((a, b) => b.length - a.length); + }; + // [legend type, [[channels, scale types]]][] const strategy: [string, [string, string][][]][] = [ - [ - 'legendCategory', - createStrategy( - [ - // @todo now haven't provide constant scale of color channel, - // so won't display the single shape scale. - ['shape', 'discrete'], - ['size', 'discrete'], - ['opacity', 'discrete'], - ], - ['color', 'discrete'], - ), - ], + ['legendCategory', createCategoryStrategy()], [ 'legendContinuousSize', [ @@ -185,10 +230,19 @@ function inferLegendComponentType( ['opacity', 'continuous'], ['size', 'continuous'], ], + [ + ['color', 'constant'], + ['opacity', 'continuous'], + ['size', 'continuous'], + ], [ ['color', 'continuous'], ['size', 'continuous'], ], + [ + ['color', 'constant'], + ['size', 'continuous'], + ], ], ], [ @@ -214,18 +268,18 @@ function inferLegendComponentType( createStrategy([['opacity', 'continuous']], ['color', 'continuous']), ], ]; - function getScaleType(scale: G2ScaleOptions): string { const { type } = scale; if (typeof type !== 'string') return null; if (type in ContinuousScale) return 'continuous'; if (type in DiscreteScale) return 'discrete'; if (type in DistributionScale) return 'distribution'; + if (type in ConstantScale) return 'constant'; return null; } const components = Array.from(scalesByField) - .map(([, scs]) => { + .map(([channel, scs]) => { const combinations = combine(scs).sort((a, b) => b.length - a.length); const options = combinations.map((combination) => ({ combination, @@ -286,7 +340,6 @@ function inferComponentsType( coordinates: G2CoordinateOptions[], ): [string | GCC, G2ScaleOptions[]][] { const avaliableScales = scales.filter((scale) => isValidScale(scale)); - return [ ...inferLegendComponentType(avaliableScales, coordinates), ...inferAxisComponentType(avaliableScales, coordinates), diff --git a/src/runtime/plot.ts b/src/runtime/plot.ts index 92b21eaada..59f7368dd4 100644 --- a/src/runtime/plot.ts +++ b/src/runtime/plot.ts @@ -68,6 +68,7 @@ import { G2Library, G2Mark, G2MarkOptions, + G2ScaleOptions, G2ShapeOptions, G2ThemeOptions, G2View, @@ -486,14 +487,17 @@ function initializeState( } = options; const theme = useTheme(inferTheme(partialTheme)); - // Infer components and compute layout. const states = Array.from(markState.values()); const scales = Array.from( new Set(states.flatMap((d) => d.channels.map((d) => d.scale))), ); - const components = inferComponent(Array.from(scales), options, library); + const components = inferComponent( + inferComponentScales(Array.from(scales), states, markState), + options, + library, + ); const layout = computeLayout(components, options); const coordinate = createCoordinate(layout, options, library); const framedStyle = frame @@ -1383,6 +1387,48 @@ function applyClip(selection, clip?: boolean) { }); } +function inferComponentScales( + scales: G2ScaleOptions[], + states: G2MarkState[], + markState: Map, +): G2ScaleOptions[] { + // add shape scale to state. + + // for cell, omit shape scale. + // @todo support shape scale for cell. + for (const [key] of markState.entries()) { + if (key.type === 'cell') { + return scales.filter((scale) => scale.name !== 'shape'); + } + } + + // can't infer shape scale if there are multiple states. + if (states.length !== 1 || scales.some((scale) => scale.name === 'shape')) { + return scales; + } + + const { defaultShape: shape } = states[0]; + const acceptMarkTypes = ['point', 'line', 'rect', 'hollow']; + if (!acceptMarkTypes.includes(shape)) return scales; + const shapeMap = { + point: 'point', + line: 'hyphen', + rect: 'square', + hollow: 'hollow', + }; + + // create shape scale + const field = scales.find((scale) => scale.name === 'color')?.field || null; + const shapeScale = { + field, + name: 'shape', + type: 'constant', + domain: [], + range: [shapeMap[shape]], + }; + return [...scales, shapeScale]; +} + export function applyStyle( selection: Selection, style: Record, diff --git a/src/runtime/types/component.ts b/src/runtime/types/component.ts index b5a5bc6b11..ad9be30d11 100644 --- a/src/runtime/types/component.ts +++ b/src/runtime/types/component.ts @@ -1,11 +1,10 @@ import { Coordinate, Transformation } from '@antv/coord'; import { DisplayObject, IAnimation as GAnimation } from '@antv/g'; -import { G2ViewTree } from './options'; import { G2Theme, G2ViewInstance, - GuideComponentPosition, GuideComponentOrientation, + GuideComponentPosition, IndexedValue, Layout, Vector2, @@ -13,6 +12,7 @@ import { import { DataComponent } from './data'; import { Encode, EncodeComponent } from './encode'; import { Mark, MarkComponent } from './mark'; +import { G2ViewTree } from './options'; import { Transform, TransformComponent } from './transform'; export type G2ComponentNamespaces = diff --git a/src/runtime/types/scale.ts b/src/runtime/types/scale.ts index e27f34a90a..2a1f3380b6 100644 --- a/src/runtime/types/scale.ts +++ b/src/runtime/types/scale.ts @@ -18,3 +18,7 @@ export const DiscreteScale = { band: 'band', point: 'point', } as const; + +export const ConstantScale = { + constant: 'constant', +} as const;