-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feature: export chart to data url #1903
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
Open
eric-gitta-moore
wants to merge
13
commits into
swimlane:master
Choose a base branch
from
eric-gitta-moore:feature/export-image
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8b8ac4d
feat: charts to dataURL
028fe3e
feat: charts to png|jpg|svg
6957895
fix: Optimizing ts statements
09c03a6
fix: Rename getDataURL
fbc6261
fix: export pixel size too big
7a4a932
feat: export charts option transparent
0147ba5
feat: export charts option custom html element
f4e1f3d
fix: export charts option remove custom element node
1f62d26
style: bracket
bed84f4
style: bracket
0fc7972
style: Includes brackets to match coding style
3408aa5
fix: the viewBox for exporting combo chart svg is too small
fbf7e53
fix: advanced pie chart is misaligned
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
7 changes: 5 additions & 2 deletions
7
projects/swimlane/ngx-charts/src/lib/common/legend/advanced-legend.component.scss
This file contains hidden or 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 hidden or 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,111 @@ | ||
export interface Options { | ||
width: number; | ||
height: number; | ||
pixelRatio?: number; | ||
transparentBackground?: boolean; | ||
} | ||
|
||
export interface StyleAble extends Element { | ||
style: CSSStyleDeclaration; | ||
} | ||
|
||
function isType<T>(obj: unknown, test: (...args: any[]) => boolean): obj is T { | ||
return test(obj); | ||
} | ||
|
||
export function cloneNodeWithStyle<T extends Element>(originNode: T): T { | ||
const clonedNode = originNode.cloneNode(false) as T; | ||
|
||
if (isType<StyleAble>(clonedNode, e => e?.style)) { | ||
const computedStyle = window.getComputedStyle(originNode); | ||
const styleText = Array.from(computedStyle) | ||
.map(e => `${e}:${computedStyle.getPropertyValue(e)}`) | ||
.join(';'); | ||
clonedNode.style.cssText = styleText; | ||
} | ||
|
||
if (!(originNode instanceof Element)) return clonedNode; | ||
const children = Array.from(originNode.childNodes).map(cloneNodeWithStyle); | ||
clonedNode.append(...children.filter(e => !!e)); | ||
return clonedNode; | ||
} | ||
|
||
export function svgToDataURL(svg: SVGElement): Promise<string> { | ||
return Promise.resolve(new XMLSerializer().serializeToString(svg)) | ||
.then(encodeURIComponent) | ||
.then(html => `data:image/svg+xml;charset=utf-8,${html}`); | ||
} | ||
|
||
export function nodeToDataURL(node: Element, options: Options): Promise<string> { | ||
const { width, height } = options; | ||
const xmlns = 'http://www.w3.org/2000/svg'; | ||
const svg = document.createElementNS(xmlns, 'svg'); | ||
const foreignObject = document.createElementNS(xmlns, 'foreignObject'); | ||
|
||
svg.setAttribute('width', `${width}`); | ||
svg.setAttribute('height', `${height}`); | ||
svg.setAttribute('viewBox', `0 0 ${width} ${height}`); | ||
|
||
foreignObject.setAttribute('width', '100%'); | ||
foreignObject.setAttribute('height', '100%'); | ||
foreignObject.setAttribute('x', '0'); | ||
foreignObject.setAttribute('y', '0'); | ||
foreignObject.setAttribute('externalResourcesRequired', 'true'); | ||
|
||
svg.appendChild(foreignObject); | ||
foreignObject.appendChild(node); | ||
return svgToDataURL(svg); | ||
} | ||
|
||
export async function toSvg<T extends HTMLElement>(node: T, options: Options): Promise<string> { | ||
return nodeToDataURL(cloneNodeWithStyle(node), options); | ||
} | ||
|
||
export async function toPng<T extends HTMLElement>(node: T, options: Options): Promise<string> { | ||
const canvas = await toCanvas(node, options); | ||
return canvas.toDataURL(); | ||
} | ||
|
||
export async function toJpeg<T extends HTMLElement>(node: T, options: Options): Promise<string> { | ||
const canvas = await toCanvas(node, options); | ||
return canvas.toDataURL('image/jpeg'); | ||
} | ||
|
||
export function createImage(url: string): Promise<HTMLImageElement> { | ||
return new Promise((resolve, reject) => { | ||
const img = new Image(); | ||
img.onload = () => resolve(img); | ||
img.onerror = reject; | ||
img.crossOrigin = 'anonymous'; | ||
img.decoding = 'async'; | ||
img.src = url; | ||
img.decode().then(() => resolve(img)); | ||
}); | ||
} | ||
|
||
export async function toCanvas<T extends HTMLElement>(node: T, options: Options): Promise<HTMLCanvasElement> { | ||
const svg = await toSvg(node, options); | ||
const img = await createImage(svg); | ||
|
||
const canvas = document.createElement('canvas'); | ||
const context = canvas.getContext('2d')!; | ||
const ratio = options.pixelRatio || window.devicePixelRatio; | ||
const canvasWidth = options.width; | ||
const canvasHeight = options.height; | ||
|
||
canvas.width = canvasWidth * ratio; | ||
canvas.height = canvasHeight * ratio; | ||
|
||
canvas.style.width = `${canvasWidth}`; | ||
canvas.style.height = `${canvasHeight}`; | ||
|
||
if (!options.transparentBackground) { | ||
context.clearRect(0, 0, canvas.width, canvas.height); | ||
context.fillStyle = '#fff'; | ||
context.fillRect(0, 0, canvas.width, canvas.height); | ||
} | ||
|
||
context.drawImage(img, 0, 0, canvas.width, canvas.height); | ||
|
||
return canvas; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.