Skip to content

Commit

Permalink
Tweak wagtail chart code and add to learn pages
Browse files Browse the repository at this point in the history
  • Loading branch information
anselmbradford committed Dec 20, 2024
1 parent a77aea7 commit 61f40c4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 21 deletions.
6 changes: 4 additions & 2 deletions cfgov/unprocessed/apps/tccp/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ function handleShowMore(event) {
}
const results = document.querySelector('.o-filterable-list-results');
const showMoreFade = document.querySelector('#u-show-more-fade');
const nextResult = document.querySelector('[data-js-hook="behavior_faded-card"]');
const nextResult = document.querySelector(
'[data-js-hook="behavior_faded-card"]',
);
nextResult.setAttribute('tabIndex', '0');
nextResult.querySelectorAll('[tabindex="-1"]').forEach( elem => {
nextResult.querySelectorAll('[tabindex="-1"]').forEach((elem) => {
elem.setAttribute('tabIndex', '0');
});
results.classList.remove('o-filterable-list-results--partial');
Expand Down
11 changes: 5 additions & 6 deletions cfgov/unprocessed/css/on-demand/wagtail-chart.scss
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
@use 'sass:math';
@use '@cfpb/cfpb-design-system/src/abstracts' as *;
@import '../main';

.o-wagtail-chart {
max-width: math.div(670px, $base-font-size-px) + em;
margin-bottom: math.div(60px, $base-font-size-px) + em;
max-width: math.div(670px, $base-font-size-px) + rem;
margin-bottom: math.div(60px, $base-font-size-px) + rem;

&__subtitle {
margin: 0 0 (math.div(30px, $base-font-size-px) + em);
margin: 0 0 (math.div(30px, $base-font-size-px) + rem);
}

&__footnote {
max-width: math.div(670px, $size-vi) + rem;
padding-top: math.div(15px, $size-vi) + em;
font-size: 0.75em;
padding-top: math.div(15px, $size-vi) + rem;
font-size: math.div(12px, $base-font-size-px) + rem;
}
}
26 changes: 14 additions & 12 deletions cfgov/unprocessed/js/routes/on-demand/wagtail-charts-chart-block.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/* eslint-disable no-undef */
import pattern from 'patternomaly';

const white60 = 'rgba(255, 255, 255, 0.6)';
// This is the gray from the CFPB DS.
const gray = '#5a5d61';

/**
* Set default text color to a dark gray
* Set default text color to a dark gray.
*
* https://www.chartjs.org/docs/latest/general/colors.html
*/
Chart.defaults.color = '#5a5d61';
Chart.defaults.color = gray;

/**
* Takes an array of Chart.js datasets and returns a new array
Expand All @@ -16,9 +20,8 @@ Chart.defaults.color = '#5a5d61';
* The first line pattern is solid, the second is dashed,
* the third is dotted and all subsequent patterns are dashed
* with an increasingly thicker line.
*
* @param {array} datasets - Array of Chart.js datasets
* @returns {array} Array of Chart.js datasets with borderDash property set
* @param {Array} datasets - Array of Chart.js datasets
* @returns {Array} Array of Chart.js datasets with borderDash property set
*
* https://www.chartjs.org/docs/latest/samples/line/styling.html
* https://www.chartjs.org/docs/latest/configuration/#dataset-configuration
Expand All @@ -43,9 +46,8 @@ const patternizeChartLines = (datasets) => {
* backgroundColor property.
*
* Patterns are from the patternomaly library.
*
* @param {array} datasets - Array of Chart.js datasets
* @returns {array} Array of Chart.js datasets with backgroundColor property set
* @param {Array} datasets - List of Chart.js datasets.
* @returns {Array} List of Chart.js datasets with backgroundColor property set.
*
* https://www.chartjs.org/docs/latest/general/colors.html#patterns-and-gradients
* https://github.com/ashiguruma/patternomaly
Expand Down Expand Up @@ -76,15 +78,15 @@ const patternizeChartBars = (datasets) => {
];
return datasets.map((dataset, i) => {
dataset.backgroundColor = dataset.data.map(() => {
// First pattern is just the solid color
// First pattern is just the solid color.
if (i === 0)
return Array.isArray(dataset.backgroundColor)
? dataset.backgroundColor[0]
: dataset.backgroundColor;
return pattern.draw(
patterns[i - 1],
dataset.backgroundColor,
'rgba(255, 255, 255, 0.6)',
white60,
10,
);
});
Expand All @@ -93,15 +95,15 @@ const patternizeChartBars = (datasets) => {
};

/**
* Change the default Chart.js tooltip options
* Change the default Chart.js tooltip options.
*/
const tooltipOptions = {
yAlign: 'bottom',
displayColors: false,
};

/**
* Define a Chart.js plugin for our CFPB customizations
* Define a Chart.js plugin for our CFPB customizations.
*
* https://www.chartjs.org/docs/latest/developers/plugins.html
*/
Expand Down
2 changes: 1 addition & 1 deletion cfgov/v1/atomic_elements/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ChartBlock(WagtailChartBlock):
notes = blocks.TextBlock(required=False, help_text="Note about the chart")

def __init__(self, **kwargs):
# Always override chart_types and colors with ours
# Always override chart_types and colors with ours.
super().__init__(
chart_types=CHART_TYPES, colors=CHART_COLORS, **kwargs
)
Expand Down
20 changes: 20 additions & 0 deletions cfgov/v1/migrations/0042_add_wagtail_chart_learnpage_content.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cfgov/v1/models/learn_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class LearnPage(AbstractFilterPage):
("table", organisms.Table()),
("faq_group", schema.FAQGroup()),
("contact_us_table", organisms.ContactUsTable()),
("wagtailchart_block", charts.ChartBlock()),
],
blank=True,
)
Expand Down

0 comments on commit 61f40c4

Please sign in to comment.