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;
  font-family: 'Karla', sans-serif;
}

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

h1 {
  font-size: 40px;
}

.content {
  margin: auto;
}

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

.chart {
  margin-top: 100px;
  overflow: visible
}

.tweet {
  font: normal normal 14px/1.4 Helvetica,Roboto,"Segoe UI",Calibri,sans-serif;
  .tweet-header {
    .tweet-author {
      font-size: 13px;
      color: #6F7888;
    }
  }
  .tweet-body {
    margin-top: 14px;
  }
  time {
    font-size: 14px;
    color: #697888;
  }
}

.d3-tip {
  line-height: 1;
  font-weight: bold;
  padding: 12px;
  background: #fff;
  color: #333;
  border-radius: 5px;
  transition: opacity 0.2s ease-in-out;
  max-width: 400px;
}

/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
  width: 100%;
  box-sizing: border-box;
  display: inline;
  font-size: 10px;
  line-height: 1;
  color: #fff;
  content: "\25BC";
  position: absolute;
  text-align: center;
}

/* Style northward tooltips specifically */
.d3-tip.n:after {
  margin: -2px 0 0 0;
  top: 100%;
  left: 0;
}

.d3-tip span {
  transition: scale 0.2s ease;
}

              
            
!

JS

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

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

      this.render();
    }
  }

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

    // create the chart
    let chart = d3.select(this.el)
      .attr('width', width + margin.right + margin.left)
      .attr('height', height + margin.top + margin.bottom);
    
    
    // define 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'));
    
    
    // define 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'));
    
    
    let main = 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
    chart.append("text")             
      .attr("transform",
            "translate(" + ((width/2) + margin.left) + " ," + 
                           (height + margin.top + margin.bottom) + ")")
      .attr('class', 'axis-label')
      .text("Date Of Tweet");
    
    // append the y-axis
    main.append('g')
      .attr('class', 'main axis date')
      .call(yAxis);
    
    // text label for the y axis
    chart.append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 10)
      .attr("x",0 - ((height / 2) + (margin.top + margin.bottom)))
      .attr('class', 'axis-label')
      .text("Time of Tweet - CST (-6)");   
    
      // draw the data
      let circles = main.append('g');

      data.forEach(item => {
        circles.append('svg:circle')
          .attr('class', 'horse-point')
          .transition()
          .attr('cx', d => {
            return x(new Date(item.created_at));
          })
          .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