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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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

              
                <div class="controls">
	<fieldset>
		<legend>Toggle color scheme</legend>
		<ul class="controls-list">
			<li>
				<input type="radio" name="color scheme" value="purple" id="c-purple" checked>
				<label for="c-purple">Purple</label>
			</li>
			<li>
				<input type="radio" name="color scheme" value="red" id="c-red">
				<label for="c-red">Red</label>
			</li>
			<li>
				<input type="radio" name="color scheme" value="blue" id="c-blue">
				<label for="c-blue">Blue</label>
			</li>
		</ul>
	</fieldset>
</div>
<div class="chart-wrapper" data-wrapper>
	<h3 data-heading>Weekly downloads</h3>
	<div class="chart-info">
		<p data-total>200,000</p>
	</div>
	<figure data-chart></figure>
</div>
              
            
!

CSS

              
                * {
	box-sizing: border-box;
}

:root {
	--textHeadingColor: rgb(117, 117, 117);
	--fill: hsl(258.1, 100%, 92%);
	--stroke: hsl(258.1, 100%, 66.9%);
}

body {
	font-family: 'Source Sans Pro', sans-serif;
	min-height: 100vh;
	display: grid;
	place-items: center;
	padding: 5rem 0;
}

figure {
	margin: 0;
	flex: 0 1 22.3rem;
	aspect-ratio: 6 / 2;
	display: flex;
}

svg {
	width: 100%;
	height: auto;
}

ul {
	list-style: none;
	margin: 0;
	padding: 0;
}

p {
	margin: 0;
	font-size: 2rem;
}

h3 {
	flex: 1 0 100%;
	margin: 0;
	font-size: 1rem;
	font-weight: 400;
	color: var(--textHeadingColor);
}

fieldset {
	max-width: 30rem;
	margin: 0 auto;
	border: 2px solid var(--fill);
}

legend {
	text-align: center;
	min-width: 10rem;
	margin: 0 auto;
}

.chart-wrapper {
	width: 100%;
	max-width: 600px;
	display: flex;
	flex-wrap: wrap;
	border-bottom: 2px solid var(--fill);
}

.chart-info {
	flex: 1 1 auto;
	padding: 0 1.5rem 0.5rem 0;
	align-self: end;
}

.controls {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	padding: 1rem;
}

.controls-list {
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	gap: 2rem;
	accent-color: var(--stroke);
}

.red {
	--stroke: hsl(338 100% 50%);
	--fill: hsl(338 100% 83%);
	--marker: hsl(277 100% 50%);
	--textHeadingColor: hsl(277 5% 9%);
	
	background-color: hsl(338 100% 93%);
	color: hsl(277 5% 9%);
}

.blue {
	--stroke: hsl(173 82% 46%);
	--fill: hsl(173 82% 56% / 0.2);
	--marker: hsl(183 100% 99%);
	--textHeadingColor: var(--stroke);
	
	background-color: hsl(211 16% 12%);
	color: white;
	color-scheme: dark;
}
              
            
!

JS

              
                const dimensions = {
	width: 600,
	height: 200,
	marginTop: 8
}

const xAccessor = (d) => d.date
const yAccessor = (d) => d.downloads

const formatDate = d3.timeFormat('%Y-%m-%d')

const getText = (data, d) => {
	const to = xAccessor(d)
	const from = d3.timeDay.offset(to, -7)
	
	return `${formatDate(from)} to ${formatDate(to)}`
}

const draw = (data) => {
	const wrapper = d3.select('[data-wrapper]')
	
	const svg = wrapper
		.select('[data-chart]')
		.append('svg')
		.attr('width', dimensions.width)
		.attr('height', dimensions.height)
		.attr('viewBox', `0 0 ${dimensions.width} ${dimensions.height}`)
	
	const xDomain = d3.extent(data, xAccessor)
	const yDomain = [0, d3.max(data, yAccessor)]
	
	const xScale = d3.scaleTime()
		.domain(xDomain)
		.range([0, dimensions.width])
	
	const yScale = d3.scaleLinear()
		.domain(yDomain)
		.range([dimensions.height, dimensions.marginTop])
	
	/* Area */
	const areaGenerator = d3.area()
		.x((d) => xScale(xAccessor(d)))
		.y1((d) => yScale(yAccessor(d)))
		.y0(dimensions.height)
		.curve(d3.curveBumpX)
	
	const area = svg
		.append('path')
		.datum(data)
		.attr('d', areaGenerator)
		.attr('fill', 'var(--fill)')
	
	/* Line */
	const lineGenerator = d3.line()
		.x((d) => xScale(xAccessor(d)))
		.y((d) => yScale(yAccessor(d)))
		.curve(d3.curveBumpX)
	
	const line = svg
		.append('path')
		.datum(data)
		.attr('d', lineGenerator)
		.attr('stroke', 'var(--stroke)')
		.attr('stroke-width', 5)
		.attr('stroke-linejoin', 'round')
		.attr('fill', 'none')
	
	/* Markers */
	const markerLine = svg
		.append('line')
		.attr('x1', 0)
		.attr('x2', 0)
		.attr('y1', 0)
		.attr('y2', dimensions.height)
		.attr('stroke-width', 3)
		.attr('stroke', 'var(--marker, var(--stroke))')
		.attr('opacity', 0)
	
	const markerDot = svg
		.append('circle')
		.attr('cx', 0)
		.attr('cy', 0)
		.attr('r', 8)
		.attr('fill', 'var(--marker, var(--stroke))')
		.attr('opacity', 0)
	
	/* Bisector */
	const bisect = d3.bisector(xAccessor)
	
	/* Events */
	svg.on('mousemove', (e) => {
		const [posX, posY] = d3.pointer(e)
		const date = xScale.invert(posX)

		const index = bisect.center(data, date)
		const d = data[index]
		
		const x = xScale(xAccessor(d))
		const y = yScale(yAccessor(d))
		
		markerLine
			.attr('x1', x)
			.attr('x2', x)
			.attr('opacity', 1)
		
		markerDot
			.attr('cx', x)
			.attr('cy', y)
			.attr('opacity', 1)
		
		d3.select('[data-heading]').text(getText(data, d))
		d3.select('[data-total]').text(yAccessor(d))
	})
	
	svg.on('mouseleave', () => {
		const lastDatum = data[data.length - 1]
		
		markerLine.attr('opacity', 0)
		markerDot.attr('opacity', 0)
		
		d3.select('[data-heading]').text('Weekly downloads')
		d3.select('[data-total]').text(yAccessor(lastDatum))
	})
}

const sortData = (data) => {
	return data.map((d) => {
		return {
			...d,
			date: new Date(d.date)
		}
	}).sort((a, b) => d3.ascending(a.date, b.date))
}

d3.json('https://api.npoint.io/6142010a473d754de4e6')
	.then(data => {
		const sortedData = sortData(data)
		draw(sortedData)
	})
	.catch(error => console.log(error))

/* Toggle color scheme */
const inputs = d3.selectAll('input[type="radio"]')

const colors = inputs.nodes().map((input) => {
	return input.value
})

d3.select('.controls-list')
	.on('click', (e) => {
		const { value, checked } = e.target
		
		if (!value || !checked) return
	
		document.body.classList.remove(...colors)
		document.body.classList.add(value)
	})
              
            
!
999px

Console