Skip to content

Commit 7462969

Browse files
committed
fix: make income query outside of income chart function thanks to tip from @mbostock
1 parent e8f27c9 commit 7462969

File tree

1 file changed

+8
-57
lines changed

1 file changed

+8
-57
lines changed

docs/income.md

+8-57
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ const selectedYear = Generators.input(yearInput);
1515
yearInput.querySelector("input[type=number]").remove();
1616
```
1717

18-
```js
19-
const mostRecentYear = uniqueYears[uniqueYears.length - 1];
20-
const income = await db.query(`
21-
SELECT income, count, sector FROM data
22-
WHERE year = ${mostRecentYear}
23-
`);
24-
```
25-
2618
```js
2719
// Order the sectors by mean income
2820
const mostRecentYear = uniqueYears[uniqueYears.length - 1];
@@ -36,20 +28,20 @@ const orderSectors = await db.query(`
3628
```
3729

3830
```js
39-
async function incomeChart(db, selectedYear, width) {
40-
const income = await db.query(`
31+
const income = db.query(`
4132
SELECT income, count, sector FROM data
4233
WHERE year = ${selectedYear}
43-
`);
44-
45-
console.log(income); // Should show the resolved array of data, not a promise
46-
console.log(orderSectors); // Should show an array of sector names
34+
`);
35+
```
4736

37+
```js
38+
function incomeChart(income, width) {
4839
// Create a histogram with a logarithmic base.
4940
return Plot.plot({
5041
width,
5142
marginLeft: 60,
5243
x: { type: "log" },
44+
y: { axis: null }, // Hide the y-axis
5345
color: { legend: "swatches", columns: 6, domain: orderSectors },
5446
marks: [
5547
Plot.rectY(
@@ -73,36 +65,6 @@ async function incomeChart(db, selectedYear, width) {
7365
}
7466
```
7567

76-
```js
77-
// Assuming yearInput is already defined and set up as shown in your code.
78-
79-
// Listen for changes on the yearInput to update the chart accordingly.
80-
yearInput.addEventListener('change', () => {
81-
// Retrieve the current selected year from the input.
82-
const selectedYear = yearInput.value;
83-
84-
// Call renderChart with the selected year.
85-
renderChart(selectedYear);
86-
});
87-
88-
// Call renderChart initially to render the chart for the first time with the most recent year or a default year.
89-
renderChart(yearInput.value);
90-
91-
function renderChart(selectedYear) {
92-
incomeChart(db, selectedYear, window.innerWidth * 0.9) // Adjust width as needed
93-
.then(chart => {
94-
const chartContainer = document.querySelector("#chartContainer");
95-
if (chartContainer) {
96-
chartContainer.innerHTML = ''; // Clear the container before appending new content
97-
chartContainer.appendChild(chart); // Append the chart
98-
} else {
99-
console.error("Chart container not found");
100-
}
101-
})
102-
.catch(error => console.error("Failed to render chart:", error));
103-
}
104-
```
105-
10668
<div class="card">
10769
<h2>The sectors in which people earn the most money have shifted over the past two decades</h2>
10870
<h3>How much income per year people reported earning in the 2000–2022 American Community Surveys, categorized by their sector of employment.</h3>
@@ -111,16 +73,5 @@ function renderChart(selectedYear) {
11173
<h1 style="margin-top: 0.5rem;">${selectedYear}</h1>
11274
${yearInput}
11375
</div>
114-
<div id="chartContainer"></div>
115-
</div>
116-
117-
118-
```js
119-
function renderChart(selectedYear) {
120-
incomeChart(db, selectedYear, window.innerWidth * 0.9) // Assuming you want to use 90% of the window width for the chart
121-
.then(chart => {
122-
document.querySelector("#chartContainer").appendChild(chart); // Append the new chart
123-
})
124-
.catch(error => console.error("Failed to render chart:", error));
125-
}
126-
```
76+
${resize((width) => incomeChart(income, width))}
77+
</div>

0 commit comments

Comments
 (0)