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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                
<div class="main">
  <div class="content">
      <h1>@horse_js tweets over time</h1>
      <div class="content">
        <svg id="chart"></svg>        
      </div>    
  </div>
</div>

              
            
!

CSS

              
                html, body {
  background-color: #f5eeec;
}

.main {
  width: 100%;
  font: 10px sans-serif;
  display: flex;
  text-align: center;
  font-family: 'Arvo', sans-serif;
}

h1 {
  font-size: 40px;
}

.content {
  margin: auto;
}

.horse-point {
  fill: rgba(224, 0, 120, 0.3)
}

.axis-label {
  text-anchor: middle;
}

              
            
!

JS

              
                class ScatterPlot {
  chart;
  
  constructor(el, options) {
    this.el = el;

    if (options) {
      this.data = options.data || [];
      this.tooltip = options.tooltip;
      this.pointClass = options.pointClass || '';

      this.render(options.width || 960);
    }
  }

  render(width) {    
    let margin = { top: 20, right: 20, bottom: 50, left: 60 };
    let height = 500 - margin.top - margin.bottom;
    width = width - margin.right - margin.left;
    let data = this.data

    // create the x axis
    let minDateValue = d3.min(data, d => {
      return new Date(moment(d.created_at).format('MM-DD-YYYY'));
    });
    
    let maxDateValue = d3.max(data, d => {
      return new Date(moment(d.created_at).format('MM-DD-YYYY'));
    });
    
    let x = d3.scaleTime()
      .domain([minDateValue, maxDateValue])
      .range([0, width]);
    
    let xAxis = d3.axisBottom(x).tickFormat(d3.timeFormat('%b-%y'));
    
    // create the y axis
    let minTimeValue = new Date().setHours(0, 0, 0, 0);
    let maxTimeValue = new Date().setHours(23, 59, 59, 999);
    
    let y = d3.scaleTime()
      .domain([minTimeValue, maxTimeValue])
      .nice(d3.timeDay)
      .range([height, 0]);
    
    let yAxis = d3.axisLeft(y).ticks(24).tickFormat(d3.timeFormat('%H:%M'));

    // create the main chart element
    this.chart = d3.select(this.el)
      .attr('width', width + margin.right + margin.left)
      .attr('height', height + margin.top + margin.bottom);

    // create the main area that holds the chart data
    let main = this.chart.append('g')
      .attr('transform', `translate(${margin.left}, ${margin.top})`)
      .attr('width', width)
      .attr('height', height)
      .attr('class', 'main');

    // append the x axis
    main.append('g')
      .attr('transform', `translate(0, ${height})`)
      .attr('class', 'main axis date')
      .call(xAxis);
    
    // text label for the x axis
    this.chart.append("text")             
      .attr("transform",
            "translate(" + ((width/2) + margin.left) + " ," + 
                           (height + margin.top + 40) + ")")
      .attr('class', 'axis-label')
      .text("Date Of Tweet");

    // append the y axis
    main.append('g')
      .attr('transform', 'translate(0,0)')
      .attr('class', 'main axis date')
      .call(yAxis);
    
    // text label for the y axis
    this.chart.append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 0)
      .attr("x",0 - (height / 2))
      .attr("dy", "1em")
      .attr('class', 'axis-label')
      .text("Time of Tweet - CST (-6)");      
    
    // create a circle for each data point
    let circles = main.append('g').attr('class', 'scatter-points');
    
    data.forEach(item => {
      circles.append('svg:circle')
      .attr('class', 'horse-point')
      .attr('cx', d => {
        return x(new Date(item.created_at));
      })
      .transition()
      .duration(Math.floor(Math.random() * (3000-2000) + 1000))
      .ease(d3.easeBounce)
      .attr('cy', d => {
         let today = new Date();
         let time = new Date(item.created_at);
         return y(today.setHours(time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds()));
      })
      .attr('r', 5);
    });
  }
}

// get the data
let data = fetch('https://s3-us-west-2.amazonaws.com/s.cdpn.io/4548/time-series.json').then(d => d.json()).then(data => {

  // masage the data a bit to get it in the right format
  let horseData = data.map(item => {
    return item.horse;
  })

  // create the chart
  let chart = new ScatterPlot('#chart', {
    data: horseData,
    width: 960
  });
})



              
            
!
999px

Console