Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>

<body class="bg-info col-12">
  <header class="header">
    <div class="container col-12">
      <h3 class="font-weight-light text-light title"></h3>
    </div>
  </header>
  <div class="bg-white py-12">
    <select id="selector">
      <option value="expat">UK Expats</option>      
      <option value="patriots">UK Based Citizens</option>
    </select>
  </div>
  <div class="bg-white py-12">
    <div id="chart"></div>
  </div>
  <script
    src="http://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
</body>

</html>
              
            
!

CSS

              
                body {
  background-color: #a3d5d3;
}
              
            
!

JS

              
                const petitionUrl = "https://petition.parliament.uk/petitions/241584.json"

const getPetitionData = () => {
	fetch(petitionUrl, {
		method: 'GET'
	})
	.then(response => response.json())
	.then(response => {
		if ($("#selector").val() === "expat") {
			loadChartData(response.data.attributes.signatures_by_country, response.data.attributes.signature_count)
		} else if ($("#selector").val() === "patriots") {
			loadChartData(response.data.attributes.signatures_by_constituency, response.data.attributes.signature_count)
		}
	})
}

const loadChartData = (petitionData, total_signatures) => {
	let signatures = [];
	let countries = [];
	
	petitionData.forEach( (e) => {
		if (e.name !== "United Kingdom") {
			countries.push(e.name)
			signatures.push(e.signature_count)
		}
	})

	const reducer = (accumulator, currentValue) => accumulator + currentValue;
	if ($("#selector").val() === "expat") {
		$('.title').html(`Revoke Article 50 British Expats By Country: ${signatures.reduce(reducer)}. Total Signatures: ${total_signatures}`)
	} else if ($("#selector").val() === "patriots") {
		$('.title').html(`Revoke Article 50 UK Based Citizens: ${signatures.reduce(reducer)}. Total Signatures: ${total_signatures}`)
	}
	let data = [{
	  values: signatures,
	  labels: countries,
	  textposition: 'inside',
	  type: 'pie'
	}];

	let layout = {
	  height: 1150,
	  width: 1200,
	  legend: {
	  	sort: false
	  }
	};
		
	Plotly.newPlot('chart', data, layout, {responsive: true});
}

getPetitionData();

$("#selector").change(() => {
	getPetitionData();
}) 
              
            
!
999px

Console