diff --git a/docs/HistogramTable.js b/docs/HistogramTable.js index 391b9ddd174..ec40d963820 100644 --- a/docs/HistogramTable.js +++ b/docs/HistogramTable.js @@ -2,6 +2,7 @@ class HistogramTable { #tableElement; #table; + #sumFrequencies; constructor(tableElement) { this.#tableElement = tableElement; @@ -12,8 +13,8 @@ class HistogramTable { } display(frequencyBySymptom) { - const symptom_frequency_arrays = Object.entries(frequencyBySymptom); - this.#setTableRows(symptom_frequency_arrays); + const symptom_frequency_pairs = Object.entries(frequencyBySymptom); + this.#setTableRows(symptom_frequency_pairs); } #createEmptyTable() { @@ -38,6 +39,17 @@ class HistogramTable { this.#getColumnIndex('Symptom') ] }, + { + render: frequency => + NumberWithBarElementFactory + .createNumberWithBarElement( + { + number: frequency, + barLenInPercent: frequency / this.#sumFrequencies * 100 + }) + .outerHTML, + targets: [this.#getColumnIndex('Frequency')] + } ] }); } @@ -51,10 +63,16 @@ class HistogramTable { } } - #setTableRows(rows) { + #setTableRows(symptom_frequency_pairs) { + this.#sumFrequencies = this.#getSumFrequencies(symptom_frequency_pairs); this.#table .clear() - .rows.add(rows) + .rows.add(symptom_frequency_pairs) .draw(); } + + #getSumFrequencies(symptom_frequency_pairs) { + const frequencies = symptom_frequency_pairs.map(symptom_frequency_pair => symptom_frequency_pair[1]) + return Utils.sum(frequencies); + } } diff --git a/docs/NumberWithBarElementFactory.js b/docs/NumberWithBarElementFactory.js new file mode 100644 index 00000000000..a2fffa6c4bc --- /dev/null +++ b/docs/NumberWithBarElementFactory.js @@ -0,0 +1,14 @@ +class NumberWithBarElementFactory { + + static createNumberWithBarElement({ number, barLenInPercent }) { + const numberWithBarElement = UIUtils.instantiateTemplate('template-number-with-bar'); + + const barElement = numberWithBarElement.querySelector('.bar'); + barElement.style.width = barLenInPercent.toString() + "%"; + + const numberElement = numberWithBarElement.querySelector('.number'); + numberElement.textContent = number; + + return numberWithBarElement; + } +} \ No newline at end of file diff --git a/docs/Utils.js b/docs/Utils.js index bae5ac4d891..e9e315a7ee7 100644 --- a/docs/Utils.js +++ b/docs/Utils.js @@ -7,6 +7,10 @@ class Utils { return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; } + static sum(numbers) { + return numbers.reduce((a, b) => a + b, 0); + } + static getKeysAlignedWithValues(dict) { const keys = []; const values = []; diff --git a/docs/batchCodeTable.html b/docs/batchCodeTable.html index d32ae3de14b..de3a8e2c333 100644 --- a/docs/batchCodeTable.html +++ b/docs/batchCodeTable.html @@ -27,6 +27,7 @@ + @@ -14937,6 +14938,12 @@
+ +