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

Scaffold for Statistics block on Landing pages #4261

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
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
69 changes: 69 additions & 0 deletions app/models/block/statistics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require "csv"

module Block
class Statistics < Block::Base
STATISTICS_DATA_PATH = "lib/data/landing_page_content_items/statistics".freeze

def title
data["title"]
end

def x_axis_label
data["x_axis_label"]
end

def y_axis_label
data["y_axis_label"]
end

def hide_legend
data["hide_legend"]
end

def x_axis_keys
@x_axis_keys ||= csv_rows.map { |row| row[row.keys.first] }.uniq
end

def rows
row_lines.map do |key, value|
{
label: key.to_s,
values: value[:values],
}
end
end

def data_source_link
data["data_source_link"]
end

private

def row_lines
csv_rows.each_with_object({}) do |row, lines|
label = row.keys.first
variable_name = row.values.second
value = row.values.last
if lines.key?(variable_name)
lines[variable_name][:values] << value.to_i
else
lines[variable_name] = {
label:,
values: [
value.to_i,
],
}
end
end
end

def csv_rows
rows = CSV.read(csv_file_path, headers: true).map(&:to_h)
rows.each(&:deep_symbolize_keys!)
end

def csv_file_path
@csv_file_path ||= Rails.root.join("#{STATISTICS_DATA_PATH}/#{data['csv_file']}")
end
end
end
13 changes: 13 additions & 0 deletions app/views/landing_page/blocks/_statistics.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%
add_gem_component_stylesheet("chart")
%>

<%= 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: block.x_axis_keys,
rows: block.rows,
} %>
<%= link_to block.data["data_source_link_text"], block.data_source_link %>
15 changes: 15 additions & 0 deletions lib/data/landing_page_content_items/landing_page.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ blocks:
- type: govspeak
inverse: true
content: <h2><a href="http://gov.uk">Title 2 govspeak title goes here</a></h2>
- type: statistics
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 some more data"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_two.csv"
data_source_link_text: "Data source"
data_source_link: https://www.ons.gov.uk/economy/inflationandpriceindices/timeseries/l55o/mm23
- type: share_links
links:
- href: "/twitter-share-link"
Expand Down
7 changes: 7 additions & 0 deletions lib/data/landing_page_content_items/statistics/data_one.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Date,variable,value
2024-01-01,variable_name,10
2024-02-01,variable_name,11
2024-03-01,variable_name,12
2024-04-01,variable_name,13
2024-05-01,variable_name,14
2024-06-01,variable_name,15
7 changes: 7 additions & 0 deletions lib/data/landing_page_content_items/statistics/data_two.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Date,variable,value
2023-01-01,variable_name,20
2023-02-01,variable_name,21
2023-03-01,variable_name,22
2023-01-01,variable_name_two,23
2023-02-01,variable_name_two,24
2023-03-01,variable_name_two,25
9 changes: 8 additions & 1 deletion spec/fixtures/landing_page.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ blocks:
card_content:
blocks:
- type: govspeak
inverse: true
inverse: true
content: <h2><a href="http://gov.uk">Title 2 govspeak title goes here</a></h2>
- type: card
image:
Expand Down Expand Up @@ -146,3 +146,10 @@ blocks:
- type: govspeak
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"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
data_source_link_text: "Data source"
data_source_link: https://www.example.com
7 changes: 7 additions & 0 deletions spec/fixtures/landing_page_statistics_data/data_one.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Date,variable,value
2024-01-01,variable_name,10
2024-02-01,variable_name,11
2024-03-01,variable_name,12
2024-04-01,variable_name,13
2024-05-01,variable_name,14
2024-06-01,variable_name,15
7 changes: 7 additions & 0 deletions spec/fixtures/landing_page_statistics_data/data_two.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Date,variable,value
2024-01-01,variable_name,10
2024-02-01,variable_name,11
2024-03-01,variable_name,12
2024-01-01,variable_name_two,13
2024-02-01,variable_name_two,14
2024-03-01,variable_name_two,15
107 changes: 107 additions & 0 deletions spec/models/block/statistics_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
RSpec.describe Block::Statistics do
let(:blocks_hash) do
{
"type" => "statistics",
"title" => "Chart to visually represent data",
"x_axis_label" => "X Axis",
"y_axis_label" => "Y Axis",
"csv_file" => "data_one.csv",
"data_source_link_text" => "Data source",
"data_source_link" => "https://www.example.com",
}
end

before do
Block::Statistics.send(:remove_const, "STATISTICS_DATA_PATH")
Block::Statistics.const_set("STATISTICS_DATA_PATH", "spec/fixtures/landing_page_statistics_data")
end

after do
Block::Statistics.send(:remove_const, "STATISTICS_DATA_PATH")
Block::Statistics.const_set("STATISTICS_DATA_PATH", "lib/data/landing_page_content_items/statistics")
end

describe "statistics content" do
describe "#title" do
it "gets statistics title" do
expect(described_class.new(blocks_hash).title).to eq("Chart to visually represent data")
end
end

describe "#x_axis_label" do
it "gets the label for the x-axix" do
expect(described_class.new(blocks_hash).x_axis_label).to eq("X Axis")
end
end

describe "#y_axis_label" do
it "gets the label for the y-axis" do
expect(described_class.new(blocks_hash).y_axis_label).to eq("Y Axis")
end
end
end

describe "chart data" do
describe "#data_source_link" do
it "returns the link to the external data source" do
expect(described_class.new(blocks_hash).data_source_link).to eq("https://www.example.com")
end
end

describe "#x_axis_keys" do
it "gets all of the x-axis data points" do
expected_keys = %w[
2024-01-01
2024-02-01
2024-03-01
2024-04-01
2024-05-01
2024-06-01
]

expect(described_class.new(blocks_hash).x_axis_keys).to eq(expected_keys)
end

it "gets all of the unique x-axis data points" do
expected_keys = %w[
2024-01-01
2024-02-01
2024-03-01
]
blocks_hash["csv_file"] = "data_two.csv"

expect(described_class.new(blocks_hash).x_axis_keys).to eq(expected_keys)
end
end

describe "#rows" do
it "gets the rows for one line of data" do
expected_rows = [
{
label: "variable_name",
values: [10, 11, 12, 13, 14, 15],
},
]

expect(described_class.new(blocks_hash).rows).to eq(expected_rows)
end

it "gets the rows for multiple lines of data" do
expected_rows = [
{
label: "variable_name",
values: [10, 11, 12],
},
{
label: "variable_name_two",
values: [13, 14, 15],
},
]

blocks_hash["csv_file"] = "data_two.csv"

expect(described_class.new(blocks_hash).rows).to eq(expected_rows)
end
end
end
end
Loading