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

              
                <div class="container">
  <div class="chart"></div>
<!-- from
<a href="https://stackoverflow.com/tags/heatmap/info">https://stackoverflow.com/tags/heatmap/info</a> -->
  <div class="debug"></div>
 </div>
              
            
!

CSS

              
                body {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #DED3BF;
}

.container {
  margin-top: 30px;
}

h3 {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 14px;
}

.xGrid rect, .yGrid rect {
  fill: none;
  stroke: url(#svgGradient);
}
              
            
!

JS

              
                // Initial setup / set margins by convention
const margin = {top: 20, right: 20, bottom: 20, left: 20},
    width = 500 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    svg = d3.select(".chart")
      .append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom);

function first48(date) {
  let year = date.getFullYear();
  return (year >= 1900) && (year < 1948);
}

function x(year) {
  let xScale = d3.scaleBand()
  .domain(d3.range(12))
  .range([0, width])
  .padding(0.3);
  // divide 48 years into 12 sets
  let x12 = xScale(year % 12) + (xScale.bandwidth() / 2);
  // add a random offset
  let rand = (Math.random() - 0.5) * xScale.bandwidth();
  return x12 + rand;
}

function y(month) {
  let yScale = d3.scaleBand()
  .domain(d3.range(12))
  .range([0, height])
  .padding(0.5);

  let y12 = yScale(month) + (yScale.bandwidth() / 2);
  let rand = (Math.random() - 0.5) * yScale.bandwidth();
  return y12 + rand;
}

function createGradient(svg) {
   // stroke gradients
  var defs = svg.append("defs");

  var gradient = defs.append("linearGradient")
    .attr("id", "svgGradient")
    .attr("x1", "43%")
    .attr("x2", "67%")
    .attr("y1", "33%")
    .attr("y2", "44%")
    .attr("spreadMethod", "reflect");

  gradient.append("stop")
    .attr('class', 'start')
    .attr("offset", "0%")
    .attr("stop-color", "#AA7B5D")
    .attr("stop-opacity", 1);

  gradient.append("stop")
    .attr('class', 'end')
    .attr("offset", "100%")
    .attr("stop-color", "#F2BFA7")
    .attr("stop-opacity", 1);
}

function createGrid(svg) {
  // create gradient for grid
  createGradient(svg);
  
  // do grid
  let xGridScale = d3.scaleBand()
    .domain(d3.range(12))
    .range([0, width]);
  
  svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .attr("class", "xGrid")
    .selectAll("rect")  
    .data(d3.range(12))
    .enter()
    .append("rect")
    .attr("width", xGridScale.bandwidth())
    .attr("height", height)
    .attr("x", d => xGridScale(d));
  
   let yGridScale = d3.scaleBand()
    .domain(d3.range(12))
    .range([0, height]);
  
  svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .attr("class", "yGrid")
    .selectAll("rect")  
    .data(d3.range(12))
    .enter()
    .append("rect")
    .attr("height", yGridScale.bandwidth())
    .attr("width", width)
    .attr("y", d => yGridScale(d));
}

function createColors(d) {
  return d3.scaleQuantize()
    .domain(d3.extent(d, d => d.temp))
    .range(["#AE8057", "#4B876B", "#1C0800", "#650001"]);
}

function createChart(d) {
  createGrid(svg);
  let colors = createColors(d);
  
  // add circles
  let chart = svg
    .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
      
  chart.selectAll("circle")
    .data(d)
    .join("circle")
      .attr("class", "circle")
      .attr("cx", d => x(d.date.getFullYear()))
      .attr("cy", d => y(d.date.getMonth()))
      .attr("r", () => Math.random() * 9 + 4)
      .attr("fill-opacity", "0.7")
      .attr("fill", d => colors(d.temp));
  
  // TODO: Use CSS patterns to give the circles texture?
  
  // document.querySelector(".debug").innerText = JSON.stringify(d);
}

d3.csv( "https://gist.githubusercontent.com/Fil/15e57d2584b618521d173d4c0088d13b/raw/2f7f1a236c074635435cc7ebf9253c20a5681690/data.csv",
  ({date, temp}) => ({date: new Date(date), temp: +temp}))
  .then(d => d.filter(el=>el.temp && first48(el.date)))
  .then(createChart);



              
            
!
999px

Console