Skip to content

Commit

Permalink
wip commit
Browse files Browse the repository at this point in the history
  • Loading branch information
helenb committed Feb 5, 2025
1 parent 87a48ef commit 41e4bcc
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 119 deletions.
19 changes: 8 additions & 11 deletions cms/datavis/models/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def media(self) -> Media:
"https://code.highcharts.com/modules/exporting.js",
"https://code.highcharts.com/modules/export-data.js",
"https://code.highcharts.com/modules/accessibility.js",
"https://code.highcharts.com/modules/annotations.js",
]
)

Expand Down Expand Up @@ -199,13 +200,7 @@ def get_component_config(self, headers: Sequence[str], rows: Sequence[list[str |
},
"xAxis": self.get_x_axis_config(headers, rows),
"yAxis": self.get_y_axis_config(headers, rows),
"navigation": {
"enabled": False,
},
"annotations": self.get_annotations_config(),
"credits": {
"enabled": False,
},
"series": self.get_series_data(headers, rows),
}

Expand Down Expand Up @@ -256,18 +251,19 @@ def get_y_axis_config(
def get_annotations_config(self) -> list[dict[str, Any]]:
config: list[dict[str, Any]] = []
annotation_group: dict[str, Any] = {
"draggable": "",
"labelOptions": {
"backgroundColor": "rgba(255,255,255,0.5)",
"verticalAlign": "top",
},
#"draggable": "",
# "labelOptions": {
# "backgroundColor": "rgba(255,255,255,0.5)",
# "verticalAlign": "top",
# },
}
# TODO: It's likely we'll want to support a few different style
# options for annotations, in which case, we'd split annotations
# into multiple groups, each with a separate 'labelOptions' value
# to control the styling.
group_labels: list[dict[str, Any]] = []
for item in self.annotations.raw_data: # pylint: disable=no-member
print(item)
value = item["value"]
group_labels.append(
{
Expand All @@ -280,6 +276,7 @@ def get_annotations_config(self) -> list[dict[str, Any]]:
},
}
)
#print(group_labels)

if group_labels:
annotation_group["labels"] = group_labels
Expand Down
29 changes: 29 additions & 0 deletions cms/jinja2/templates/datavis/_macro.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% macro onsChart(params) %}
<div data-highcharts-base-chart data-highcharts-type="{{ params.chartType }}" data-highcharts-theme="{{ params.theme }}" data-highcharts-title="{{ params.title }}">
<figure class="highcharts-figure" id="{{ params.domId }}">
<h2>{{ params.title }}</h2>
<h3>{{ params.subtitle }}</h3>
<div data-highcharts-chart data-highcharts-id="{{ params.uuid }}"></div>
{% if params.caption %}
<figcaption>{{ params.caption }}</figcaption>
{% endif %}
</figure>
{% set scriptid = ["config--", params.uuid] | join %}
{{ params.config|json_script(scriptid) }}

{% if params.download_title and (params.download_image or params.download_csv or params.download_excel) %}
{% if params.download_title %}<h3>{{ params.download_title }}</h3>{% endif %}
<ol>
{% if params.download_excel %}
<li><a href="{{ params.download_excel }}">Excel spreadsheet (XLSX format{% if params.download_excel_size %}, {{ params.download_excel_size }}{% endif %})</a></li>
{% endif %}
{% if params.download_csv %}
<li><a href="{{ params.download_csv }}">Simple text file (CSV format{% if params.download_csv_size %}, {{ params.download_csv_size }}{% endif %})</a></li>
{% endif %}
{% if params.download_image %}
<li><a href="{{ params.download_image }}">Image (PNG format{% if params.download_image_size %}, {{ params.download_image_size }}{% endif %})</a></li>
{% endif %}
</ol>
{% endif %}
</div>
{% endmacro %}
41 changes: 26 additions & 15 deletions cms/jinja2/templates/datavis/base_highcharts_chart.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
<div data-highcharts-base-chart data-highcharts-type="{{ object.highcharts_chart_type }}" data-highcharts-theme="{{ object.theme }}" data-highcharts-title="{{ object.title }}">
{% if not media_already_loaded and object.media %}
{{ object.media }}
{% endif %}
<figure class="highcharts-figure" id="chart-figure--{{ object.uuid }}">
<h2>{{ object.title }}</h2>
<h3>{{ object.subtitle }}</h3>
<div data-highcharts-chart data-highcharts-id="{{ object.uuid }}"></div>
{% if object.caption %}
<figcaption>{{ object.caption|richtext }}</figcaption>
{% endif %}
</figure>
{% set scriptid = ["config--", object.uuid] | join %}
{{ config|json_script(scriptid) }}
</div>
{% from "templates/datavis/_macro.njk" import onsChart %}

