|
11 | 11 |
|
12 | 12 | let localCopyOfData = $state([...data]);
|
13 | 13 |
|
14 |
| - const metrics = Object.keys(localCopyOfData[0]).slice( |
15 |
| - 1, |
16 |
| - localCopyOfData[0].length, |
17 |
| - ); |
| 14 | + function hasUniqueValues(array, key) { |
| 15 | + const seen = new Set(); |
| 16 | + for (const obj of array) { |
| 17 | + if (seen.has(obj[key])) { |
| 18 | + return false; // Duplicate found |
| 19 | + } |
| 20 | + seen.add(obj[key]); |
| 21 | + } |
| 22 | + return true; // All values are unique |
| 23 | + } |
| 24 | +
|
| 25 | + // $inspect( |
| 26 | + // localCopyOfData[0].areaName, |
| 27 | + // "data type is", |
| 28 | + // typeof localCopyOfData[0].areaName, |
| 29 | + // ); |
| 30 | + // $inspect( |
| 31 | + // localCopyOfData[0]["Household waste recycling rate"], |
| 32 | + // "data type is", |
| 33 | + // typeof localCopyOfData[0]["Household waste recycling rate"], |
| 34 | + // ); |
| 35 | +
|
| 36 | + let columns = []; |
| 37 | +
|
| 38 | + for (const column in localCopyOfData[0]) { |
| 39 | + // create a variable to store whether the key is unique or not |
| 40 | + const keyIsUnique = hasUniqueValues(localCopyOfData, column); |
| 41 | + // get data type of each column |
| 42 | + const columnDataType = typeof localCopyOfData[0][column]; |
| 43 | + // for each one create an object and push it into the array |
| 44 | + const columnObject = { |
| 45 | + key: column, |
| 46 | + isUnique: keyIsUnique, |
| 47 | + dataType: columnDataType, |
| 48 | + }; |
| 49 | + columns.push(columnObject); |
| 50 | + } |
| 51 | +
|
| 52 | + $inspect("columns array is ", columns); |
| 53 | +
|
| 54 | + const metrics = columns |
| 55 | + .filter((column) => column.dataType === "number") |
| 56 | + .map((column) => column.key); |
| 57 | +
|
| 58 | + $inspect("metrics is", metrics); |
18 | 59 |
|
19 | 60 | let sortState = $state({ column: "sortedColumn", order: "ascending" });
|
20 | 61 |
|
|
82 | 123 | }
|
83 | 124 |
|
84 | 125 | const colorKey = Object.entries({ Good: 1, Ok: 0.5, Bad: 0 });
|
85 |
| -</script> |
86 | 126 |
|
87 |
| -{#snippet propNameAndValue(marginTW, paddingTW, text)} |
88 |
| - <span class="bg-slate-100 inline-block italic {marginTW} {paddingTW} rounded" |
89 |
| - >{text}</span |
90 |
| - > |
91 |
| -{/snippet} |
| 127 | + $inspect("the first column key is", columns[0].key); |
| 128 | +</script> |
92 | 129 |
|
93 | 130 | <div class="p-4">
|
94 | 131 | {#if colourScale === "On"}
|
|
107 | 144 | <caption class="govuk-table__caption">{caption}</caption>
|
108 | 145 | <thead class="govuk-table__head"
|
109 | 146 | ><tr class="govuk-table__row">
|
110 |
| - <th scope="col" class="govuk-table__header" aria-sort="ascending" |
111 |
| - >Area</th |
112 |
| - > |
113 |
| - {#each metrics as metric} |
| 147 | + {#each columns as column} |
114 | 148 | <th
|
115 | 149 | scope="col"
|
116 |
| - class="govuk-table__header govuk-table__header--numeric" |
117 |
| - title={metaData[metric].explainer} |
| 150 | + class={`govuk-table__header ${column.dataType === "number" ? "govuk-table__header--numeric" : ""}`} |
| 151 | + title={metaData[column.key].explainer} |
118 | 152 | aria-sort="none"
|
119 | 153 | >
|
120 | 154 | <div class="header">
|
121 | 155 | <Button
|
122 |
| - textContent={metaData[metric].shortLabel} |
| 156 | + textContent={metaData[column.key].shortLabel} |
123 | 157 | buttonType={"table header"}
|
124 | 158 | onClickFunction={() => {
|
125 | 159 | const newDirection =
|
126 |
| - sortState.column === metric && |
| 160 | + sortState.column === column.key && |
127 | 161 | sortState.order === "ascending"
|
128 | 162 | ? "descending"
|
129 | 163 | : "ascending";
|
130 | 164 |
|
131 |
| - updateSortState(metric, newDirection); |
| 165 | + updateSortState(column.key, newDirection); |
132 | 166 | sortFunction();
|
133 | 167 | }}
|
134 | 168 | ></Button>
|
|
140 | 174 | <tbody class="govuk-table__body">
|
141 | 175 | {#each localCopyOfData as row}
|
142 | 176 | <tr class="govuk-table__row">
|
143 |
| - <td class="govuk-table__cell">{row["areaName"]}</td> |
144 |
| - {#each metrics as metric} |
145 |
| - {#if colourScale === "On"} |
146 |
| - {#if metaData[metric].direction === "Higher is better"} |
147 |
| - <td |
148 |
| - class="govuk-table__cell govuk-table__cell--numeric" |
149 |
| - style="background-color: {normToColor( |
150 |
| - row[metric + '__normalised'], |
151 |
| - )}" |
152 |
| - data-sort-value="42">{row[metric]}</td |
153 |
| - > |
| 177 | + {#each columns as column} |
| 178 | + {#if column.dataType === "number"} |
| 179 | + {#if colourScale === "On"} |
| 180 | + {#if metaData[column.key].direction === "Higher is better"} |
| 181 | + <td |
| 182 | + class="govuk-table__cell govuk-table__cell--numeric" |
| 183 | + style="background-color: {normToColor( |
| 184 | + row[column.key + '__normalised'], |
| 185 | + )}" |
| 186 | + data-sort-value="42">{row[column.key]}</td |
| 187 | + > |
| 188 | + {:else} |
| 189 | + <td |
| 190 | + class="govuk-table__cell govuk-table__cell--numeric" |
| 191 | + style="background-color: {normToColorReverse( |
| 192 | + row[column.key + '__normalised'], |
| 193 | + )}" |
| 194 | + data-sort-value="42">{row[column.key]}</td |
| 195 | + > |
| 196 | + {/if} |
154 | 197 | {:else}
|
155 | 198 | <td
|
156 | 199 | class="govuk-table__cell govuk-table__cell--numeric"
|
157 |
| - style="background-color: {normToColorReverse( |
158 |
| - row[metric + '__normalised'], |
159 |
| - )}" |
160 |
| - data-sort-value="42">{row[metric]}</td |
| 200 | + data-sort-value="42">{row[column.key]}</td |
161 | 201 | >
|
162 | 202 | {/if}
|
163 | 203 | {:else}
|
164 |
| - <td |
165 |
| - class="govuk-table__cell govuk-table__cell--numeric" |
166 |
| - data-sort-value="42">{row[metric]}</td |
167 |
| - > |
| 204 | + <td class="govuk-table__cell">{row[column.key]}</td> |
168 | 205 | {/if}
|
169 | 206 | {/each}
|
170 | 207 | </tr>
|
|
0 commit comments