From 519ee625a87e3fe7159fa17a1fcb65692e6c83c1 Mon Sep 17 00:00:00 2001 From: BENcorry <1417224785@qq.com> Date: Fri, 12 Apr 2024 22:43:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(label):=20=E4=BF=AE=E5=A4=8Dlabel=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../static/stackBarAnnotationStyleText.svg | 2454 +++++++++++++++++ __tests__/plots/static/index.ts | 1 + .../static/stack-bar-annotation-style-text.ts | 156 ++ src/runtime/plot.ts | 3 +- 4 files changed, 2613 insertions(+), 1 deletion(-) create mode 100644 __tests__/integration/snapshots/static/stackBarAnnotationStyleText.svg create mode 100644 __tests__/plots/static/stack-bar-annotation-style-text.ts diff --git a/__tests__/integration/snapshots/static/stackBarAnnotationStyleText.svg b/__tests__/integration/snapshots/static/stackBarAnnotationStyleText.svg new file mode 100644 index 0000000000..1d15b4f09c --- /dev/null +++ b/__tests__/integration/snapshots/static/stackBarAnnotationStyleText.svg @@ -0,0 +1,2454 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Lon + + + + + + + + + + + + + + + + + + + + Bor + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1991/01 + + + + + + + 1992/01 + + + + + + + 1993/01 + + + + + + + 1994/01 + + + + + + + 1995/01 + + + + + + + 1996/01 + + + + + + + 1997/01 + + + + + + + 1998/01 + + + + + + + 1999/01 + + + + + + + + + time + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + + + + + 5 + + + + + + + 10 + + + + + + + 15 + + + + + + + + + value + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 3 + + + + + + + + + + + + + + + 8 + + + + + + + + + + + + + + + 7 + + + + + + + + + + + + + + + 6 + + + + + + + + + + + + + + + 4 + + + + + + + + + + + + + + + 9 + + + + + + + + + + + + + + + 11 + + + + + + + + + + + + + + + 18 + + + + + + + + + + + + + + + 2 + + + + + + + + + + + + + + + 0 + + + + + + + + + + + + + 4 + + + + + + + + + + + + + 3.5 + + + + + + + + + + + + + 1 + + + + + + + + + + + + + 2 + + + + + + + + + + + + + 3 + + + + + + + + + + + + + 4 + + + + + + + + + + + + + 9 + + + + + + + + + + + + + 1 + + + + + + + + + + + + + 3 + + + + + + + + + + + + + 4 + + + + + + + + + + + + + 3.5 + + + + + + + + + + + + + 5 + + + + + + + + + + + + + 2 + + + + + + + + + + + + + 6 + + + + + + + + + + + + + 7 + + + + + + + + + + + + + 9 + + + + + + + + + + + + + 1 + + + + + + + + + + + + \ No newline at end of file diff --git a/__tests__/plots/static/index.ts b/__tests__/plots/static/index.ts index 956912b23c..da38f22013 100644 --- a/__tests__/plots/static/index.ts +++ b/__tests__/plots/static/index.ts @@ -331,3 +331,4 @@ export { flareElementPointMoveRadar } from './flare-element-point-move-radar'; export { flareElementPointMovePie } from './flare-element-point-move-pie'; export { flareElementPointMoveArea } from './flare-element-point-move-area'; export { flareElementPointMoveAreaNormalizeY } from './flare-element-point-move-area-normalizeY'; +export { stackBarAnnotationStyleText } from './stack-bar-annotation-style-text'; diff --git a/__tests__/plots/static/stack-bar-annotation-style-text.ts b/__tests__/plots/static/stack-bar-annotation-style-text.ts new file mode 100644 index 0000000000..bfe0910d52 --- /dev/null +++ b/__tests__/plots/static/stack-bar-annotation-style-text.ts @@ -0,0 +1,156 @@ +import { G2Spec } from '../../../src'; + +export const data = [ + { + time: '1991/01', + value: 0, + type: 'Lon', + }, + { + time: '1992/01', + value: 4, + type: 'Lon', + }, + { + time: '1993/01', + value: 3.5, + type: 'Lon', + }, + { + time: '1994/01', + value: 1, + type: 'Lon', + }, + { + time: '1995/01', + value: 2, + type: 'Lon', + }, + { + time: '1996/01', + value: 3, + type: 'Lon', + }, + { + time: '1997/01', + value: 4, + type: 'Lon', + }, + { + time: '1998/01', + value: 9, + type: 'Lon', + }, + { + time: '1999/01', + value: 1, + type: 'Lon', + }, + { + time: '1991/01', + value: 3, + type: 'Bor', + }, + { + time: '1992/01', + value: 4, + type: 'Bor', + }, + { + time: '1993/01', + value: 3.5, + type: 'Bor', + }, + { + time: '1994/01', + value: 5, + type: 'Bor', + }, + { + time: '1995/01', + value: 2, + type: 'Bor', + }, + { + time: '1996/01', + value: 6, + type: 'Bor', + }, + { + time: '1997/01', + value: 7, + type: 'Bor', + }, + { + time: '1998/01', + value: 9, + type: 'Bor', + }, + { + time: '1999/01', + value: 1, + type: 'Bor', + }, +]; + +export function stackBarAnnotationStyleText(): G2Spec { + const groupData = data.reduce((acc, item) => { + if (acc[item.time]) { + acc[item.time] = [...acc[item.time], item]; + } else { + acc[item.time] = [item]; + } + return acc; + }, {}); + + const annotations = Object.keys(groupData).map((key) => { + const val = groupData[key].reduce((a, b) => a + b.value, 0); + return { + type: 'text', + data: [key, val], + encode: { + x: 'time', + y: 'value', + }, + style: { + text: `${val}`, + textBaseline: 'bottom', + position: 'top', + textAlign: 'center', + fontSize: 14, + fill: 'rgba(0,0,0,0.85)', + }, + tooltip: false, + }; + }); + return { + type: 'view', + height: 300, + width: 640, + data, + children: [ + { + type: 'interval', + axis: { x: { labelAutoHide: true } }, + encode: { + x: 'time', + y: 'value', + color: 'type', + }, + transform: [ + { + type: 'stackY', + }, + ], + labels: [ + { + text: 'value', + textBaseline: 'bottom', + position: 'inside', + }, + ], + }, + ...annotations, + ], + }; +} diff --git a/src/runtime/plot.ts b/src/runtime/plot.ts index f3230f34e5..dbd12efd43 100644 --- a/src/runtime/plot.ts +++ b/src/runtime/plot.ts @@ -6,6 +6,7 @@ import { format } from 'd3-format'; import { mapObject } from '../utils/array'; import { ChartEvent } from '../utils/event'; import { + isStrictObject, appendTransform, compose, copyAttributes, @@ -1378,7 +1379,7 @@ function valueOf( ) { if (typeof value === 'function') return value(datum, i, data); if (typeof value !== 'string') return value; - if (datum[value] !== undefined) return datum[value]; + if (isStrictObject(datum) && datum[value] !== undefined) return datum[value]; return value; }