Skip to content

Commit

Permalink
Use the longest value in column to get column width in PDF report tab…
Browse files Browse the repository at this point in the history
…les (#3326)

* Use the longest value in column to get column width

* Added changelog

* Improved code readability
  • Loading branch information
asteriscos authored Jun 4, 2021
1 parent 608da10 commit c73c3dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ All notable changes to the Wazuh app project will be documented in this file.
- Fixed dark mode visualization background in pdf reports [#3315](https://github.com/wazuh/wazuh-kibana-app/pull/3315)
- Adapt Kibana integrations to Kibana 7.11 and 7.12 [#3309](https://github.com/wazuh/wazuh-kibana-app/pull/3309)
- Fixed error agent view does not render correctly [#3306](https://github.com/wazuh/wazuh-kibana-app/pull/3306)
- Fixed miscalculation in table column width in PDF reports [#3326](https://github.com/wazuh/wazuh-kibana-app/pull/3326)
- Normalized visData table property for 7.12 retro-compatibility [#3323](https://github.com/wazuh/wazuh-kibana-app/pull/3323)

## Wazuh v4.2.0 - Kibana 7.10.2 , 7.11.2 - Revision 4201
Expand Down
32 changes: 29 additions & 3 deletions server/lib/reporting/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,13 @@ export class ReportPrinter{
let totalLength = columns.length - 1;
const widthColumn = 385/totalLength;
let totalWidth = totalLength * widthColumn;
const widthCharacter = 5; //min width per character
const widths = [];

const widths:(number)[] = [];

for (let step = 0; step < columns.length - 1; step++) {
let columnLength = tableRows[0][step].text.length * widthCharacter;

let columnLength = this.getColumnWidth(columns[step], tableRows, step);

if (columnLength <= Math.round(totalWidth / totalLength)) {
widths.push(columnLength);
totalWidth -= columnLength;
Expand Down Expand Up @@ -615,4 +617,28 @@ export class ReportPrinter{
document.end();
}

/**
* Returns the width of a given column
*
* @param column
* @param tableRows
* @param step
* @returns {number}
*/
getColumnWidth(column, tableRows, index){
const widthCharacter = 5; //min width per character

//Get the longest row value
const maxRowLength = tableRows.reduce((maxLength, row)=>{
return (row[index].text.length > maxLength ? row[index].text.length : maxLength);
},0);

//Get column name length
const headerLength = column.label.length;

//Use the longest to get the column width
const maxLength = maxRowLength > headerLength ? maxRowLength : headerLength;

return maxLength * widthCharacter;
}
}

0 comments on commit c73c3dc

Please sign in to comment.