Skip to content

Commit e8f27c9

Browse files
committed
fix: debug rendering of chart into a specific container
1 parent 946545c commit e8f27c9

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

docs/income.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ async function incomeChart(db, selectedYear, width) {
4242
WHERE year = ${selectedYear}
4343
`);
4444

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
47+
4548
// Create a histogram with a logarithmic base.
4649
return Plot.plot({
4750
width,
@@ -70,6 +73,36 @@ async function incomeChart(db, selectedYear, width) {
7073
}
7174
```
7275

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+
73106
<div class="card">
74107
<h2>The sectors in which people earn the most money have shifted over the past two decades</h2>
75108
<h3>How much income per year people reported earning in the 2000–2022 American Community Surveys, categorized by their sector of employment.</h3>
@@ -78,5 +111,16 @@ async function incomeChart(db, selectedYear, width) {
78111
<h1 style="margin-top: 0.5rem;">${selectedYear}</h1>
79112
${yearInput}
80113
</div>
81-
${resize((width) => incomeChart(db, selectedYear, width))}
114+
<div id="chartContainer"></div>
82115
</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+
```

0 commit comments

Comments
 (0)