{% if not media_already_loaded and object.media %}
{{ object.media }}
{% endif %}

{# fmt:off #}
{{
onsChart({
"chartType": object.highcharts_chart_type,
"theme": object.theme,
"title": object.title,
"subtitle": object.subtitle,
"uuid": object.uuid,
"caption": object.caption|richtext,
"config": config,
"download_title": "Figure 1 data",
"download_image": "xyz",
"download_image_size": "18KB",
"download_csv": "xyz",
"download_csv_size": "25KB",
"download_excel": "xyz",
"download_excel_size": "25KB",
})
}}
{# fmt:on #}
37 changes: 37 additions & 0 deletions cms/static_src/javascript/components/bar-chart-plot-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import ChartConstants from './chart-constants';

class BarChartPlotOptions {
static plotOptions() {
return this.plotOptions;
}

constructor() {
const constants = ChartConstants.constants();
this.plotOptions = {
bar: {
// Set the width of the bars to be 30px and the spacing between them to be 10px
pointWidth: 30, // Fixed bar height
groupPadding: 0, // No padding between groups
pointPadding: 0, // No padding within groups
borderWidth: 0,
borderRadius: 0,
spacing: [10, 0, 0, 0], // [top, right, bottom, left] spacing between bars
// Set the data labels to be enabled and positioned outside the bars
// We can add custom formatting on each chart to move the labels inside the bars if the bar is wide enough
dataLabels: {
enabled: true,
inside: false,
style: {
textOutline: 'none',
// there is no semibold font weight available in the design system fonts, so we use 400 instead
fontWeight: '400',
color: constants.labelColor,
fontSize: constants.mobileFontSize,
},
},
},
};
}
}

export default BarChartPlotOptions;
30 changes: 30 additions & 0 deletions cms/static_src/javascript/components/chart-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class ChartConstants {
static constants() {
const constants = {
primaryTheme: [
'#206095',
'#27a0cc',
'#003c57',
'#118c7b',
'#a8bd3a',
'#871a5b',
'#f66068',
'#746cb1',
'#22d0b6',
],
// Alternate theme colours from https://service-manual.ons.gov.uk/data-visualisation/colours/using-colours-in-charts
alternateTheme: ['#206095', '#27A0CC', '#871A5B', '#A8BD3A', '#F66068'],
labelColor: '#414042',
axisLabelColor: '#707071',
gridLineColor: '#d9d9d9',
zeroLineColor: '#b3b3b3',
// Responsive font sizes
mobileFontSize: '0.875rem',
desktopFontSize: '1rem',
};

return constants;
}
}

export default ChartConstants;
17 changes: 17 additions & 0 deletions cms/static_src/javascript/components/column-chart-plot-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ColumnChartPlotOptions {
static plotOptions() {
return this.plotOptions;
}

constructor() {
this.plotOptions = {
column: {
groupPadding: 0.2,
borderWidth: 0,
borderRadius: 0,
},
};
}
}

export default ColumnChartPlotOptions;
117 changes: 38 additions & 79 deletions cms/static_src/javascript/components/common-chart-options.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
class ChartOptions {
/* this contains global options for all charts */
import ChartConstants from './chart-constants';

class CommonChartOptions {
static options() {
return this.options;
}

static annotionLabelOptions() {
return {
style: {
color: constants.axisLabelColor,
fontSize: desktopFontSize,
},
borderWidth: 0,
backgroundColor: undefined,
};
}

constructor(theme, title, type) {
// Primary theme colours from https://service-manual.ons.gov.uk/data-visualisation/colours/using-colours-in-charts
const primaryTheme = [
'#206095',
'#27a0cc',
'#003c57',
'#118c7b',
'#a8bd3a',
'#871a5b',
'#f66068',
'#746cb1',
'#22d0b6',
];
// Alternate theme colours from https://service-manual.ons.gov.uk/data-visualisation/colours/using-colours-in-charts
const alternateTheme = ['#206095', '#27A0CC', '#871A5B', '#A8BD3A', '#F66068'];
const labelColor = '#414042';
const axisLabelColor = '#707071';
const gridLineColor = '#d9d9d9';
const zeroLineColor = '#b3b3b3';
// Responsive font sizes
const mobileFontSize = '0.875rem';
const desktopFontSize = '1rem';
const constants = ChartConstants.constants();

// These options can be set globally for all charts
this.options = {
colors: theme === 'primary' ? primaryTheme : alternateTheme,
colors: theme === 'primary' ? constants.primaryTheme : constants.alternateTheme,
chart: {
backgroundColor: 'transparent',
style: {
Expand All @@ -44,8 +38,8 @@ class ChartOptions {
symbolHeight: type === 'line' ? 3 : 12,
margin: 30,
itemStyle: {
color: labelColor,
fontSize: desktopFontSize,
color: constants.labelColor,
fontSize: constants.desktopFontSize,
fontWeight: 'normal',
},
},
Expand All @@ -62,72 +56,37 @@ class ChartOptions {
// Remove Highcharts watermark
enabled: false,
},
plotOptions: {
bar: {
// Set the width of the bars to be 30px and the spacing between them to be 10px
pointWidth: 30, // Fixed bar height
groupPadding: 0, // No padding between groups
pointPadding: 0, // No padding within groups
borderWidth: 0,
borderRadius: 0,
spacing: [10, 0, 0, 0], // [top, right, bottom, left] spacing between bars
// Set the data labels to be enabled and positioned outside the bars
// We can add custom formatting on each chart to move the labels inside the bars if the bar is wide enough
dataLabels: {
enabled: true,
inside: false,
style: {
textOutline: 'none',
// there is no semibold font weight available in the design system fonts, so we use 400 instead
fontWeight: '400',
color: labelColor,
fontSize: mobileFontSize,
},
},
},
line: {
lineWidth: 3, // Sets the line thickness to 3px
linecap: 'round',
marker: {
enabled: false,
},
states: {
hover: {
lineWidth: 3, // Maintain line width on hover
},
},
},
column: {
groupPadding: 0.2,
borderWidth: 0,
borderRadius: 0,
// disabled the download button for now
navigation: {
buttonOptions: {
enabled: false,
},
},
yAxis: {
labels: {
style: {
color: axisLabelColor,
fontSize: desktopFontSize,
color: constants.axisLabelColor,
fontSize: constants.desktopFontSize,
},
},
lineColor: gridLineColor,
gridLineColor: gridLineColor,
zeroLineColor: zeroLineColor,
lineColor: constants.gridLineColor,
gridLineColor: constants.gridLineColor,
zeroLineColor: constants.zeroLineColor,
},
xAxis: {
labels: {
style: {
color: axisLabelColor,
fontSize: desktopFontSize,
color: constants.axisLabelColor,
fontSize: constants.desktopFontSize,
},
},
lineColor: gridLineColor,
gridLineColor: gridLineColor,
zeroLineColor: zeroLineColor,
lineColor: constants.gridLineColor,
gridLineColor: constants.gridLineColor,
zeroLineColor: constants.zeroLineColor,
// Add tick marks
tickWidth: 1,
tickLength: 6,
tickColor: gridLineColor,
tickColor: constants.gridLineColor,
},
// Adjust font size for smaller width of chart
// Note this is not the same as the viewport width
Expand All @@ -140,20 +99,20 @@ class ChartOptions {
chartOptions: {
legend: {
itemStyle: {
fontSize: mobileFontSize,
fontSize: constants.mobileFontSize,
},
},
xAxis: {
labels: {
style: {
fontSize: mobileFontSize,
fontSize: constants.mobileFontSize,
},
},
},
yAxis: {
labels: {
style: {
fontSize: mobileFontSize,
fontSize: constants.mobileFontSize,
},
},
},
Expand All @@ -165,4 +124,4 @@ class ChartOptions {
}
}

export default ChartOptions;
export default CommonChartOptions;
Loading

0 comments on commit 41e4bcc

Please sign in to comment.