@@ -15,14 +15,6 @@ const selectedYear = Generators.input(yearInput);
15
15
yearInput .querySelector (" input[type=number]" ).remove ();
16
16
```
17
17
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
-
26
18
``` js
27
19
// Order the sectors by mean income
28
20
const mostRecentYear = uniqueYears[uniqueYears .length - 1 ];
@@ -36,20 +28,20 @@ const orderSectors = await db.query(`
36
28
```
37
29
38
30
``` js
39
- async function incomeChart (db , selectedYear , width ) {
40
- const income = await db .query (`
31
+ const income = db .query (`
41
32
SELECT income, count, sector FROM data
42
33
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
+ ```
47
36
37
+ ``` js
38
+ function incomeChart (income , width ) {
48
39
// Create a histogram with a logarithmic base.
49
40
return Plot .plot ({
50
41
width,
51
42
marginLeft: 60 ,
52
43
x: { type: " log" },
44
+ y: { axis: null }, // Hide the y-axis
53
45
color: { legend: " swatches" , columns: 6 , domain: orderSectors },
54
46
marks: [
55
47
Plot .rectY (
@@ -73,36 +65,6 @@ async function incomeChart(db, selectedYear, width) {
73
65
}
74
66
```
75
67
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
-
106
68
<div class =" card " >
107
69
<h2 >The sectors in which people earn the most money have shifted over the past two decades</h2 >
108
70
<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) {
111
73
<h1 style =" margin-top : 0.5rem ;" >${selectedYear}</h1 >
112
74
${yearInput}
113
75
</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