BizCharts中将图表保存成图片
import { Chart, Interval, Tooltip } from 'bizcharts';
import html2canvas from 'html2canvas';
export const List1 = (props: any) => {
const data = [
{ year: '1951 年', sales: 0 },
{ year: '1952 年', sales: 52 },
{ year: '1956 年', sales: 61 },
{ year: '1957 年', sales: 45 },
{ year: '1958 年', sales: 48 },
{ year: '1959 年', sales: 38 },
{ year: '1960 年', sales: 38 },
{ year: '1962 年', sales: 38 },
];
const download = () => {
html2canvas(document.getElementById('download') as any).then(function (
canvas: any,
) {
var img = canvas
.toDataURL('image/png')
.replace('image/png', 'image/octet-stream');
var creatIMg = document.createElement('a');
creatIMg.download = '图表.png';
creatIMg.href = img;
document.body.appendChild(creatIMg);
creatIMg.click();
creatIMg.remove();
});
};
return(
<div style={{ width: '500px' }} id="download">
<Chart height={300} autoFit data={data}>
<Interval position="year*sales" />
<Tooltip shared />
</Chart>
</div>
<Button onClick={download}>download</Button>
)
}