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">
  <h3>Drone Trends</h3>
  <div class="chart"></div>
  <div id="tooltip" class="hidden">
    <div id="tt-date"></div>
    <div id="tt-data">
      <table>
      </table>
    </div>
  </div>  
  <div class="debug"></div>
</div>
              
            
!

CSS

              
                body {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.container {
  margin-top: 30px;
  position: relative;
}

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

.debug {
  width: 500px;
  font-size: 10px;
}

.line {
  fill: none;
  stroke-width: 3px;
}

.axis .domain {
  display: none;
}

.axis .tick {
  color: #C8C8C8;
  shape-rendering: crispEdges;
}

.xaxis .tick line {
  display: none;
}

.yaxis .tick:not(:first-of-type) line {
  stroke-opacity: 0.3;
}

.overlay {
  fill: none;
  pointer-events: all;
}

#tooltip {
  position: absolute;
  padding: 5px;
  font: 10px sans-serif;
  background: rgba(255,255,255,.8);
  border: 1px solid black;
  pointer-events: none;
}

.hidden {
  visibility: hidden;
}

#tt-date {
  font-weight: bold;
  padding-bottom: 5px;
}
              
            
!

JS

              
                // set margins by convention
const margin = {top: 50, right: 80, bottom: 70, left: 50},
      width = 700,
      height = 300;

const URL = "https://gist.githubusercontent.com/fraziern/95fc8cfce299accd608928caf65f134f/raw/42095ee60cf540b78b37fc33017d03632af9f77d/trendsdata.csv";

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

const chart = svg
  .append("g")
  .attr("transform", `translate(${margin.left},${margin.top})`);

const tooltip = d3
  .select('#tooltip');

// initialize the ranges
const x = d3.scaleUtc()
  .range([0, width])
  .clamp(true);  // prevent rollover OB errors

const y = d3.scaleLinear()
  .domain([0, 100])
  .range([height, 0]);

function createChart(rawData) {

  // data wrangling
  const categories = d3.keys(rawData[0]).filter(d => d !== "Week" && d !== "date");
  const data = categories.map(c => ({
    category: c,
    values: rawData.map(d => ({
      date: d.date,
      value: d[c]
    }))
  }));
  
  x.domain(d3.extent(data[0].values, d => d.date));
  
  // add axes
  let yAxisGenerator = d3.axisLeft(y).tickSize(-width).ticks(5);
  svg.append('g')
    .attr("class", "axis yaxis")
    .attr("transform", `translate(${margin.left - 10},${margin.top})`)
    .call(yAxisGenerator);
  
  let xAxisGenerator = d3.axisBottom(x);
  svg.append('g')
    .attr("class", "axis xaxis")
    .attr("transform", `translate(${margin.left},${margin.top + height})`)
    .call(xAxisGenerator);
  
  // colors
  const color = d3.scaleOrdinal(d3.schemeCategory10)
    .domain(categories);
  
  const valueline = d3.line()
    .x(d => x(d.date))
    .y(d => y(d.value));
  
  // Add the paths.
  const name = chart.selectAll(".name")
      .data(data)
    .join("g")
      .attr("class", "name");
  
  name.append("path")
      .attr("class", "line")
      .attr("d", d => valueline(d.values))
      .style("stroke", d => color(d.category));
 
  // rollover implementation
  // Create data rows based on categories
  let ttrows = d3.select('#tt-data table')
      .selectAll('tr')
      .data(categories)
      .join('tr');

  svg.on("touchmove mousemove", mousemove);
  svg.on("touchend mouseleave", mouseout);
  
  // helper function for locating the nearest point in the data
  // corresponding to the mouse position
  // https://github.com/d3/d3-array#bisector
  const bisectDate = d3.bisector(d => d.date).left;
  
  function mousemove() {
    // get date corresponding to mouse x position
    const date = x.invert(d3.mouse(this)[0]);
    
    // get data
    const index = bisectDate(rawData, date, 1);
    // document.querySelector(".debug").innerText = JSON.stringify(index);
    const datapoint = rawData[index];
    
    // display date
    d3.select('#tt-date').text(datapoint.Week);
    
    // add rollover data rows, clearing old ones first
    ttrows.selectAll('td').remove();
    
    ttrows.each(function (d) {
      d3.select(this).append('td').text(d);
      d3.select(this).append('td')
        .text(datapoint[d])
        .style("color", d => color(d));
    });
    
    // display rollover
    d3.select('#tooltip')
      .style("left", x(datapoint.date) + "px")
      .style("top", width/2 + "px")
      .classed('hidden', false);
  }
  
  function mouseout() {
    d3.select('#tooltip')
      .classed('hidden', true);
  }
}

d3.csv(URL, d3.autoType)
  .then(function (data) {
    data.forEach(el => {
      el.date = new Date(el.Week)
    });
    return data;
})
  .then(createChart);


              
            
!
999px

Console