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.

              <h1>College Tuition Over Ten Years</h1>
<h3>In-state Tuition at Flagship Universities 2007-2017 </h3>
<p>Adjusted to 2017 Dollars</p>
<div class="chart"></div>
<div class="key">
  <span id="q1color" class="color">Quartile 1</span>
  <span id="q1"></span>
<div class="key">
  <span id="q2color" class="color">Quartile 2</span>
  <span id="q2"></span>
<div class="key">
  <span id="q3color" class="color">Quartile 3</span>
  <span id="q3"></span>
<div class="key">
  <span id="q4color" class="color">Quartile 4</span>
  <span id="q4"></span>
<p>Data Source: <a href="https://trends.collegeboard.org/college-pricing/figures-tables/tuition-fees-flagship-universities-over-time">College Board</a></p>
              body {
  font-family: Arial, Helvetica, sans-serif;
  text-align: center;
  color: #0a1c02;
  background: #d6cdb7;

div.tooltip {
  position: absolute;
  text-align: left;
  width: 200px;
  height: 70px;
  padding: 10px;
  font-size: 14px;
  font-weight: bold;
  background: #c1d6b7;
  border: 0px;
  border-radius: 8px;
  pointer-events: none;
  top: -35%;
  left: 135%;
  transform: translateX(10%);
  margin-top: -10px;

a {
  text-decoration: none;

.chart {
  margin-bottom: 20px;

.key {
  text-align: left;
  margin-left: 30%;
  margin-bottom: 10px;
  font-weight: bold;

.color {
  padding: 5px;
  border-radius: 2px;
  margin-right: 10px;

#q1color {
  background: #bdb7d6;

#q2color {
  background: #948DB3;

#q3color {
  background: #605885;

#q4color {
  background: #433B67;
              //data from https://trends.collegeboard.org/college-pricing/figures-tables/tuition-fees-flagship-universities-over-time
//heat map showing tuition increases over ten years 2017 adjusted dollars

d3.json("https://raw.githubusercontent.com/ashlynnpai/charts/master/tuition-heatmap/data.json", function(response) {
    var data = response;

//set up chart dimensions
    const cellSize = 20;
    const numYears = 11;
    const margin = {top: 30, right: 0, bottom: 30, left: 50},
        width = 1000 - margin.left - margin.right,
        height = cellSize * numYears - 10;
    var x = d3.scaleBand()
        .range([0, width]);
    var y = d3.scaleTime()
        .range([height, 0]);

    const svg = d3.select(".chart").append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    //convert time to and from string
    var parseTime = d3.timeParse("%Y");
    var formatTime = d3.timeFormat("%Y");
    const years = data.map((d) => {parseTime(d.year)});

    //set the values of x and y axes
    const states = d3.set(data.map((d) => {return d.state})).values();
    y.domain(d3.extent(data, d => d.year));

    //takes the range of tuition values and divides by the number of colors
    //assigns a color for each range of values
    var color = d3.scaleQuantize()
    .domain(d3.extent(data, d => d.tuition))
    .range(["#bdb7d6", "#948DB3", "#605885", "#433B67"]);

    //draw the chart
        .attr('width', cellSize)
        .attr('height', cellSize)
        .attr('x', d => x(d.state))
        //put the cells on top of the y increments to prevent x-axis labels overlapping
        .attr('y', d => y(d.year) - cellSize)
        //set colors based on tuition
        .attr('fill', d => color(d.tuition))
        .style("stroke", "#d6cdb7")
        .on("mouseover", function(d) {
            .style("opacity", .9);
            div.html(d.uni + "<br/>$" + d.tuition)
            .style("left", (d3.event.pageX) + "px")
            .style("top", (d3.event.pageY - 28) + "px");
        .on("mouseout", function(d) {
            .style("opacity", 0)

    //draws the axes
    var xAxis = d3.axisBottom(x);
    var yAxis = d3.axisLeft(y)

        .classed('x axis', true)
        .attr('transform', 'translate(0,' + height + ')')

        .classed('y axis', true)

    //draws the tooltips
    var div = d3.select(".chart").append("div")
        .attr("class", "tooltip")
        .style("opacity", 0);

    //make the legend showing tuition ranges for heat map colors
    var tuition = d3.set(data.map((d) => {return d.tuition})).values();
    tuition.sort(function(a,b){return a - b})
    var q1 = d3.quantile(tuition, .25);
    var q2 = d3.quantile(tuition, .5);
    var q3 = d3.quantile(tuition, .75);
    var q4 = d3.quantile(tuition, 1);
    d3.select("#q1").node().innerHTML = "$0 - $" + q1;
    d3.select("#q2").node().innerHTML = "$" + q1 + " - $" + q2 ;
    d3.select("#q3").node().innerHTML = "$" + q2 + " - $" + q3 ;
    d3.select("#q4").node().innerHTML = "$" + q3 + " - $" + q4 ;

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