Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <script src="https://unpkg.com/alpinejs@3.1.x/dist/cdn.min.js" defer></script>

<div x-data="app">
	<table>
		<thead>
			<tr>
				<template x-for="city in cities">
					<th x-text="city.label"></th>
				</template>
			</tr>
		</thead>
		<tbody>
			<template x-for="i in numForecasts">
				<tr>
					<template x-for="city in cities">
						<td x-text="city.forecast[i-1].temperature"></td>
					</template>				
				</tr>
			</template>
		</tbody>
	</table>
	<p>
	<canvas id="myChart"></canvas>
</div>

              
            
!

CSS

              
                [x-cloak] { display: none !important; }

table {
	border-collapse: collapse;
	border: 1px solid black;
	width: 100%;
	max-width: 500px;
}

th, td {
	border: 1px solid black;
	padding: 5px;	
}
              
            
!

JS

              
                const APIKEY = 'lrokzEoN2n7ifLAVgrChU4V6XPEyqAZp5ikO6UWF';

document.addEventListener('alpine:init', () => {
  Alpine.data('app', () => ({
		async init() {
			console.log('init');
			// get Forecasts for my cities
			await this.getForecasts(this.cities);
			this.renderChart();
		},
		cities: [
			{ label: "Lafayette, LA", latitude: 30.22, longitude: -92.02 },
			{ label: "Bellingham, WA", latitude: 48.768, longitude: -122.485 },
			{ label: "Chicago, IL", latitude: 41.881, longitude: -87.623 },
			{ label: "Washington, DC", latitude: 38.895, longitude: -77.036 }			
		],
		numForecasts:0,
		async getForecasts(locs) {
			console.log('get forecasts for my locations');
			let requests = [];
			locs.forEach(l => {
				requests.push(this.getHourlyForecast(l.latitude, l.longitude));
			});
			let data = await Promise.all(requests);
			data.forEach((d, i) => {
				this.cities[i].forecast = d;
			});
			this.numForecasts = this.cities[0].forecast.length;
		},
		async getHourlyForecast(lat, lng) {
			/*
			Implement simple caching so CodePen doesn't go crazy hitting the API.
			*/
			if(sessionStorage[`forecast_${lat}_${lng}`]) return JSON.parse(sessionStorage[`forecast_${lat}_${lng}`]);
			console.log('not in cache');
			let req = await fetch(`https://api.pirateweather.net/forecast/${APIKEY}/${lat},${lng}?exclude=alerts,daily,currently,minutely&units=us`);
			let data = await req.json();
			let result = data.hourly.data.slice(0,12);
			sessionStorage[`forecast_${lat}_${lng}`] = JSON.stringify(result);
			return result;
		},
		renderChart() {
			const ctx = document.getElementById('myChart');

			let names = this.cities.map(c => c.label);
			
			let highestTemps = this.cities.map(c => {
				return c.forecast.reduce((highest,f) => {
					if(f.temperature > highest) return f.temperature;
					return highest;
				},0);
			});

			let lowestTemps = this.cities.map(c => {
				return c.forecast.reduce((lowest,f) => {
					if(f.temperature < lowest) return f.temperature;
					return lowest;
				},999);
			});
			
			new Chart(ctx, {
				type: 'line',
				data: {
					labels: names,
					datasets: [
						{
							label: 'Highest Temp',
							data: highestTemps,
							borderWidth: 1
						},
						{
							label: 'Lowest Temp',
							data: lowestTemps,
							borderWidth: 1
						}						
					]
				},
				options: {
					scales: {
						y: {
							min: -20,
							max: 120
						}
					}
				}
			});
			
		}
  }))
});


              
            
!
999px

Console