Skip to content

Commit 529b020

Browse files
author
Miles Hinchliffe
committed
Merge branch 'table-generalise-input-data' into main-check-merge
2 parents a4c3e1b + 0baca70 commit 529b020

File tree

2 files changed

+81
-38
lines changed

2 files changed

+81
-38
lines changed

src/lib/components/data-vis/table/Table.svelte

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,51 @@
1111
1212
let localCopyOfData = $state([...data]);
1313
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);
1859
1960
let sortState = $state({ column: "sortedColumn", order: "ascending" });
2061
@@ -82,13 +123,9 @@
82123
}
83124
84125
const colorKey = Object.entries({ Good: 1, Ok: 0.5, Bad: 0 });
85-
</script>
86126
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>
92129

93130
<div class="p-4">
94131
{#if colourScale === "On"}
@@ -107,28 +144,25 @@
107144
<caption class="govuk-table__caption">{caption}</caption>
108145
<thead class="govuk-table__head"
109146
><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}
114148
<th
115149
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}
118152
aria-sort="none"
119153
>
120154
<div class="header">
121155
<Button
122-
textContent={metaData[metric].shortLabel}
156+
textContent={metaData[column.key].shortLabel}
123157
buttonType={"table header"}
124158
onClickFunction={() => {
125159
const newDirection =
126-
sortState.column === metric &&
160+
sortState.column === column.key &&
127161
sortState.order === "ascending"
128162
? "descending"
129163
: "ascending";
130164

131-
updateSortState(metric, newDirection);
165+
updateSortState(column.key, newDirection);
132166
sortFunction();
133167
}}
134168
></Button>
@@ -140,31 +174,34 @@
140174
<tbody class="govuk-table__body">
141175
{#each localCopyOfData as row}
142176
<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}
154197
{:else}
155198
<td
156199
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
161201
>
162202
{/if}
163203
{: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>
168205
{/if}
169206
{/each}
170207
</tr>

static/data/testData.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52545,6 +52545,12 @@
5254552545
},
5254652546
"metaData" : {
5254752547

52548+
"areaName": {
52549+
"direction": "none",
52550+
"explainer": "Local authorities",
52551+
"label": "Local Authority",
52552+
"shortLabel": "Local Authority"
52553+
},
5254852554
"Household waste recycling rate": {
5254952555
"direction": "Higher is better",
5255052556
"explainer": "Percentage of household waste sent for recycling.",

0 commit comments

Comments
 (0)