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

              
                <link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital@0;1&display=swap" rel="stylesheet">
<div id="chart-container">
  <div id="event-chart"></div>
</div>
<div id="intersection-zone"><div>
<div id="info"></div>
              
            
!

CSS

              
                body {
  margin: 0;
  padding: 0;
  height: 100%;
  padding-bottom: 1000px;
  font-family: 'Roboto', sans-serif;
}
div {
/*   border: 1px solid silver; */
}
#chart-container {
  position: fixed;
  top: 0;
  background-color:rgba(255, 255, 255, 0.9)
}
#info {
  display: flex;
  flex-direction: column;
  margin-top: 300px;
}
.start-line {
  stroke: #666;
  stroke-dasharray: 5, 5;
}
.line-event {
  fill: skyblue;
  fill-opacity: 0.2;
  stroke: skyblue;
}
.line-trend {
  fill: lime;
  fill-opacity: 0.05;
  stroke: lime;
}
.dot-event {
  fill: skyblue;
  r: 5;
  fill-opacity: 0.5
}
.dot-event:hover {
  fill: orange;
  r: 10;
}
.entry {
  display: flex;
  justify-content: center;
  width: 100%;
}
.bubble {
  fill: orange;
  fill-opacity: 0.5;
}
.domain, .tick line {
  stroke: #ccc;
}
.tick text {
  fill: #666;
}
.event-card {
  display: flex;
  border: 1px solid #ccc;
  padding: 16px;
  height: 140px;
  margin: 0 32px;
  box-shadow: 2px 4px 6px #eee;
  flex: 0 0 75%;
  max-width: 600px;
}
.event-image {
  display: flex;
  width: 120px;
  height: 120px;
  border: 1px solid silver;
  margin-right: 16px;
}
.event-info {
  display: flex;
  flex: 1;
  flex-direction: column;
}
.event-info a {
  color: black;
}
.event-title {
  color: #aaa;
  font-size: 18px;
}
.event-author {
  color: #aaa;
}
.event-date {
  color: #aaa;
  margin-bottom: 16px;
}
.event-metadata {
 margin-bottom: 8px; 
}

.event-quote {
  font-style: italic;
}
              
            
!

JS

              
                /*
TODO:
* image for each
* tooltip on line chart
* add testimonies
* make responsive
x solve the d3 pre-history
X quote for each
X make example chart cumulative
x d3 trend line
*/

const eventSheet = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRSeXTlU_N1XvIYKjmoF6AQBYbn14V3fZolFHfYrAvpUZybsgr4GBJhoAvlo-gKoiQfVcTaixhKLg0Q/pub?gid=0&single=true&output=csv"

const exampleSheet = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRSeXTlU_N1XvIYKjmoF6AQBYbn14V3fZolFHfYrAvpUZybsgr4GBJhoAvlo-gKoiQfVcTaixhKLg0Q/pub?gid=1287116229&single=true&output=csv"

const trendSheet = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRSeXTlU_N1XvIYKjmoF6AQBYbn14V3fZolFHfYrAvpUZybsgr4GBJhoAvlo-gKoiQfVcTaixhKLg0Q/pub?gid=572073882&single=true&output=csv"

const margin = { top: 20, right: 40, bottom: 20, left: 20 },
  bubbleSize = 10,
  cardHeight = 200

let svg = null

