Skip to content

Commit

Permalink
feat: legend 支持 nameStyle 和 valueStyle (#1387)
Browse files Browse the repository at this point in the history
  • Loading branch information
ACERY1 committed Mar 10, 2022
1 parent 0657c4f commit eb24656
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/f2/src/components/legend/legendView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default (props) => {
itemFormatter,
style,
marker = 'circle', // 图例标记默认为 circle
nameStyle,
valueStyle,
} = props;

const formatValue = (value) => {
Expand Down Expand Up @@ -68,13 +70,15 @@ export default (props) => {
attrs={{
fill: filtered ? '#bfbfbf' : '#808080',
text: name,
...nameStyle,
}}
/>
{value ? (
<text
attrs={{
fill: '#808080',
text: formatValue(value),
...valueStyle,
}}
/>
) : null}
Expand Down
10 changes: 9 additions & 1 deletion packages/f2/src/components/legend/withLegend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Component from '../../base/component';
import Chart from '../../chart';
import { find } from '@antv/util';
import { getElementsByClassName, isInBBox } from '../../util';
import { Style } from '../../types';
import { Style, TextAttrs } from '../../types';

interface LegendItem {
/**
Expand Down Expand Up @@ -65,6 +65,14 @@ export interface LegendProps {
* 图例标记。
*/
marker?: 'circle' | 'square';
/**
* 用于设置图例项的文本样式
*/
nameStyle?: TextAttrs;
/**
* 用于设置图例项的文本样式
*/
valueStyle?: TextAttrs;
}

export default (View) => {
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 31 additions & 1 deletion packages/f2/test/components/legend/legend.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clone, values } from '@antv/util';
import { jsx, Component, Canvas, Chart, Tooltip, Geometry } from '../../../src';
import { jsx, Component, Canvas, Chart, Tooltip, Geometry, Interval } from '../../../src';
import { Line, Axis, Legend } from '../../../src/components';
import { createContext, delay } from '../../util';

Expand Down Expand Up @@ -153,5 +153,35 @@ describe('图例', () => {
await delay(1000);
expect(context).toMatchImageSnapshot();
});

it('设置 nameStyle', async () => {
const data = [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
];
const context = createContext('设置 nameStyle', {
height: '100px',
});
const { props } = (
<Canvas context={context} pixelRatio={1}>
<Chart data={data}>
<Legend
position="left"
nameStyle={{
fontSize: '40px',
fill: 'red',
}}
/>
<Geometry x="genre" y="sold" color="genre" />
</Chart>
</Canvas>
);
const canvas = new Canvas(props);
canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
});
});
});

0 comments on commit eb24656

Please sign in to comment.