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(label): support specified transform (close: #4881) #4914

Merged
merged 1 commit into from
Apr 25, 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.
29 changes: 29 additions & 0 deletions __tests__/plots/static/alphabet-interval-label-rotate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalLabelRotate(): G2Spec {
return {
type: 'interval',
transform: [{ type: 'sortX', by: 'y', reverse: true }],
data: {
type: 'fetch',
value: 'data/alphabet.csv',
transform: [{ type: 'slice', end: 5 }],
},
axis: { y: { labelFormatter: '.0%' } },
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
labels: [
{
text: 'letter',
style: {
transform: 'translateY(10) rotate(45)',
transformOrigin: 'center center',
fontSize: 30,
},
},
],
};
}
pearmini marked this conversation as resolved.
Show resolved Hide resolved
pearmini marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,4 @@ export { gaugeCustomColor } from './gauge-custom-color';
export { gaugeCustomShape } from './gauge-custom-shape';
export { scoreByItemAreaRadarSize } from './score-by-item-area-radar-size';
export { mockPointLogTicks } from './mock-point-log-ticks';
export { alphabetIntervalLabelRotate } from './alphabet-interval-label-rotate';
pearmini marked this conversation as resolved.
Show resolved Hide resolved
pearmini marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 13 additions & 2 deletions src/shape/label/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ function getDefaultStyle(
*/
export const Label: SC<LabelOptions> = (options) => {
return (points, value, coordinate, theme) => {
const { text, x, y, ...overrideStyle } = value;
const {
text,
x,
y,
transform: specifiedTS = '',
transformOrigin,
...overrideStyle
} = value;
const {
rotate = 0,
transform = '',
Expand All @@ -56,7 +63,11 @@ export const Label: SC<LabelOptions> = (options) => {
return select(new Advance())
.call(applyStyle, defaultStyle)
.style('text', `${text}`)
.style('transform', `${transform}rotate(${+rotate})`)
.style(
'labelTransform',
`${transform} rotate(${+rotate}) ${specifiedTS}`.trim(),
)
.style('labelTransformOrigin', transformOrigin)
.style('coordCenter', coordinate.getCenter())
.call(applyStyle, overrideStyle)
.node();
pearmini marked this conversation as resolved.
Show resolved Hide resolved
pearmini marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
13 changes: 11 additions & 2 deletions src/shape/text/advance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type TextShapeStyleProps = Omit<TextStyleProps, 'text'> &
connector?: boolean;
startMarker?: boolean;
endMarker?: boolean;
labelTransform?: string;
labelTransformOrigin?: string;
rotate?: number;
};

function getConnectorPoint(shape: GText | Rect) {
Expand Down Expand Up @@ -125,8 +128,9 @@ export const Advance = createElement((g) => {
// Do not pass className
class: className,
transform,
// @ts-ignore
rotate,
labelTransform,
labelTransformOrigin,
x,
y,
x0 = x,
Expand All @@ -147,7 +151,12 @@ export const Advance = createElement((g) => {
const shape1 = select(g)
.maybeAppend('text', 'text')
.style('zIndex', 0)
.call(applyStyle, { textBaseline: 'middle', ...rest })
.call(applyStyle, {
textBaseline: 'middle',
transform: labelTransform,
transformOrigin: labelTransformOrigin,
...rest,
})
.node();

const shape2 = select(g)
pearmini marked this conversation as resolved.
Show resolved Hide resolved
pearmini marked this conversation as resolved.
Show resolved Hide resolved
Expand Down