123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

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

            
          
!
            
              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)
}

.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;
}

            
          
!
            
              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: 15, bottom: 60, left: 60 };
    let height = this.height - margin.top - margin.bottom;
    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 x axis
    main.append('g')
      .attr('transform', `translate(0, ${height})`)
      .attr('class', 'main axis date')
      .call(xAxis);
    
    // append the y axis
    main.append('g')
      .attr('class', 'main axis date')
      .call(yAxis);
  }
}

// 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,
    pointClass: 'horse-point',
    tooltip: function(d) {
      return `
        <div class="tweet">
          <div class="tweet-header">
            <div class="tweet-author">
              <span class="tweet-screenname">@horse_js</span>
            </div>
          </div>
          <div class="tweet-body">
            <p>${d.text}</p>
            <div>
              <time>${moment(d.created_at).format('LT')} - ${moment(d.created_at).format('MMMM Do YYYY')}</time>
            </div>
          </div>
        </div>
      `
    }
  });
})



            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console