Skip to content

Commit 5a77fdb

Browse files
author
Miles Hinchliffe
committed
Render all columns together rather than area first then metrics
1 parent d072ab1 commit 5a77fdb

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

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

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,30 @@
2222
return true; // All values are unique
2323
}
2424
25-
$inspect(localCopyOfData[0]);
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+
// );
2635
2736
let columns = [];
2837
29-
for (const key in localCopyOfData[0]) {
38+
for (const column in localCopyOfData[0]) {
3039
// create a variable to store whether the key is unique or not
31-
const keyIsUnique = hasUniqueValues(localCopyOfData, key);
32-
console.log(keyIsUnique);
33-
40+
const keyIsUnique = hasUniqueValues(localCopyOfData, column);
41+
// get data type of each column
42+
const columnDataType = typeof localCopyOfData[0][column];
3443
// for each one create an object and push it into the array
35-
const columnObject = { key: key, isUnique: keyIsUnique };
44+
const columnObject = {
45+
key: column,
46+
isUnique: keyIsUnique,
47+
dataType: columnDataType,
48+
};
3649
columns.push(columnObject);
3750
}
3851
@@ -43,6 +56,8 @@
4356
localCopyOfData[0].length,
4457
);
4558
59+
$inspect("metrics is", metrics);
60+
4661
let sortState = $state({ column: "sortedColumn", order: "ascending" });
4762
4863
function updateSortState(columnToSort, sortOrder) {
@@ -109,13 +124,9 @@
109124
}
110125
111126
const colorKey = Object.entries({ Good: 1, Ok: 0.5, Bad: 0 });
112-
</script>
113127
114-
{#snippet propNameAndValue(marginTW, paddingTW, text)}
115-
<span class="bg-slate-100 inline-block italic {marginTW} {paddingTW} rounded"
116-
>{text}</span
117-
>
118-
{/snippet}
128+
$inspect("the first column key is", columns[0].key);
129+
</script>
119130

120131
<div class="p-4">
121132
{#if colourScale === "On"}
@@ -134,28 +145,28 @@
134145
<caption class="govuk-table__caption">{caption}</caption>
135146
<thead class="govuk-table__head"
136147
><tr class="govuk-table__row">
137-
<th scope="col" class="govuk-table__header" aria-sort="ascending"
148+
<!-- <th scope="col" class="govuk-table__header" aria-sort="ascending"
138149
>Area</th
139-
>
140-
{#each metrics as metric}
150+
> -->
151+
{#each columns as column}
141152
<th
142153
scope="col"
143154
class="govuk-table__header govuk-table__header--numeric"
144-
title={metaData[metric].explainer}
155+
title={metaData[column.key].explainer}
145156
aria-sort="none"
146157
>
147158
<div class="header">
148159
<Button
149-
textContent={metaData[metric].shortLabel}
160+
textContent={metaData[column.key].shortLabel}
150161
buttonType={"table header"}
151162
onClickFunction={() => {
152163
const newDirection =
153-
sortState.column === metric &&
164+
sortState.column === column.key &&
154165
sortState.order === "ascending"
155166
? "descending"
156167
: "ascending";
157168

158-
updateSortState(metric, newDirection);
169+
updateSortState(column.key, newDirection);
159170
sortFunction();
160171
}}
161172
></Button>
@@ -167,31 +178,34 @@
167178
<tbody class="govuk-table__body">
168179
{#each localCopyOfData as row}
169180
<tr class="govuk-table__row">
170-
<td class="govuk-table__cell">{row["areaName"]}</td>
171-
{#each metrics as metric}
172-
{#if colourScale === "On"}
173-
{#if metaData[metric].direction === "Higher is better"}
174-
<td
175-
class="govuk-table__cell govuk-table__cell--numeric"
176-
style="background-color: {normToColor(
177-
row[metric + '__normalised'],
178-
)}"
179-
data-sort-value="42">{row[metric]}</td
180-
>
181+
{#each columns as column}
182+
{#if column.dataType === "number"}
183+
{#if colourScale === "On"}
184+
{#if metaData[column.key].direction === "Higher is better"}
185+
<td
186+
class="govuk-table__cell govuk-table__cell--numeric"
187+
style="background-color: {normToColor(
188+
row[column.key + '__normalised'],
189+
)}"
190+
data-sort-value="42">{row[column.key]}</td
191+
>
192+
{:else}
193+
<td
194+
class="govuk-table__cell govuk-table__cell--numeric"
195+
style="background-color: {normToColorReverse(
196+
row[column.key + '__normalised'],
197+
)}"
198+
data-sort-value="42">{row[column.key]}</td
199+
>
200+
{/if}
181201
{:else}
182202
<td
183203
class="govuk-table__cell govuk-table__cell--numeric"
184-
style="background-color: {normToColorReverse(
185-
row[metric + '__normalised'],
186-
)}"
187-
data-sort-value="42">{row[metric]}</td
204+
data-sort-value="42">{row[column.key]}</td
188205
>
189206
{/if}
190207
{:else}
191-
<td
192-
class="govuk-table__cell govuk-table__cell--numeric"
193-
data-sort-value="42">{row[metric]}</td
194-
>
208+
<td class="govuk-table__cell">{row[column.key]}</td>
195209
{/if}
196210
{/each}
197211
</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)