Skip to content

[frontend] fix dashboard export to png when containing donuts (#10786) #11009

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions opencti-platform/opencti-front/src/components/ExportButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Tooltip from '@mui/material/Tooltip';
import ToggleButton from '@mui/material/ToggleButton';
import themeLight from './ThemeLight';
import themeDark from './ThemeDark';
import { commitLocalUpdate } from '../relay/environment';
import { commitLocalUpdate, MESSAGING$ } from '../relay/environment';
import { exportImage, exportPdf } from '../utils/Image';
import inject18n from './i18n';
import Loader from './Loader';
Expand Down Expand Up @@ -104,6 +104,7 @@ class ExportButtons extends Component {
}

handleOpenPdf(event) {
console.log('ANGIE - handleOpenPdf');
this.setState({ anchorElPdf: event.currentTarget });
}

Expand All @@ -112,9 +113,10 @@ class ExportButtons extends Component {
}

exportPdf(domElementId, name, theme, background) {
console.log('ANGIE - exportPdf start');
this.setState({ exporting: true });
this.handleClosePdf();
const { theme: currentTheme, pixelRatio = 1 } = this.props;
const { theme: currentTheme, pixelRatio = 1, t } = this.props;
let timeout = 4000;
if (theme !== currentTheme.palette.mode) {
timeout = 6000;
Expand All @@ -124,9 +126,11 @@ class ExportButtons extends Component {
me.setValue(theme, 'theme');
});
}
console.log('ANGIE - exportPdf -1-');
setTimeout(() => {
const buttons = document.getElementById('export-buttons');
buttons.setAttribute('style', 'display: none');
console.log('ANGIE - exportPdf -2-');
exportPdf(
domElementId,
name,
Expand All @@ -139,14 +143,17 @@ class ExportButtons extends Component {
pixelRatio,
this.adjust,
).then(() => {
console.log('ANGIE - exportPdf -3- then');
buttons.setAttribute('style', 'display: block');
commitLocalUpdate((store) => {
const me = store.getRoot().getLinkedRecord('me');
me.setValue(false, 'exporting');
me.setValue(currentTheme.palette.mode, 'theme');
}).catch(() => MESSAGING$.notifyError(t('Dashboard cannot be exported')))
.finally(() => {
commitLocalUpdate((store) => {
const me = store.getRoot().getLinkedRecord('me');
me.setValue(false, 'exporting');
me.setValue(currentTheme.palette.mode, 'theme');
});
this.setState({ exporting: false });
});
this.setState({ exporting: false });
});
}, timeout);
}

Expand Down
2 changes: 1 addition & 1 deletion opencti-platform/opencti-front/src/utils/Charts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ export const donutChartOptions = (
legendPosition = 'bottom',
reversed = false,
chartColors = [],
displayLegend = true,
displayLegend = false,
displayLabels = true,
displayValue = true,
displayTooltip = true,
Expand Down
13 changes: 12 additions & 1 deletion opencti-platform/opencti-front/src/utils/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ export const exportPdf = (
pixelRatio = 1,
adjust = null,
) => {
console.log('ANGIE - exportPdf in image start');
const container = document.getElementById(domElementId);
const { offsetWidth, offsetHeight } = container;
const imageWidth = offsetWidth * pixelRatio;
const imageHeight = offsetHeight * pixelRatio;
return new Promise((resolve) => {
console.log('ANGIE - exportPdf in image params', { offsetWidth, offsetHeight, imageWidth, imageHeight, backgroundColor, name, adjust, container, pixelRatio });
return new Promise((resolve, reject) => {
console.log(`ANGIE - exportPdf in image htmlToImage start container:${container.className}`);
console.log('ANGIE - exportPdf in image params 2', { offsetWidth, offsetHeight, imageWidth, imageHeight, backgroundColor, name, adjust, container, pixelRatio });
htmlToImage
.toPng(container, {
useCORS: true,
Expand All @@ -83,6 +87,7 @@ export const exportPdf = (
return true;
},
})
.then((truc) => { console.log('ANGIE truc:', truc); return truc; })
.then((image) => {
const docDefinition = {
pageSize: {
Expand Down Expand Up @@ -111,15 +116,21 @@ export const exportPdf = (
},
],
};
console.log('ANGIE - exportPdf before createPdf');
const pdf = pdfMake.createPdf(docDefinition);
pdf.download(`${name}.pdf`);
if (adjust) {
console.log('ANGIE - exportPdf adjust');
container.setAttribute(
'style',
`width:${offsetWidth}px; height:${offsetHeight}px;`,
);
}
resolve();
})
.catch((reason) => {
console.log('ANGIE error in exportPdf in image', JSON.stringify(reason));
reject(reason);
});
});
};
Expand Down