Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pop pyramid improvements #276

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions population-pyramid-static-with-comparison/chart.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ div.legend--item div:last-child{
.legend--icon--circle{
margin-right: 8px;
}

.legend--item--right{
display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: center;
padding-right: 2px;
}

4 changes: 2 additions & 2 deletions population-pyramid-static-with-comparison/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ config = {
"sourceText": "Office for National Statistics",
"graphic_data_url": "data.csv",
"comparison_data": "comparison.csv",
"dataType": "numbers",
// dataType can be a 'percentage' or 'numbers' where it works out the percentage in the script
"colour_palette": ["#9A86E9", "#3fb0b3"],
// this is the lighter palette for reference lines ["#9A86E9", "#3fb0b3"]
"comparison_colour_palette": ["#5c5185", "#306970"],
"legend": ["Area name", "England and Wales"],
"legendJustify": "centre", // "start" or "centre"
"xAxisDisplayFormat": "percentage", //"raw" or "percentage"
"xAxisNumberFormat": ".1%",
"yAxisTicksEvery": 10,
"xAxisLabel": "Percentage",
Expand Down
104 changes: 53 additions & 51 deletions population-pyramid-static-with-comparison/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function drawGraphic() {
// calculate percentage if we have numbers
// percentages are based of total populations as is common practice amongst pop pyramids

if (config.essential.dataType == 'numbers') {
if (config.essential.xAxisDisplayFormat == 'percentage') {
popTotal = d3.sum(graphic_data, (d) => d.maleBar + d.femaleBar);

comparisonPopTotal = d3.sum(
Expand Down Expand Up @@ -82,7 +82,7 @@ function drawGraphic() {

maxPercentage = d3.max([
d3.max(graphic_data_new, (d) => d.value),
d3.max(comparison_data_new, (d) => d3.max([d.femaleBar, d.maleBar]))
d3.max(comparison_data_new, (d) => d3.max([d.femalePercent, d.malePercent]))
]);

// set up widths
Expand Down Expand Up @@ -246,55 +246,57 @@ function drawGraphic() {

widths = [chart_width + margin.left, chart_width + margin.right];

legend
.append('div')
.attr('class', 'flex-row')
.style('gap', margin.centre + 'px')
.selectAll('div')
.data(['Females', 'Males'])
.join('div')
.style('width', (d, i) => widths[i] + 'px')
.append('div')
.attr('class', 'chartLabel')
.append('p')
.text((d) => d);

dataForLegend = [
['x', 'x'],
['y', 'y']
]; //dummy data

titleDivs = titles
.selectAll('div')
.data(dataForLegend)
.join('div')
.attr('class', 'flex-row')
.style('gap', margin.centre + 'px')
.selectAll('div')
.data((d) => d)
.join('div')
.style('width', (d, i) => widths[i] + 'px')
.append('div')
.attr('class', 'legend--item');

titleDivs
.append('div')
.style('background-color', (d, i) =>
d == 'x'
? config.essential.colour_palette[i]
: config.essential.comparison_colour_palette[i]
)
.attr('class', (d) =>
d == 'x' ? 'legend--icon--circle' : 'legend--icon--refline'
);

titleDivs
.append('div')
.append('p')
.attr('class', 'legend--text')
.html((d) =>
d == 'x' ? config.essential.legend[0] : config.essential.legend[1]
);
const legendData = ['Females', 'Males'];
const dummyData = [['x', 'x'], ['y', 'y']]; // dummy data

const createLegend = (justify) => {
const legendDiv = legend.append('div').attr('class', 'flex-row').style('gap', margin.centre + 'px');
const titleDiv = titles.selectAll('div').data(dummyData).join('div').attr('class', 'flex-row').style('gap', margin.centre + 'px');

legendDiv.selectAll('div')
.data(legendData)
.join('div')
.style('width', (d, i) => widths[i] + 'px')
.append('div')
.attr('class', 'chartLabel')
.style('text-align', (d, i) => justify === 'centre' && i === 0 ? 'right' : 'left')
.append('p')
.text((d) => d);

titleDiv.selectAll('div')
.data((d) => d)
.join('div')
.style('width', (d, i) => widths[i] + 'px')
.append('div')
.attr('class', (d, i) => i === 0 && justify === 'centre' ? 'legend--item legend--item--right' : 'legend--item') // Apply CSS class for alignment and padding
.each(function (d, i) {
const div = d3.select(this);
if (justify === 'centre' && i === 0) {
div.append('div')
.append('p')
.attr('class', 'legend--text')
.html(d == 'x' ? config.essential.legend[0] : config.essential.legend[1])
.style("padding-right", "8px")
.attr("class", "legend--text");
div.append('div')
.style('background-color', d == 'x' ? config.essential.colour_palette[i] : config.essential.comparison_colour_palette[i])
.attr('class', d == 'x' ? 'legend--icon--circle' : 'legend--icon--refline');
d3.select('.legend--icon--circle')
.style('margin-right', '0px')
.style('margin-left', '8px');
} else {
div.append('div')
.style('background-color', d == 'x' ? config.essential.colour_palette[i] : config.essential.comparison_colour_palette[i])
.attr('class', d == 'x' ? 'legend--icon--circle' : 'legend--icon--refline');
div.append('div')
.append('p')
.attr('class', 'legend--text')
.html(d == 'x' ? config.essential.legend[0] : config.essential.legend[1]);
}
});
};

createLegend(config.essential.legendJustify);

//create link to source
d3.select('#source').text('Source: ' + config.essential.sourceText);
Expand Down
7 changes: 7 additions & 0 deletions population-pyramid-static/chart.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ div#titles {
flex-direction: row;
}

.legend--item--right{
display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: center;
padding-right: 2px;
}
4 changes: 2 additions & 2 deletions population-pyramid-static/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ config = {
"This chart has been hidden from screen readers. The main message of the chart is summarised in the chart title.",
"sourceText": "Office for National Statistics",
"graphic_data_url": "data.csv",
"dataType": "numbers",
// dataType can be a 'percentage' or 'numbers' where it works out the percentage in the script
"colour_palette": ["#6749A6", "#2EA1A4"],
// this is the darker palette when there are no comparison lines ["#6749A6","#2EA1A4"]
"legend": ["Variable name"],
"legendJustify": "centre", // "start" or "centre"
"xAxisDisplayFormat": "percentage", //"raw" or "percentage"
"xAxisNumberFormat": ".1%",
"yAxisTicksEvery": 10,
"xAxisLabel": "Percentage",
Expand Down
91 changes: 52 additions & 39 deletions population-pyramid-static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function drawGraphic() {

// calculate percentage if we have numbers
// percentages are based of total populations as is common practice amongst pop pyramids
if (config.essential.dataType == 'numbers') {
if (config.essential.xAxisDisplayFormat == 'percentage') {
popTotal = d3.sum(graphic_data, (d) => d.maleBar + d.femaleBar);

// turn into tidy data
Expand Down Expand Up @@ -191,44 +191,57 @@ function drawGraphic() {

widths = [chart_width + margin.left, chart_width + margin.right];

legend
.append('div')
.attr('class', 'flex-row')
.style('gap', margin.centre + 'px')
.selectAll('div')
.data(['Females', 'Males'])
.join('div')
.style('width', (d, i) => widths[i] + 'px')
.append('div')
.attr('class', 'chartLabel')
.append('p')
.text((d) => d);

dataForLegend = [['x', 'x']]; //dummy data

titleDivs = titles
.selectAll('div')
.data(dataForLegend)
.join('div')
.attr('class', 'flex-row')
.style('gap', margin.centre + 'px')
.selectAll('div')
.data((d) => d)
.join('div')
.style('width', (d, i) => widths[i] + 'px')
.append('div')
.attr('class', 'legend--item');

titleDivs
.append('div')
.style('background-color', (d, i) => config.essential.colour_palette[i])
.attr('class', 'legend--icon--circle');

titleDivs
.append('div')
.append('p')
.attr('class', 'legend--text')
.html(config.essential.legend);
const legendData = ['Females', 'Males'];
const dummyData = [['x', 'x']]; // dummy data

const createLegend = (justify) => {
const legendDiv = legend.append('div').attr('class', 'flex-row').style('gap', margin.centre + 'px');
const titleDiv = titles.selectAll('div').data(dummyData).join('div').attr('class', 'flex-row').style('gap', margin.centre + 'px');

legendDiv.selectAll('div')
.data(legendData)
.join('div')
.style('width', (d, i) => widths[i] + 'px')
.append('div')
.attr('class', 'chartLabel')
.style('text-align', (d, i) => justify === 'centre' && i === 0 ? 'right' : 'left')
.append('p')
.text((d) => d);

titleDiv.selectAll('div')
.data((d) => d)
.join('div')
.style('width', (d, i) => widths[i] + 'px')
.append('div')
.attr('class', (d, i) => i === 0 && justify === 'centre' ? 'legend--item legend--item--right' : 'legend--item') // Apply CSS class for alignment and padding
.each(function (d, i) {
const div = d3.select(this);
if (justify === 'centre' && i === 0) {
div.append('div')
.append('p')
.attr('class', 'legend--text')
.html(d == 'x' ? config.essential.legend[0] : config.essential.legend[1])
.style("padding-right", "8px")
.attr("class", "legend--text");
div.append('div')
.style('background-color', d == 'x' ? config.essential.colour_palette[i] : config.essential.comparison_colour_palette[i])
.attr('class', d == 'x' ? 'legend--icon--circle' : 'legend--icon--refline');
d3.select('.legend--icon--circle')
.style('margin-right', '0px')
.style('margin-left', '8px');
} else {
div.append('div')
.style('background-color', d == 'x' ? config.essential.colour_palette[i] : config.essential.comparison_colour_palette[i])
.attr('class', d == 'x' ? 'legend--icon--circle' : 'legend--icon--refline');
div.append('div')
.append('p')
.attr('class', 'legend--text')
.html(d == 'x' ? config.essential.legend[0] : config.essential.legend[1]);
}
});
};

createLegend(config.essential.legendJustify);

//create link to source
d3.select('#source').text('Source: ' + config.essential.sourceText);
Expand Down
8 changes: 8 additions & 0 deletions population-pyramid-with-comparison-toggle/chart.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ input[type='radio']:focus-visible+label {

.legend--icon--circle{
margin-right: 8px;
}

.legend--item--right{
display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: center;
padding-right: 2px;
}
4 changes: 2 additions & 2 deletions population-pyramid-with-comparison-toggle/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ config = {
"comparison_data": "comparison.csv",
"comparison_time_data": "comparison-time.csv",
"buttonLabels": ["England and Wales", "2011"],
"dataType": "numbers",
// dataType can be a 'percentage' or 'numbers' where it works out the percentage in the script
"colour_palette": ["#9A86E9", "#3fb0b3"],
// this is the lighter palette for reference lines ["#9A86E9", "#3fb0b3"]
"comparison_colour_palette": ["#5c5185", "#306970"],
"legend": ["Area name", "Selected comparison"],
"legendJustify": "start", // "start" or "centre"
"xAxisDisplayFormat": "percentage", //"raw" or "percentage"
"xAxisNumberFormat": ".1%",
"yAxisTicksEvery": 10,
"xAxisLabel": "Percentage",
Expand Down
Loading