diff --git a/docs/SymptomsCausedByVaccines/index.html b/docs/SymptomsCausedByVaccines/index.html index d13d55058c5..235bbd3ae9a 100644 --- a/docs/SymptomsCausedByVaccines/index.html +++ b/docs/SymptomsCausedByVaccines/index.html @@ -31,6 +31,7 @@ + diff --git a/docs/SymptomsCausedByVaccines/js/PrrByKeyTable.js b/docs/SymptomsCausedByVaccines/js/PrrByKeyTable.js new file mode 100644 index 00000000000..2c925d93330 --- /dev/null +++ b/docs/SymptomsCausedByVaccines/js/PrrByKeyTable.js @@ -0,0 +1,107 @@ +class PrrByKeyTable { + + #tableElement; + #table; + #sumPrrs; + #prrByKey; + #keyColumnName; + #prrColumnName; + #shallMarkRowIfPrrTooHigh; + + constructor({ tableElement, keyColumnName, prrColumnName, shallMarkRowIfPrrTooHigh }) { + this.#tableElement = tableElement; + this.#keyColumnName = keyColumnName; + this.#prrColumnName = prrColumnName; + this.#shallMarkRowIfPrrTooHigh = shallMarkRowIfPrrTooHigh; + } + + initialize() { + this.#table = this.#createEmptyTable(); + } + + display(prrByKey) { + this.#prrByKey = prrByKey; + const key_prr_pairs = Object.entries(prrByKey); + this.#setTableRows(key_prr_pairs); + } + + getDisplayedTableAsCsv(heading) { + return PrrByKey2CsvConverter.convertPrrByKey2Csv( + { + heading: heading, + columns: { + keyColumn: this.#keyColumnName, + prrColumn: this.#prrColumnName + }, + prrByKey: this.#prrByKey + }); + } + + #createEmptyTable() { + return this.#tableElement.DataTable( + { + search: + { + return: false + }, + processing: true, + deferRender: true, + order: [[this.#getColumnIndex(this.#prrColumnName), "desc"]], + columnDefs: + [ + { + searchable: false, + targets: [this.#getColumnIndex(this.#prrColumnName)] + }, + { + render: prr => + NumberWithBarElementFactory + .createNumberWithBarElement( + { + number: prr, + barLenInPercent: prr / this.#sumPrrs * 100 + }) + .outerHTML, + targets: [this.#getColumnIndex(this.#prrColumnName)] + } + ], + createdRow: (row, data) => { + if (this.#shallMarkRowIfPrrTooHigh) { + this.#markRowIfPrrTooHigh( + { + prr: data[this.#getColumnIndex(this.#prrColumnName)], + row: row + }); + } + } + }); + } + + #markRowIfPrrTooHigh({ prr, row }) { + if (prr > 1.0) { + $(row).addClass('prrTooHigh'); + } + } + + #getColumnIndex(columnName) { + switch (columnName) { + case this.#keyColumnName: + return 0; + case this.#prrColumnName: + return 1; + } + } + + #setTableRows(key_prr_pairs) { + this.#sumPrrs = this.#getSumPrrs(key_prr_pairs); + this.#table + .clear() + .rows.add(key_prr_pairs) + .draw(); + } + + #getSumPrrs(key_prr_pairs) { + const prrs = key_prr_pairs.map(key_prr_pair => key_prr_pair[1]) + return Utils.sum(prrs); + } +} diff --git a/docs/SymptomsCausedByVaccines/js/PrrBySymptomTable.js b/docs/SymptomsCausedByVaccines/js/PrrBySymptomTable.js index 28268aef55a..103111d7048 100644 --- a/docs/SymptomsCausedByVaccines/js/PrrBySymptomTable.js +++ b/docs/SymptomsCausedByVaccines/js/PrrBySymptomTable.js @@ -1,86 +1,25 @@ class PrrBySymptomTable { - #tableElement; - #table; - #sumPrrs; - #prrBySymptom; + #delegate; constructor(tableElement) { - this.#tableElement = tableElement; + this.#delegate = new PrrByKeyTable({ + tableElement: tableElement, + keyColumnName: 'Symptom', + prrColumnName: 'Proportional Reporting Ratio > 1', + shallMarkRowIfPrrTooHigh: false + }); } initialize() { - this.#table = this.#createEmptyTable(); + this.#delegate.initialize(); } display(prrBySymptom) { - this.#prrBySymptom = prrBySymptom; - const symptom_prr_pairs = Object.entries(prrBySymptom); - this.#setTableRows(symptom_prr_pairs); + this.#delegate.display(prrBySymptom); } getDisplayedTableAsCsv(heading) { - return PrrByKey2CsvConverter.convertPrrByKey2Csv( - { - heading: heading, - columns: { - keyColumn: 'Symptom', - prrColumn: 'Proportional Reporting Ratio > 1' - }, - prrByKey: this.#prrBySymptom - }); - } - - #createEmptyTable() { - return this.#tableElement.DataTable( - { - search: - { - return: false - }, - processing: true, - deferRender: true, - order: [[this.#getColumnIndex('Proportional Reporting Ratio > 1'), "desc"]], - columnDefs: - [ - { - searchable: false, - targets: [this.#getColumnIndex('Proportional Reporting Ratio > 1')] - }, - { - render: prr => - NumberWithBarElementFactory - .createNumberWithBarElement( - { - number: prr, - barLenInPercent: prr / this.#sumPrrs * 100 - }) - .outerHTML, - targets: [this.#getColumnIndex('Proportional Reporting Ratio > 1')] - } - ] - }); - } - - #getColumnIndex(columnName) { - switch (columnName) { - case 'Symptom': - return 0; - case 'Proportional Reporting Ratio > 1': - return 1; - } - } - - #setTableRows(symptom_prr_pairs) { - this.#sumPrrs = this.#getSumPrrs(symptom_prr_pairs); - this.#table - .clear() - .rows.add(symptom_prr_pairs) - .draw(); - } - - #getSumPrrs(symptom_prr_pairs) { - const prrs = symptom_prr_pairs.map(symptom_prr_pair => symptom_prr_pair[1]) - return Utils.sum(prrs); + return this.#delegate.getDisplayedTableAsCsv(heading); } } diff --git a/docs/SymptomsCausedByVaccines/js/PrrByVaccineTable.js b/docs/SymptomsCausedByVaccines/js/PrrByVaccineTable.js index 637b79e5cf9..fac70832c31 100644 --- a/docs/SymptomsCausedByVaccines/js/PrrByVaccineTable.js +++ b/docs/SymptomsCausedByVaccines/js/PrrByVaccineTable.js @@ -1,99 +1,25 @@ class PrrByVaccineTable { - #tableElement; - #table; - #sumPrrs; - #prrByVaccine; + #delegate; constructor(tableElement) { - this.#tableElement = tableElement; + this.#delegate = new PrrByKeyTable({ + tableElement: tableElement, + keyColumnName: 'Vaccine', + prrColumnName: 'Proportional Reporting Ratio', + shallMarkRowIfPrrTooHigh: true + }); } initialize() { - this.#table = this.#createEmptyTable(); + this.#delegate.initialize(); } display(prrByVaccine) { - this.#prrByVaccine = prrByVaccine; - const vaccine_prr_pairs = Object.entries(prrByVaccine); - this.#setTableRows(vaccine_prr_pairs); + this.#delegate.display(prrByVaccine); } getDisplayedTableAsCsv(heading) { - return PrrByKey2CsvConverter.convertPrrByKey2Csv( - { - heading: heading, - columns: { - keyColumn: 'Vaccine', - prrColumn: 'Proportional Reporting Ratio' - }, - prrByKey: this.#prrByVaccine - }); - } - - #createEmptyTable() { - return this.#tableElement.DataTable( - { - search: - { - return: false - }, - processing: true, - deferRender: true, - order: [[this.#getColumnIndex('Proportional Reporting Ratio'), "desc"]], - columnDefs: - [ - { - searchable: false, - targets: [this.#getColumnIndex('Proportional Reporting Ratio')] - }, - { - render: prr => - NumberWithBarElementFactory - .createNumberWithBarElement( - { - number: prr, - barLenInPercent: prr / this.#sumPrrs * 100 - }) - .outerHTML, - targets: [this.#getColumnIndex('Proportional Reporting Ratio')] - } - ], - createdRow: (row, data) => { - this.#markRowIfPrrTooHigh( - { - prr: data[this.#getColumnIndex('Proportional Reporting Ratio')], - row: row - }); - } - }); - } - - #markRowIfPrrTooHigh({ prr, row }) { - if (prr > 1.0) { - $(row).addClass('prrTooHigh'); - } - } - - #getColumnIndex(columnName) { - switch (columnName) { - case 'Vaccine': - return 0; - case 'Proportional Reporting Ratio': - return 1; - } - } - - #setTableRows(vaccine_prr_pairs) { - this.#sumPrrs = this.#getSumPrrs(vaccine_prr_pairs); - this.#table - .clear() - .rows.add(vaccine_prr_pairs) - .draw(); - } - - #getSumPrrs(vaccine_prr_pairs) { - const prrs = vaccine_prr_pairs.map(vaccine_prr_pair => vaccine_prr_pair[1]) - return Utils.sum(prrs); + return this.#delegate.getDisplayedTableAsCsv(heading); } } diff --git a/test/SymptomsCausedByVaccines/index.test.html b/test/SymptomsCausedByVaccines/index.test.html index a775a4aa996..8f9b260d3ca 100644 --- a/test/SymptomsCausedByVaccines/index.test.html +++ b/test/SymptomsCausedByVaccines/index.test.html @@ -14,6 +14,7 @@ +