const drawEventChart = async () => {
  const width = window.innerWidth - margin.left - margin.right,
    height = 290 - margin.top - margin.bottom,
    center = height / 2
  
  // Data and scales
  ///////////////////////
  
  const data = await d3.csv(eventSheet)
  data.forEach((d) => {
    d.Date = new Date(d.Date)
  })
  
  const exampleData = await d3.csv(exampleSheet)
  exampleData.forEach((d) => {
    d.Date = new Date(d.Date)
    d.Count = +d.Count
    d.Cumulative = +d.Cumulative
  })

  const trendData = await d3.csv(trendSheet)
  trendData.forEach((d) => {
    d.Date = new Date(d.Date)
    d.Count = +d.Count
  })
  
  const xExtent = d3.extent(exampleData, d => d.Date)

  const x = d3.scaleTime()
    .range([0, width])
    .domain(xExtent)
    .clamp(true)

  // Base chart
  ///////////////////////
  
  svg = d3.select("#event-chart")
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
  
  svg.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x))
  
  // D3js trend
  ///////////////////////
  
  const trendY = d3.scaleLinear()
    .range([height, 0])
    .domain([0, d3.max(trendData, (d) => d.Count)])
  
  const trendLine = d3.area()
    .x((d) => x(d.Date))
    .y1((d) => trendY(d.Count))
    .y0(height)

  svg.append("path")
    .data([trendData])
    .attr("class", "line-trend")
    .attr("d", trendLine)
  
  // Bl.ocks chart
  ///////////////////////
  
  const exampleY = d3.scaleLinear()
    .range([height, 0])
    .domain([0, d3.max(exampleData, (d) => d.Cumulative)])
    
  const exampleLine = d3.area()
    .x((d) => x(d.Date))
    .y1((d) => exampleY(d.Cumulative))
    .y0(height)

  svg.append("path")
    .data([exampleData])
    .attr("class", "line-event")
    .attr("d", exampleLine)
  
  // Start line
  ///////////////////////
  const d3LaunchDate = new Date("2011-02-17")
  svg.append("line")
    .attr("class", "start-line")
    .attr("x1", x(d3LaunchDate))
    .attr("y1", exampleY.range()[0])
    .attr("x2", x(d3LaunchDate))
    .attr("y2", exampleY.range()[1])

  // Event floating bubbles
  ///////////////////////
  
  const nodes = data.map(function (d, i) {
    return {
      Date: d.Date,
      x: x(d.Date),
      y: center,
      r: bubbleSize,
      ...d
    }
  })
  
  let bubbles = svg.selectAll(".bubble")
    .data(nodes)

  const bubblesEnter = bubbles.enter().append("circle")
    .classed("bubble", true)
    .attr("r", 0)
    .on("click", (e, d) => {
      window.scrollTo({ top: d.index * cardHeight, behavior: 'smooth' })
    })
    .on("mouseenter", function(e, d) {
      console.log("enter")
      d.r = bubbleSize * 1.5
      force.force("collide", d3.forceCollide(d => d.r))
      force.alpha(0.1).restart()
    })
    .on("mouseout", function(e, d) {
      d.r = bubbleSize
      force.force("collide", d3.forceCollide(d => d.r))
      force.alpha(0.1)
    })

  bubbles = bubbles.merge(bubblesEnter)

  bubbles.transition()
    .duration(500)
    .attr("r", bubbleSize)

  function ticked() {
    console.log("ticked")
    bubbles
      .attr("cx", d => d.x)
      .attr("cy", d => d.y)
      .attr("r", d => d.r)
  }

  const force = d3.forceSimulation(nodes)
    .force("forceX", d3.forceX(d => d.x).strength(1))
    .force("forceY", d3.forceY(d => d.y).strength(0))
    .force("y", d3.forceY().y(center))
    .force("collide", d3.forceCollide(d => d.r))
    .on("tick", ticked)
  
  // Event list
  ///////////////////////
  
  const format = d3.timeFormat("%b %d %Y")

  const info = d3.select("#info")
    .selectAll("div.entry")
    .data(data)
    .enter().append("div")
    .attr("class", "entry")
    .style("height", `${cardHeight}px`)
    .html((d) => {
      return `<div class="event-card">
        <div class="event-image"><img src="${d.Image}" /></div>
        <div class="event-info">
          <div class="event-title">
            <a href="${d.Link}">${d.Event}</a><br>
          </div>
          <div class="event-metadata">
            <span class="event-author">${d.Author}</span>
            <span class="event-date">${format(d.Date)}</span>
          </div>
          <div class="event-quote">“${d.Quote}”</div>
        </div>
      </div>`
    }) 
}

drawEventChart()

// Scroll interaction
///////////////////////

function onScroll(scrollPos) {
  const scrollPosY = window.scrollY
  const cardIndex = Math.floor(scrollPosY / cardHeight)
  console.log(cardIndex)
  if (!svg) return
  svg.selectAll(".bubble")
    .style("fill", "orange")
    .filter((d, i) => i === cardIndex)
    .style("fill", "red")
}

function throttle(fn, wait) {
  var time = Date.now();
  return function() {
    if ((time + wait - Date.now()) < 0) {
      fn();
      time = Date.now();
    }
  }
}

document.addEventListener('scroll', throttle(onScroll, 10))
              
            
!
999px

Console