Skip to content

Commit

Permalink
Update Download.jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
dobri1408 authored Oct 2, 2024
1 parent 77227ef commit c90bbfd
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions src/Utils/Download.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export default function Download(props) {
});
};

// Function to adjust the y position and set font styles

const adjustYPositionAndStyle = (textElement, newY) => {
textElement.setAttribute('y', newY);
const tspanElements = textElement.querySelectorAll('tspan');
Expand All @@ -180,14 +180,13 @@ export default function Download(props) {
tspan.setAttribute('fill', DEFAULT_FILL_COLOR);
let currentStyle = tspan.getAttribute('style') || '';

// Verifică dacă există deja o proprietate `fill` și o înlocuiește, sau adaugă dacă nu există
const updatedStyle = currentStyle.includes('fill')
? currentStyle.replace(/fill:\s*[^;]+/, `fill: ${DEFAULT_FILL_COLOR}`)
: `${currentStyle}; fill: ${DEFAULT_FILL_COLOR}`;

// Actualizează atributul `style` al elementului
console.log(updatedStyle);

tspan.setAttribute('style', updatedStyle);

});
const textElements = textElement.querySelectorAll('text');
textElements.forEach((tspan) => {
Expand All @@ -197,14 +196,14 @@ export default function Download(props) {
tspan.setAttribute('fill', DEFAULT_FILL_COLOR);
let currentStyle = tspan.getAttribute('style') || '';

// Verifică dacă există deja o proprietate `fill` și o înlocuiește, sau adaugă dacă nu există

const updatedStyle = currentStyle.includes('fill')
? currentStyle.replace(/fill:\s*[^;]+/, `fill: ${DEFAULT_FILL_COLOR}`)
: `${currentStyle}; fill: ${DEFAULT_FILL_COLOR}`;

// Actualizează atributul `style` al elementului
console.log(updatedStyle);

tspan.setAttribute('style', updatedStyle);

});
};

Expand All @@ -216,9 +215,8 @@ export default function Download(props) {
let totalTextHeight = 0;

const titleElement = allSvgs?.[1]?.querySelector('.g-gtitle');
const totalTitileHeight = titleElement.children?.[0]
? chartRef.current.querySelector('.g-gtitle').getBBox().height
: 0;
const totalTitileHeight = titleElement.children?.[0] ? chartRef.current.querySelector('.g-gtitle').getBBox().height : 0;


if (allSvgs.length > 0) {
const combinedSvg = document.createElementNS(
Expand All @@ -229,8 +227,7 @@ export default function Download(props) {
if (filters.length > 0) {
totalTextHeight =
START_DISTANCE +
filters.length * (DEFAULT_FONT_SIZE + PADDING_BETWEEN_TEXT) -
PADDING_BETWEEN_TEXT;
filters.length * (DEFAULT_FONT_SIZE + PADDING_BETWEEN_TEXT) - PADDING_BETWEEN_TEXT;
}

// Loop through each SVG and adjust layout
Expand All @@ -251,10 +248,8 @@ export default function Download(props) {
);

const originalY = svgClone.getAttribute('y') || 0;
const newY =
originalY +
filters.length * (DEFAULT_FONT_SIZE + PADDING_BETWEEN_TEXT) +
PADDING_BETWEEN_TEXT;
const newY = originalY +
filters.length * (DEFAULT_FONT_SIZE + PADDING_BETWEEN_TEXT) + PADDING_BETWEEN_TEXT
svgClone.setAttribute('y', newY);
if (svgWidth > maxWidth) maxWidth = svgWidth;
totalHeight = Math.max(svgHeight, totalHeight);
Expand Down Expand Up @@ -298,25 +293,18 @@ export default function Download(props) {
'http://www.w3.org/2000/svg',
'svg',
);
console.log(chartRef.current.querySelector('.g-gtitle').getBBox());

filters.forEach((filter, filterIndex) => {
const textElement = document.createElementNS(
'http://www.w3.org/2000/svg',
'text',
);
textElement.setAttribute(
'x',
titleElement.children?.[0]
? titleElement.children?.[0].getAttribute('x')
: '50%',
);
textElement.setAttribute('x', titleElement.children?.[0] ? titleElement.children?.[0].getAttribute('x') : '50%');
textElement.setAttribute(
'y',
START_DISTANCE +
totalTitileHeight +
PADDING_BETWEEN_TEXT +
filterIndex * (DEFAULT_FONT_SIZE + PADDING_BETWEEN_TEXT),
totalTitileHeight + PADDING_BETWEEN_TEXT +
filterIndex * (DEFAULT_FONT_SIZE + PADDING_BETWEEN_TEXT),
);
textElement.setAttribute('text-anchor', 'middle');
textElement.setAttribute('dominant-baseline', 'middle');
Expand All @@ -331,7 +319,7 @@ export default function Download(props) {

// Trigger download of final SVG or PNG
if (type === 'svg') {
downloadSVG(combinedSvg, `test.${type.toLowerCase()}`);
downloadSVG(combinedSvg, `${title}.${type.toLowerCase()}`);
} else if (type === 'png') {
downloadSVGAsPNG(combinedSvg, `${title}.${type.toLowerCase()}`);
}
Expand Down

0 comments on commit c90bbfd

Please sign in to comment.