@@ -15,6 +15,8 @@ import { isHorizontalAxis } from '../chart_types/xy_chart/utils/axis_utils';
1515import { Position } from '../chart_types/xy_chart/utils/specs' ;
1616import { CursorEvent } from '../specs/settings' ;
1717import { ChartSize , getChartSize } from '../utils/chart_size' ;
18+ import { Stage } from 'react-konva' ;
19+ import Konva from 'konva' ;
1820
1921interface ChartProps {
2022 /** The type of rendered
@@ -38,9 +40,11 @@ export class Chart extends React.Component<ChartProps, ChartState> {
3840 } ;
3941 private chartSpecStore : ChartStore ;
4042 private chartContainerRef : React . RefObject < HTMLDivElement > ;
43+ private chartStageRef : React . RefObject < Stage > ;
4144 constructor ( props : any ) {
4245 super ( props ) ;
4346 this . chartContainerRef = createRef ( ) ;
47+ this . chartStageRef = createRef ( ) ;
4448 this . chartSpecStore = new ChartStore ( props . id ) ;
4549 this . state = {
4650 legendPosition : this . chartSpecStore . legendPosition . get ( ) ,
@@ -91,6 +95,56 @@ export class Chart extends React.Component<ChartProps, ChartState> {
9195 }
9296 }
9397 }
98+
99+ getPNGSnapshot (
100+ options = {
101+ backgroundColor : 'transparent' ,
102+ pixelRatio : 2 ,
103+ } ,
104+ ) : {
105+ blobOrDataUrl : any ;
106+ browser : 'IE11' | 'other' ;
107+ } | null {
108+ if ( ! this . chartStageRef . current ) {
109+ return null ;
110+ }
111+ const stage = this . chartStageRef . current . getStage ( ) . clone ( ) ;
112+ const width = stage . getWidth ( ) ;
113+ const height = stage . getHeight ( ) ;
114+ const backgroundLayer = new Konva . Layer ( ) ;
115+ const backgroundRect = new Konva . Rect ( {
116+ fill : options . backgroundColor ,
117+ x : 0 ,
118+ y : 0 ,
119+ width,
120+ height,
121+ } ) ;
122+
123+ backgroundLayer . add ( backgroundRect ) ;
124+ stage . add ( backgroundLayer ) ;
125+ backgroundLayer . moveToBottom ( ) ;
126+ stage . draw ( ) ;
127+ const canvasStage = stage . toCanvas ( {
128+ width,
129+ height,
130+ callback : ( ) => { } ,
131+ } ) ;
132+ // @ts -ignore
133+ if ( canvasStage . msToBlob ) {
134+ // @ts -ignore
135+ const blobOrDataUrl = canvasStage . msToBlob ( ) ;
136+ return {
137+ blobOrDataUrl,
138+ browser : 'IE11' ,
139+ } ;
140+ } else {
141+ return {
142+ blobOrDataUrl : stage . toDataURL ( { pixelRatio : options . pixelRatio } ) ,
143+ browser : 'other' ,
144+ } ;
145+ }
146+ }
147+
94148 getChartContainerRef = ( ) => {
95149 return this . chartContainerRef ;
96150 } ;
@@ -119,8 +173,8 @@ export class Chart extends React.Component<ChartProps, ChartState> {
119173 < ChartResizer />
120174 < Crosshair />
121175 { // TODO reenable when SVG rendered is aligned with canvas one
122- renderer === 'svg' && < ChartContainer /> }
123- { renderer === 'canvas' && < ChartContainer /> }
176+ renderer === 'svg' && < ChartContainer forwardRef = { this . chartStageRef } /> }
177+ { renderer === 'canvas' && < ChartContainer forwardRef = { this . chartStageRef } /> }
124178 < Tooltips getChartContainerRef = { this . getChartContainerRef } />
125179 < AnnotationTooltip getChartContainerRef = { this . getChartContainerRef } />
126180 < Highlighter />
0 commit comments