Front End Design Highcharts - NCIOCPL/cgov-digital-platform GitHub Wiki
There are a small number of interactive charts on the NCI Budget Fact Book sub-site. These charts are implemented via the HighCharts library.
The charts are populated by data contained within JSON formatted files which reside in the Drupal CMS, and the data file(s) in Drupal. The data files are retrieved via JavaScript code on the page containing the corresponding chart(s).
There are 16 charts in total, with 14 of them currently active, and displayed in FactBook. List of pages, and their corresponding charts are:
Page | URL | Chart Title | Chart Unique Id | File Name | Active |
---|---|---|---|---|---|
AIDS Funding History | https://www.cancer.gov/about-nci/budget/fact-book/historical-trends/aids-funding | AIDS Funding History | NCI-Chart__aids-funding | aids-funding | YES |
Professional Judgment and Appropriations | https://www.cancer.gov/about-nci/budget/fact-book/historical-trends/bypass-appropriations | Historical Budget Comparison | NCI-Chart__appropriations | bypass-appropriations | YES |
Grant and Contract Awards | https://www.cancer.gov/about-nci/budget/fact-book/extramural-programs/grant-contract-awards | Grant and Contract Awards by State and Institution | NCI-Chart__grants-contracts | grants-contracts | NO |
Comparison of Dollars, Positions & Space | https://www.cancer.gov/about-nci/budget/fact-book/historical-trends/comparison | Comparison of Dollars, Positions, and Space | NCI-Chart__compare-html-amount | html-amount | YES |
Research Career "K" Awards | https://www.cancer.gov/about-nci/budget/fact-book/extramural-programs/k-awards | Total Number of K Awards | NCI-Chart__html-bar | html-bar | YES |
Research Career "K" Awards | https://www.cancer.gov/about-nci/budget/fact-book/extramural-programs/k-awards | Percent of Total Research Career Awards Funded | NCI-Chart__kawards-pie | kawards-pie | YES |
Extramural Funding | https://www.cancer.gov/about-nci/budget/fact-book/data/extramural | Extramural Funds | NCI-Chart__html-extramural | html-extramural | YES |
Dr. Ruth L. Kirschstein National Research Service Awards | https://www.cancer.gov/about-nci/budget/fact-book/extramural-programs/nrsa | National Research Service Awards (NRSAs) Predoctoral and Postdoctoral Trainees | NCI-Chart__html-nrsa | html-nrsa | YES |
NIH Management Fund | https://www.cancer.gov/about-nci/budget/fact-book/data/management-fund | NIH Management Fund, Service & Supply Fund, and GSA Rent | NCI-Chart__management-fund | management-funds | YES |
Obligations | https://www.cancer.gov/about-nci/budget/fact-book/cancer-moonshot/moonshot-obligations | Cancer Moonshot and Carryover - Obligations by Mechanism | NCI-Chart__moonshot-obligations | moonshot-obligations | YES |
Research Project Grants | https://www.cancer.gov/about-nci/budget/fact-book/extramural-programs/rpg | RPGs Number of Awards | NCI-Chart__number-rpg-awards | number-rpg-awards | YES |
Research Project Grants | https://www.cancer.gov/about-nci/budget/fact-book/extramural-programs/rpg | RPGs Average Cost | NCI-Chart__rpg-cost-per-award | rpg-cost-per-award | NO |
Research Project Grants | https://www.cancer.gov/about-nci/budget/fact-book/extramural-programs/rpg | Percent Share of Total RPG Funds, ... | NCI-Chart__rpg-funding | rpg-funding | YES |
NCI and Division Obligations | https://www.cancer.gov/about-nci/budget/fact-book/data/obligations | Percent Share of Total NCI Dollars | NCI-Chart_obligations-mechanism | obligations-mechanism | YES |
Program Structure | https://www.cancer.gov/about-nci/budget/fact-book/data/program-structure | Program Structure | NCI-Chart__program-structure | program-structure | YES |
Extramural vs Intramural & RMS Funding | https://www.cancer.gov/about-nci/budget/fact-book/historical-trends/extramural-intramural-rms | Extramural vs Intramural and RMS Funding | NCI-Chart__rms-funding | rms-funding | YES |
To add a chart to a new or existing page (existing pages include any of listed ones in the table above), it requires a code update, as well as a Drupal content update.
The code controlling charts resides in the cgov-digital-platform repository in the docroot/profiles/custom/cgov_site/themes/custom/cgov/cgov_common/src/libraries/charts directory.
Key Points:
- Charts are restricted to appear on only certain paths, controlled by the
rules.js
file. - Each chart must have a unique identifier (see above).
- Each chart also has an "edition" of the form
YYYYMMDD_REVISION
(e.g.20230329_2
). See the "Cache Busting" topic below. - Each chart has a Raw HTML block in the CMS' custom blocks library. This block contains both the unique identifier and the "Edition" and has the form:
The block is inserted into the page where the chart is to appear.
<div style="min-width: 310px; height: 400px; margin: 0 auto;" id="UNIQUE_IDENTIFIER" data-chart-revision="EDITION"></div>
- Each style of chart has a separate JavaScript file in the library which is responsible for providing an
initChart
function which returns an object containing the chart's ID as well as the data to populate the chart.
When a user hits a FactBook url ("https://www.cancer.gov/about-nci/budget/fact-book/"), the url pathname is retrieved and getShouldLoadChartWrapper()
validates to see if it matches a set of rules. Once pathname matches rule set (/about-nci/budget/fact-book/
), then the DOM is checked for a div container (added to the page by Drupal)
with an element id which matches a unique id that's already defined within a javascript chart file in the code. If the requested DOM element is found,
the chart data is retrieved, chart is initiated with data, and DOM element is populated with chart.
In order to add a new chart, a javascript chart file will have to be added to the codebase in the library folder that contains the following
- A defined data file name (File Name) - Name of the file which will hold the json data for the chart that will be uploaded into Drupal.
- Unique chart id (Chart Id) - Unique name that follows naming convention for
Chart Id
column listed in the table above. - Chart init function - Javascript function that initiates the chart.
A working example can be seen here for the Aids History chart.
Once code is in place, Drupal content update needs to be made.
- Log in to Drupal CMS for the target environment
- Hover over the Content > Media > Add media, and click on File
- Click on the "SITE SECTION" link
- In the "SITE SECTION" block, click on the "Select Site Section" button
- In the "Search by Path" field, enter "/about-nci/budget/fact-book", and click on "Apply" to list matching paths
- Select the checkbox next to the "/about-nci/budget/fact-book" path from the results
- Click on "Select Site Section" button at the bottom of the page
- In the "Pretty URL" field, enter the "File Name" that was set in the javascript chart file
- In the "Name" field, enter "factbook-" followed by the file name provided in the previous step
- In the "Browser Title" field, enter the same value entered in step 9 above "factbook-{file-name}"
- In the "Save as" field, click on the dropdown, and select "Publish"
- Click "Save"
- In Drupal CMS, hover over the Structure > Block layout, and click on Add custom block
- From the options provided, click on "Raw HTML Block"
- In the "Block description" field, enter the file name set in the javascript chart file
- In the "Body" field, enter the chart container block element with and id attribute matching the id defined in the javascript chart file created
similar to what's shown below
<div id="NCI-Chart__{chart-id}" data-chart-revision="{YYYYMMDD_revisionNumber}"></div>
- Confirm the "Save as" field has "Published" selected from the dropdown
- Click on the "Save" button
- In Drupal CMS, hover over the Content > Add content, and click on Article
- Fill in all the necessary fields for creating the page
- In the "BODY" section, for the "Content" WYSIWYG editor, click on the icon to Insert Block Content
- In the "Name" field, enter the block description name (file name), you entered in step 3 for the Add custom block above
- Click on the "Apply" button
- Select the checkbox next to the entry
- Click on the "Select block" button
- In the resulting modal, select "Full" from the dropdown for the 'Display as' field and click the "Embed" button
- In the "Save as" field, from the dropdown, select "Published"
- Click on the "Save" button at the bottom of the page
- Visit page to see included chart
- In Drupal CMS, click on the Content menu
- Select the "Article" option from the "Content type" dropdown field
- In the "Title" field, enter the page name you wish to add chart, and click on the "Filter" button
- Click on the "Edit" button in the "OPERATIONS" column for the filtered result
- In the "BODY" section, for the "Content" WYSIWYG editor, place your cursor anywhere within the content where you want your chart located and click on the icon to Insert Block Content
- In the "Name" field, enter the block description name (file name), you entered in step 3 for the Add custom block above
- Click on the "Apply" button
- Select the checkbox next to the entry
- Click on the "Select block" button
- In the resulting modal, select "Full" from the dropdown for the 'Display as' field and click the "Embed" button
- In the "Save as" field, from the dropdown, select "Published"
- Click on the "Save" button at the bottom of the page
- Visit page to see included chart
The Frontend Globals Config holds the chart data configuration for FactBook. It holds information for the base URL (where all related chart data files are located), and data type (which only currently supports json
data types).
A snippet of the config for FactBook's setup is shown below
"chartData": {
"factBook": {
"baseUrl": "/about-nci/budget/fact-book",
"dataType": "json"
}
To view the Frontend Globals configuration In Drupal CMS menu, hover over the Configuration > System, and click on Frontend Globals Config Editor
In order to update the associated data on the AIDS Funding History page for example. You will need to follow the steps below.
- Once logged in to Drupal CMS for the target environment, hover over the Content menu, and click on the Media sub-menu
- Enter associated File Name "aids-funding" from the table above in the "Media name" field and click on the "Filter" button
- Click on the Edit button in the OPERATIONS field for the aids-funding filtered result
- For the required File field, click on the Remove button to remove the currently associated chart data file
- Click on the "Choose File" button and select your desired data file to replace the previously removed one. Note the spinning icon, and allow to complete before proceeding to next step
- Once step above is complete, click the Save button at the bottom of the page
- Filter again for "aids-funding" entry just edited, and click on edit again
- At the bottom of the page for the "Change to" field, select ‘Published, and then click Save
The response from the call for the data file is cached by the browser. In order for the call to return a response for the updated data file, an attribute was placed in the HTML block container for each chart with a value that is appended to the request for the data file to invalidate the cache. This value has to be changed everytime a chart data file has been updated in order to return an updated response.
HTML container block sample:
<div id="NCI-Chart__aids-funding" data-chart-revision="20230314_1"></div>
Data file request sample: https://www.cancer.gov/about-nci/budget/fact-book/aids-funding.json?t=20230314_1
To retrieve updated data for chart on the AIDS Funding History page
- In Drupal CMS menu, hover over the Structure > Block Layout, and click on Custom block library
- Enter associated File Name "aids-funding" from the table above into the "Block description" field and click on the "Apply" button to filter
- For "aids-funding" under the "Operations" column, click on the "Edit" button
- In the "Body" field, look for the HTML div block that contains the attribute "data-chart-revision", and update its value to match existing format "YYYYMMDD_revision-number" (e.g 20230314_1, 20230314_2, ...), and click on the "Save" button
- Visit the AIDS Funding History page containing chart data just updated to see your changes