Skip to content

Commit b3fceea

Browse files
committed
Fix y axis bug bar chart
1 parent 9dbf24e commit b3fceea

File tree

2 files changed

+71
-30
lines changed

2 files changed

+71
-30
lines changed

src/js/graphs/chart.js

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function initBarChart(completeData, country, year) {
3636
.attr('transform', `translate(0, ${graphHeight})`);
3737

3838
// Append the y-axis to the bar chart
39-
const yAxisGroup = graph.append('g');
39+
const yAxisGroup = graph.append('g').attr('class', 'y-axis');
4040

4141
// Set the domain of the y-axis
4242
y = d3.scaleLinear().range([graphHeight, 0]);
@@ -73,31 +73,28 @@ export function initBarChart(completeData, country, year) {
7373

7474
// Initialze the data for the bar chart, selecting the appropriate year and country
7575
let data = completeData[year][country];
76+
let graphData;
7677

7778
// Set the value of the bars to zero if the data of a particular year is not available in the dataset
7879
if (data === undefined) {
79-
data = {
80-
Country: country,
81-
Region: '',
80+
graphData = {
8281
'Economy (GDP per Capita)': 0,
8382
'Freedom to make life choices': 0,
8483
Generosity: 0,
85-
'Happiness Rank': 0,
86-
'Happiness Score': 0,
8784
'Healthy life expectancy': 0,
8885
'Trust (Government Corruption)': 0,
8986
};
87+
} else {
88+
// Assign the data to seperate categories
89+
graphData = {
90+
'Economy (GDP per Capita)': data['Economy (GDP per Capita)'],
91+
'Freedom to make life choices': data['Freedom to make life choices'],
92+
Generosity: data['Generosity'],
93+
'Healthy life expectancy': data['Healthy life expectancy'],
94+
'Trust (Government Corruption)': data['Trust (Government Corruption)'],
95+
};
9096
}
9197

92-
// Assign the data to seperate categories
93-
const graphData = {
94-
'Economy (GDP per Capita)': data['Economy (GDP per Capita)'],
95-
'Freedom to make life choices': data['Freedom to make life choices'],
96-
Generosity: data['Generosity'],
97-
'Healthy life expectancy': data['Healthy life expectancy'],
98-
'Trust (Government Corruption)': data['Trust (Government Corruption)'],
99-
};
100-
10198
// Set the name of a bar equal to the corresponding category
10299
const barsKeys = Object.keys(graphData);
103100
// Set the value of a bar equal to the corresponding category
@@ -192,34 +189,59 @@ export function initBarChart(completeData, country, year) {
192189

193190
// Update the bar charts based on the selected country and year
194191
export function updateBarChartData(completeData, year, country) {
192+
console.log('Y-axis: ', graph.selectAll('.x-axis'));
193+
graph.selectAll('.y-axis').remove();
194+
195+
// Append the x-axis to the bar chart, translate the x-axis to the origin
196+
const xAxisGroup = graph
197+
.append('g')
198+
.attr('transform', `translate(0, ${graphHeight})`);
199+
200+
// Append the y-axis to the bar chart
201+
const yAxisGroup = graph.append('g').attr('class', 'y-axis');
202+
203+
// Set the domain of the y-axis
204+
y = d3.scaleLinear().range([graphHeight, 0]);
205+
206+
// Set the domain of the x-axis, set the space between the bars using padding
207+
const x = d3
208+
.scaleBand()
209+
.range([0, graphWidth])
210+
.paddingInner(0.2)
211+
.paddingOuter(0.2);
212+
213+
// Initialize the x-axis
214+
const xAxis = d3.axisBottom(x);
215+
// Initialize the y-axis
216+
const yAxis = d3.axisLeft(y);
217+
195218

196219
// Reinitialze the data for the bar chart, selecting the appropriate year and country
197220
let data = completeData[year][country];
198221

222+
// Variable containing the formatted data
223+
let graphData;
224+
199225
// Set the value of the bars to zero if the data of a particular year is not available in the dataset
200226
if (data === undefined) {
201-
data = {
202-
Country: country,
203-
Region: '',
227+
graphData = {
204228
'Economy (GDP per Capita)': 0,
205229
'Freedom to make life choices': 0,
206230
Generosity: 0,
207-
'Happiness Rank': 0,
208-
'Happiness Score': 0,
209231
'Healthy life expectancy': 0,
210232
'Trust (Government Corruption)': 0,
211233
};
234+
} else {
235+
// Assign the data to seperate categories
236+
graphData = {
237+
'Economy (GDP per Capita)': data['Economy (GDP per Capita)'],
238+
'Freedom to make life choices': data['Freedom to make life choices'],
239+
Generosity: data['Generosity'],
240+
'Healthy life expectancy': data['Healthy life expectancy'],
241+
'Trust (Government Corruption)': data['Trust (Government Corruption)'],
242+
};
212243
}
213244

214-
// Assign the data to seperate categories
215-
const graphData = {
216-
'Economy (GDP per Capita)': data['Economy (GDP per Capita)'],
217-
'Freedom to make life choices': data['Freedom to make life choices'],
218-
Generosity: data['Generosity'],
219-
'Healthy life expectancy': data['Healthy life expectancy'],
220-
'Trust (Government Corruption)': data['Trust (Government Corruption)'],
221-
};
222-
223245
// Set the name of a bar equal to the corresponding category
224246
const barsKeys = Object.keys(graphData);
225247
// Set the value of a bar equal to the corresponding category
@@ -231,6 +253,12 @@ export function updateBarChartData(completeData, year, country) {
231253
bars[idx] = { name: barsKeys[idx], value: barsValues[idx] };
232254
});
233255

256+
// Dynamically set the domain of the y-axis to category containing the maximum value
257+
y.domain([0, (Math.ceil(d3.max(barsValues) * 10) / 10).toFixed(1)]);
258+
259+
// Dynamically set the domain of the x-axis to the categories in the dataset
260+
x.domain(barsKeys);
261+
234262
// Associate the data with the available bars
235263
const rects = graph.selectAll('rect').data(bars);
236264
rects.exit().remove();
@@ -246,6 +274,19 @@ export function updateBarChartData(completeData, year, country) {
246274
.attr('height', (d) => {
247275
return graphHeight - y(d.value);
248276
});
277+
278+
// Call the x-axis
279+
xAxisGroup.call(xAxis);
280+
// Call the y-axis
281+
yAxisGroup.call(yAxis);
282+
283+
// Styling properties of the category names on the x-axis
284+
xAxisGroup
285+
.selectAll('text')
286+
.attr('transform', `rotate(-40)`)
287+
.attr('text-anchor', 'end')
288+
.attr('fill', 'white')
289+
.style('font-size', '12px');
249290
}
250291

251292
// Remove the bar chart when a country is deselected

src/js/graphs/scatter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ export function initScatter(completeData, year) {
510510
scale = 1.8;
511511
}
512512

513-
graph.selectAll('.x-axis').remove();
513+
fx
514514

515515
// Set the default new domain of the x-axis
516516
const x = d3.scaleLinear().domain([0, scale]).range([0, graphWidth]);

0 commit comments

Comments
 (0)