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

docs: add custom legend example #2278

Merged
merged 1 commit into from
Dec 5, 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
80 changes: 80 additions & 0 deletions site/examples/statistics/column/demo/legend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Column } from '@ant-design/plots';
import React from 'react';
import ReactDOM from 'react-dom';

const data = [
{ type: '抖音', sold: 275 },
{ type: '快手', sold: 115 },
{ type: '小米', sold: 120 },
{ type: '微信', sold: 350 },
{ type: 'Keep', sold: 150 },
];

const DemoColumn = () => {
const logo = [
['抖音', 'https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*8IXHQLvx9QkAAAAAAAAAAAAADmJ7AQ/original'],
['快手', 'https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*swueRrrKvbcAAAAAAAAAAAAADmJ7AQ/original'],
['小米', 'https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*79G3TIt3mBoAAAAAAAAAAAAADmJ7AQ/original'],
['微信', 'https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*_ELBTJLp0dQAAAAAAAAAAAAADmJ7AQ/original'],
['Keep', 'https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*JzbKRpFhR14AAAAAAAAAAAAADmJ7AQ/original'],
];
const chartRef = React.useRef();
const config = {
data,
xField: 'type',
yField: 'sold',
colorField: 'type',
onReady: ({ chart }) => {
chartRef.current = chart;
},
legend: {
color: {
itemMarker: (name, index) => () => {
const chart = chartRef.current;
const { canvas } = chart.getContext();
const { document } = canvas;
window.c = chartRef.current;
const image = document.createElement('image', {
style: {
width: 20,
height: 20,
anchor: '0.5 0.5',
src: logo[index][1],
},
});
const tooltip = document.createElement('html', {
style: {
innerHTML: `<p style="color: red; width: 80; text-align: center;line-height: 30px">${name}</p>`,
fill: 'white',
stroke: '#ccc',
width: 80,
height: 30,
pointerEvents: 'none',
visibility: 'hidden',
},
});
canvas.appendChild(tooltip);
image.addEventListener('mousemove', (e) => {
tooltip.setPosition(e.x, e.y);
tooltip.style.visibility = 'visible';

console.log('move', e.target);
});
image.addEventListener('mouseleave', (e) => {
tooltip.setPosition(0, 0);
tooltip.style.visibility = 'hidden';

console.log('leave', e.target);
});
return image;
},
itemMarkerSize: 40,
itemLabelText: (_, index) => logo[index][0],
maxRows: 1,
},
},
};
return <Column {...config} />;
};

ReactDOM.render(<DemoColumn />, document.getElementById('container'));
8 changes: 8 additions & 0 deletions site/examples/statistics/column/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@
"en": "Customize tooltip"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*lZu1Q5vMEewAAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "legend.js",
"title": {
"zh": "自定义Legend",
"en": "Customize legend"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*FFVDR4pgMPgAAAAAAAAAAAAADmJ7AQ/original"
}
]
}