Exporting Options in SisenseJS
Published 03-17-2023
In case some of you want to download the widget as image here is how:
export const getImage = (dashboardId: string, widgetId: string|undefined, onReady: (blob: Blob) => void, onFail: (error: string) => void) => {
const xhr = new XMLHttpRequest();
const url = process.env.NEXT_PUBLIC_SISENSE_SERVER + (widgetId ? `/api/v1/export/dashboards/${dashboardId}/widgets/${widgetId}/png` : `/export/dashboards/${dashboardId}/png`);
xhr.open("POST", url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === HttpStatusCodes.OK_200) {
onReady(xhr.response);
} else {
onFail(xhr.statusText);
}
}
};
xhr.withCredentials = true; // This will be cross-domain request, so we force the browser to add cookies
xhr.responseType = "blob"; // This is to make the download easier. Use ArrayBuffer if visualizing
xhr.setRequestHeader('Accept', 'image/png');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(
widgetId ? `{
"params": {
"width": 0,
"height": 0,
"fitWidth": false
}
}` : `{
"params": {
"width": 0,
"layout": "asis",
"showDashboardTitle": true,
"showDashboardFilters": true,
"showDatasourceInfo": true
}
}`
);
}