Skip to content

Commit

Permalink
WIP use chart component
Browse files Browse the repository at this point in the history
  • Loading branch information
andysellick committed Oct 17, 2024
1 parent 67fc1f8 commit 6ca4b44
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
2 changes: 2 additions & 0 deletions app/assets/javascripts/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
//= require govuk_publishing_components/components/step-by-step-nav
//= require govuk_publishing_components/components/table
//= require govuk_publishing_components/components/tabs

//= require govuk_publishing_components/components/chart
4 changes: 4 additions & 0 deletions app/models/block/statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def y_axis_label
data["y_axis_label"]
end

def hide_legend
data["hide_legend"]
end

def row_header
CSV.read(csv_file_path, headers: true).headers
end
Expand Down
43 changes: 37 additions & 6 deletions app/views/landing_page/blocks/_statistics.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
<p>Placeholder statistics partial</p>
<%= block.row_header %>
<br>
<%= block.rows %>
<br>
<%= link_to block.data["data_source_link_text"], block.data_source_link %>
<%
add_gem_component_stylesheet("chart")

rows = []
lines = {}
keys = []
block.rows.each_with_index do |row, i|
keys << row["Date"]
if lines.key?(row["variable"].to_sym)
lines[row["variable"].to_sym][:values] << row["value"].to_i
else
lines[row["variable"].to_sym] = {
label: row["Date"],
values: [
row["value"].to_i
]
}
end

end

lines.each do |key, array|
rows << {
label: key.to_s,
values: lines[key][:values]
}
end
%>
<%= render "govuk_publishing_components/components/chart", {
chart_heading: block.title,
h_axis_title: block.x_axis_label,
v_axis_title: block.y_axis_label,
hide_legend: block.hide_legend,
keys: keys,
rows: rows,
} %>
<%= link_to block.data["data_source_link_text"], block.data_source_link %>
5 changes: 3 additions & 2 deletions lib/data/landing_page_content_items/landing_page.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ blocks:
inverse: true
content: <h2><a href="http://gov.uk">Title 2 govspeak title goes here</a></h2>
- type: statistics
title: "Chart to visually represent data"
title: "Chart to visually represent some data"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
hide_legend: true
csv_file: "data_one.csv"
data_source_link_text: "Data source"
data_source_link: https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyq/qna
- type: statistics
title: "Chart to visually represent data"
title: "Chart to visually represent some more data"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_two.csv"
Expand Down

0 comments on commit 6ca4b44

Please sign in to comment.