forked from eosphoros-ai/DB-GPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): add download image function&fix null data point (eosphoros…
- Loading branch information
Showing
4 changed files
with
97 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ChartRef as G2Chart } from "@berryv/g2-react"; | ||
|
||
const getChartCanvas = (chart: G2Chart) => { | ||
if (!chart) return; | ||
const chartContainer = chart.getContainer(); | ||
const canvasNode = chartContainer.getElementsByTagName('canvas')[0]; | ||
return canvasNode | ||
} | ||
|
||
/** 获得 g2 Chart 实例的 dataURL */ | ||
function toDataURL(chart: G2Chart) { | ||
const canvasDom = getChartCanvas(chart); | ||
if (canvasDom) { | ||
const dataURL = canvasDom.toDataURL('image/png'); | ||
return dataURL; | ||
} | ||
} | ||
|
||
/** | ||
* 图表图片导出 | ||
* @param chart chart 实例 | ||
* @param name 图片名称 | ||
*/ | ||
export function downloadImage(chart: G2Chart, name: string = 'Chart') { | ||
const link = document.createElement('a'); | ||
const filename = `${name}.png`; | ||
|
||
setTimeout(() => { | ||
const dataURL = toDataURL(chart); | ||
if (dataURL) { | ||
link.addEventListener('click', () => { | ||
link.download = filename; | ||
link.href = dataURL; | ||
}); | ||
const e = document.createEvent('MouseEvents'); | ||
e.initEvent('click', false, false); | ||
link.dispatchEvent(e); | ||
} | ||
}, 16); | ||
} |