Gauge print and export in React Circular gauge component
24 Jan 202311 minutes to read
To use the print functionality, we should inject the Print
module into services
and set the allowPrint
property to true. The rendered circular gauge can be printed directly from the browser by calling the method print
.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { CircularGaugeComponent, Print, Inject } from '@syncfusion/ej2-react-circulargauge';
export function App() {
var gaugeInstance;
function clickHandler() {
gaugeInstance.print();
}
return (<div>
<ButtonComponent onClick={clickHandler}>
print
</ButtonComponent>
<CircularGaugeComponent
id="circulargauge"
allowPrint={true}
ref={(g) => (gaugeInstance = g)}
>
<Inject services={[Print]} />
</CircularGaugeComponent>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { CircularGaugeComponent, Print, Inject } from '@syncfusion/ej2-react-circulargauge';
export function App() {
let gaugeInstance : CircularGaugeComponent;
function clickHandler() {
gaugeInstance.print();
}
return (<div>
<ButtonComponent onClick={clickHandler}>
print
</ButtonComponent>
<CircularGaugeComponent
id="circulargauge"
allowPrint={true}
ref={(g) => (gaugeInstance = g)}
>
<Inject services={[Print]} />
</CircularGaugeComponent>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Export
Image Export
To use the image export functionality, we should inject the ImageExport
module into services
and set the allowImageExport
property to true. The rendered circular gauge can be exported as an image using the export
method. The method requires two parameters: image type and file name. The circular gauge can be exported as an image in the following formats.
- JPEG
- PNG
- SVG
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { CircularGaugeComponent, ImageExport, Inject } from '@syncfusion/ej2-react-circulargauge';
export function App() {
var gaugeInstance;
function clickHandler(){
gaugeInstance.export('PNG','Gauge');
}
return (<div>
<ButtonComponent onClick= {clickHandler}>Export</ButtonComponent>
<CircularGaugeComponent allowImageExport={true} ref={g => gaugeInstance = g}>
<Inject services={[ImageExport]} />
</CircularGaugeComponent>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { CircularGaugeComponent, ImageExport, Inject } from '@syncfusion/ej2-react-circulargauge';
export function App() {
let gaugeInstance : CircularGaugeComponent;
function clickHandler() {
gaugeInstance.export('PNG','Gauge');
}
return (<div>
<ButtonComponent onClick= {clickHandler}>Export</ButtonComponent>
<CircularGaugeComponent allowImageExport={true} ref={g => gaugeInstance = g}>
<Inject services={[ImageExport]} />
</CircularGaugeComponent>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
We can get the image file as base64 string for the JPEG and PNG formats. The circular gauge can be exported to image as a base64 string using the export
method. There are four parameters required: image type, file name, orientation of the exported PDF document which must be set as null for image export and finally allowDownload which should be set as false to return base64 string.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { PdfPageOrientation } from '@syncfusion/ej2-pdf-export';
import { CircularGaugeComponent, ImageExport, Inject } from '@syncfusion/ej2-react-circulargauge';
export function App() {
var gaugeInstance;
function clickHandler(){
gaugeInstance.export('PNG', 'Gauge', PdfPageOrientation.Landscape, false).then((data)=>{
document.writeln(data);
})
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<CircularGaugeComponent allowImageExport={true} ref={g => gaugeInstance = g}>
<Inject services={[ImageExport]} />
</CircularGaugeComponent>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { CircularGaugeComponent, ImageExport, Inject } from '@syncfusion/ej2-react-circulargauge';
export function App() {
let gaugeInstance: CircularGaugeComponent;
function clickHandler(){
gaugeInstance.export('PNG', 'Gauge', null, false).then((data)=>{
document.writeln(data);
})
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<CircularGaugeComponent allowImageExport={true} ref={g => gaugeInstance = g}>
<Inject services={[ImageExport]} />
</CircularGaugeComponent>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
PDF Export
To use the PDF export functionality, we should inject the PdfExport
module into services
and set the allowPdfExport
property to true. The rendered circular gauge can be exported as PDF using the export
method. The export
method requires three parameters: file type, file name and orientation of the PDF document. The orientation setting is optional and “0” indicates portrait and “1” indicates landscape.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { CircularGaugeComponent, PdfExport, Inject } from '@syncfusion/ej2-react-circulargauge';
export function App() {
var gaugeInstance;
function clickHandler(){
gaugeInstance.export('PDF','Gauge', 0);
}
return (<div>
<ButtonComponent onClick= {clickHandler}>Export</ButtonComponent>
<CircularGaugeComponent allowPdfExport={true} ref={g => gaugeInstance = g}>
<Inject services={[PdfExport]} />
</CircularGaugeComponent>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { CircularGaugeComponent, PdfExport, Inject } from '@syncfusion/ej2-react-circulargauge';
export function App() {
let gaugeInstance : CircularGaugeComponent;
function clickHandler(){
gaugeInstance.export('PDF','Gauge', 0);
}
return (<div>
<ButtonComponent onClick= {clickHandler}>Export</ButtonComponent>
<CircularGaugeComponent allowPdfExport={true} ref={g => gaugeInstance = g}>
<Inject services={[PdfExport]} />
</CircularGaugeComponent>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Note: The exporting of the circular gauge as base64 string is not supported in the PDF export.