css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div>Drag data points to re-render chart</div>
            
          
!
            
              body {
	background-color: #111;
	-webkit-touch-callout: none;
	-webkit-user-select: none;
	-khtml-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}
div {
	margin: 20px auto 0;
	color: #aaa;
	font-weight: 100;
}
svg {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%,-50%);
}

.xaxis path,
.xaxis line,
.yaxis path,
.yaxis line {
    stroke: #aaa;
    fill: none;
    shape-rendering: crispEdges;
}

.xaxis text,
.yaxis text {
    font-family: sans-serif;
    font-size: 11px;
    fill: #aaa;
}

.datapoint {
    cursor: pointer;
    stroke: #111;
    fill: rgba(0, 150, 255, .75);
    stroke-width: 7;
}

.lineChart {
    fill: none;
    stroke-width: 3;
    stroke: rgba(100,100,100, 1);
}
            
          
!
            
              let w = 800;
let h = 400;
let padding = 25;
let dataset = [
	[10, 10],
	[20, 50],
	[30, 40],
	[40, 80],
	[50, 90],
	[60, 50],
	[70, 70],
	[80, 60],
	[90, 10],
	[100, 50],
	[110, 40],
	[120, 70],
	[130, 20],
	[140, 40],
	[150, 30]
];

/*create svg element*/
let svg = d3
	.select("body")
	.append("svg")
	.attr("width", w)
	.attr("height", h)
	.attr("id", "chart");

let drag = d3.behavior
	.drag()
	.on("dragstart", dragstarted)
	.on("drag", dragged)
	.on("dragend", dragended);

/*x scale*/
let xScale = d3.scale
	.linear()
	.domain([0, d3.max(dataset, d => d[0])])
	.range([padding, w - padding]);

/*y scale*/
let yScale = d3.scale
	.linear()
	.domain([0, d3.max(dataset, d => d[1])])
	.range([h - padding, padding]);

/*x axis*/
let xAxis = d3.svg
	.axis()
	.scale(xScale)
	.orient("bottom");

/*append x axis*/
svg
	.append("g")
	.attr({
		class: "xaxis",
		transform: `translate(0, ${h - padding})`
	})
	.call(xAxis);

/*y axis*/
let yAxis = d3.svg
	.axis()
	.scale(yScale)
	.orient("left");

/*append y axis*/
svg
	.append("g")
	.attr({
		class: "yaxis",
		transform: `translate(${padding}, 0)`
	})
	.call(yAxis);

/*define line*/
let lines = d3.svg
	.line()
	.x(d => xScale(d[0]))
	.y(d => yScale(d[1]))
	.interpolate("monotone");

/*append line*/
let path = svg.append("path").attr({
	d: lines(dataset),
	class: "lineChart"
});

svg
	.select(".lineChart")
	.style("opacity", 0)
	.transition()
	.duration(2500)
	.delay(1000)
	.style("opacity", 1);

/*add points*/
let points = svg
	.selectAll("circle")
	.data(dataset)
	.enter()
	.append("circle")
	.call(drag);

/*point attributes*/
points
	.attr("cy", 0)
	.transition()
	.duration(1500)
	.delay((d, i) => i * 100 + 500)
	.ease("elastic")
	.attr({
		cx: d => xScale(d[0]),
		cy: d => yScale(d[1]),
		r: 7,
		class: "datapoint",
		id: (d, i) => i
	})
	.style("opacity", 1);

let xMax = d3.max(dataset, d => d[0]),
	yMax = d3.max(dataset, d => d[1]);

function dragstarted() {
	d3.event.sourceEvent.stopPropagation();
	d3.select(this).classed("dragging datapoint", true);
}

function dragged() {
	d3.select(this).attr({
		cx: Math.max(padding, Math.min(d3.event.x, w - padding)),
		cy: Math.max(padding, Math.min(d3.event.y, h - padding))
	});
}

function dragended() {
	d3.select(this).classed("datapoint", true);
	// get id of dragged point
	let id = d3.select(this).attr("id"),
		// get new absolute position coordinates of the point
		xPos = d3.select(this).attr("cx"),
		yPos = h - d3.select(this).attr("cy");

	// convert absolute position coordinates relative to scales
	xPos = (xPos - padding) * (xMax / (w - padding * 2));
	yPos = (yPos - padding) * (yMax / (h - padding * 2));
	dataset[id][0] = xPos;
	dataset[id][1] = yPos;

	// update line
	svg
		.select(".lineChart")
		.transition()
		.duration(500)
		.attr("d", lines(dataset));
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console