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 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

              
                <body>
    <div id="timeline"></div>
	<img class="original-map" src="http://www.slate.com/content/dam/slate/blogs/the_vault/2013/8/12/HistomapFinal.jpg.CROP.article920-large.jpg"/>
</body>
<script>
    function getMetrics() {
        var metrics = ["2000bc", "1950bc", "1900bc", "1850bc", "1800bc", "1750bc", "1700bc", "1650bc", "1600bc", "1550bc", "1500bc", "1450bc", "1400bc", "1350bc", "1300bc", "1250bc", "1200bc", "1150bc", "1100bc", "1050bc", "1000bc", "950bc", "900bc", "850bc", "800bc", "750bc", "700bc", "650bc", "600bc", "550bc", "500bc", "450bc", "400bc", "350bc", "300bc", "250bc", "200bc", "150bc", "100bc", "50bc", "0ad", "50ad", "100ad", "150ad", "200ad", "250ad", "300ad", "350ad", "400ad", "450ad", "500ad", "550ad", "600ad", "650ad", "700ad", "750ad", "800ad", "850ad", "900ad", "950ad", "1000ad", "1050ad", "1100ad", "1150ad", "1200ad", "1250ad", "1300ad", "1350ad", "1400ad", "1450ad", "1500ad", "1550ad", "1600ad", "1650ad", "1700ad", "1750ad", "1800ad", "1850ad", "1900ad", "~1950ad", "~2000ad"];
        return metrics;
    };
</script>
              
            
!

CSS

              
                body {
	overflow: visible;
}

.axisYears path {
	fill: none;
}

.tick line {
	stroke: black;
}

$timeline-width: 800px;
#timeline {
	float: left;
	width: $timeline-width;
	padding-top: 30px;
	/* 	border: 1px solid green; */
}

.original-map {
	position: absolute;
	top: 0;
	left: $timeline-width;
	height: 2830px;
	float: left;
	/* 	border: 1px solid red; */
}
              
            
!

JS

              
                d3.json("https://codepen.io/cmchap/pen/qddbOQ.js",
        
function (err, jsonResponse) {

    // NOTE: d is usually "current datum", per d3 convention.
    // e is usually something like "current array element".
    // i is usually "index of current datum or array element"

    //Grab our data
    var civData = jsonResponse,
        metrics = getMetrics();

    // Set some dimensions for our chart
    var width = 500,
        height = width * 5.3,
        padding = width/5;

    // Create the svg.
    // We make it a little bigger than the chart will be to account for labels overflowing (for now)
    var svg = d3.select("#timeline").append("svg")
        .attr({
        "width": width + 2 * padding,
            "height": height + 2 * padding
            })

    // Create a scale for the x axis of our chart.
    // It scales from the layout scale (0,1) to the chart scale (0,width)
    // It also reverses it. I'm a little unclear on why.
    var xScale = d3.scale.linear()
        .domain([0, 1])
        .range([width, 0]);

    // Create a scale for the y axis of our chart, as above.
    var timeScale = d3.scale.linear()
        .domain([0, metrics.length - 1])
        .range([0, height]);

    // The stack function takes an array (layers) of arrays of objects (datapoints in each layer)
    // This bit constructs that array of arrays.

    civData.forEach(function (v, i, a) {
        v.layer = metrics.map(function (metric, i) {
            return {
                x: i,
                y: v.metrics[metric]
            };
        });
    });

    // Prep the stack layout function. https://github.com/mbostock/d3/wiki/Stack-Layout
    // "expand" tells it to fill out the (0,1) space
    // "reverse" tells it to stack the layers in reverse order. I'm a little unclear on why.
    var stack = d3.layout.stack()
        .offset("expand")
        .order("reverse")
        .values(function (d) {
        return d.layer;
    });

    // Use the stack function we configured earlier to lay out the chart
    layers = stack(civData);

    // The area function constructs a polygon from two lines, more or less.
    var area = d3.svg.area()
    // .y tells it to use the same y value for both lines, and to get it this way
    .y(function (d) {
        return timeScale(d.x) + padding;
    })
    // .x0 tells it to get the x value for the left line this way
    .x0(function (d) {
        return xScale(null) + padding;
    })
    // .x1 tells it to get the x value for the right line this way
    .x1(function (d) {
        return xScale(d.y0 + d.y) + padding;
    });

    // NOTE: Area and stack sort of think of x and y differently.
    // Area means it literally: x and y on your svg/canvas.
    // Stack is more like y=direction things are stacked and x=direction things flow

    //Draw layers
    svg.selectAll("path")
        .data(layers)
        .enter().append("path")
        .attr("d", function (d) {
        return area(d.layer);
    })
        .style({
        "fill": function (d) {
            return d.color;
        },
            "stroke": function (d) {
            return d3.hsl(d.color).darker(0.25);
        },
            "stroke-width": width / 200
    });

    //Draw text
    svg.selectAll("text")
        .data(layers)
        .enter().append("text")
        .each(function (d) {
        // Figure out what the layer's highest value is
        var max = d3.max(d.layer, function (e) {
            return e.y;
        });
        // Figure out at what index that occurs
        var index = d.layer.map(function (e) {
            return e.y;
        }).indexOf(max);
        // Place the text halfway between the left and right edges of the layer at that index
        var x = xScale(d.layer[index].y / 2 + d.layer[index].y0) + padding;
        // Place the text at that index, vertically
        var y = timeScale(d.layer[index].x) + padding;
        d3.select(this).attr({
            "x": x,
                "y": y,
                "text-anchor": "middle",
            "class": "civName"
        });
    })
        .text(function (d) {
        return d.name;
        });

    //Create scale our of metrics
    var yearScale = d3.scale.ordinal().domain(metrics).rangePoints([0, height]);

    //Create left axis
    var leftAxis = d3.svg.axis().orient("left").scale(yearScale);
    svg.append("g")
        .attr("transform", "translate(" + padding + "," + padding + ")")
        .attr("class", "axisYears")
        .call(leftAxis);

    //Create right axis
    var rightAxis = d3.svg.axis().orient("right").scale(yearScale);
    svg.append("g")
        .attr("transform", "translate(" + (padding + width) + "," + padding + ")")
        .attr("class", "axisYears")
        .call(rightAxis);
    
    //Set civilization name font size relative to chart width
    d3.selectAll(".civName")
        .style("font-size", function () {
            return width/30;
        });
    
    // Set axis font size relative to chart width
    d3.selectAll(".axisYears")
        .style("font-size", function () {
            return width/20;
        });
});
              
            
!
999px

Console