@@ -42,6 +42,9 @@ async function incomeChart(db, selectedYear, width) {
42
42
WHERE year = ${ selectedYear}
43
43
` );
44
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
47
+
45
48
// Create a histogram with a logarithmic base.
46
49
return Plot .plot ({
47
50
width,
@@ -70,6 +73,36 @@ async function incomeChart(db, selectedYear, width) {
70
73
}
71
74
```
72
75
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
+
73
106
<div class =" card " >
74
107
<h2 >The sectors in which people earn the most money have shifted over the past two decades</h2 >
75
108
<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) {
78
111
<h1 style =" margin-top : 0.5rem ;" >${selectedYear}</h1 >
79
112
${yearInput}
80
113
</div >
81
- ${resize((width) => incomeChart(db, selectedYear, width))}
114
+ < div id = " chartContainer " ></ div >
82
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
+ ```
0 commit comments