Export
yh-flow provides comprehensive export functionality, supporting the export of flowcharts to JSON, PNG, SVG and other formats.
Exporting JSON
Basic Usage
typescript
const flowRef = ref<FlowInstance>()
// Export as JSON string
const json = flowRef.value.exportJson()
console.log(json)Custom Export Data
Use exportFlowData to export complete data including viewport information:
typescript
import { exportFlowData } from '@yh-ui/flow'
const data = exportFlowData(nodes.value, edges.value, { x: 0, y: 0, zoom: 1 })
console.log(data) // JSON stringExporting Images
Export as PNG
typescript
const flowRef = ref<FlowInstance>()
// Export as PNG
await flowRef.value.exportImage?.({
imageType: 'png',
fileName: 'flow-chart',
download: true
})Export as JPEG
typescript
await flowRef.value.exportImage?.({
imageType: 'jpeg',
imageQuality: 0.9,
fileName: 'flow-chart',
download: true
})Export as SVG
SVG export allows exporting the flowchart as vector graphics:
typescript
// Get SVG string
const svgString = await captureFlowAsSvg(flowRef.value.$el)
// Or use plugin method
await flowRef.value.exportImage?.({
// Future version support
})Export Options
ScreenshotOptions
| Option | Type | Default | Description |
|---|---|---|---|
mode | 'viewport' | 'full' | 'viewport' | Export scope |
imageType | 'png' | 'jpeg' | 'webp' | 'png' | Image format |
imageQuality | number | 1 | Image quality (0-1) |
pixelRatio | number | 2 | Device pixel ratio |
backgroundColor | string | '#ffffff' | Background color |
fileName | string | 'flow-chart' | File name |
download | boolean | true | Trigger download |
copyToClipboard | boolean | false | Copy to clipboard |
fullModePadding | number | 20 | Full mode padding |
Export Modes
| Mode | Description |
|---|---|
viewport | Only export current visible area |
full | Export complete flowchart (adjusts viewport automatically) |
Complete Example
Export Toolbar Demo
Importing Data
Importing Data
Import from JSON
typescript
import { importFlowData } from '@yh-ui/flow'
const loadFromJson = (json: string) => {
const data = importFlowData(json)
if (data) {
nodes.value = data.nodes
edges.value = data.edges as unknown as Edge[]
if (data.viewport) {
viewport.value = data.viewport
}
}
}Best Practices
- Maintain data persistence: Regularly export JSON as backup for data recovery
- Quality settings: Choose appropriate image quality and format based on use case
- User feedback: Export operations may take time, consider adding loading state
- Clipboard functionality: Use
copyToClipboardin supported environments for better UX
TIP
Using mode: 'full' exports the complete flowchart even if the current viewport only shows partial content.