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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


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


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.


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.


                <h1>D3 Axis Tick Tricks</h1>
<p>You aren’t limited to using just one axis per axis in D3—(ab)using multiple axes is a key technique to building more descriptive labeling systems.</p>
<p>Here, there are three axes: one for the hour tickmarks, one for the day tickmarks, and one for the day labels. The day labels are positioned between the tickmarks, and custom formats are applied to hours and dates with <code>.tickFormat()</code>—displaying the month on the first label, or customizing the hour display. Take a look at the liberally-commented code if you’re curious. Tested in latest Chrome/Firefox/Safari.</p>



                @import "bourbon";

@font-face {
    font-family: 'FontAwesome';
    src: url('');
    font-weight: normal;
    font-style: normal;

html, body {
  min-width: 100%;
  min-height: 100vh;
  padding: 0;
  margin: 0;
  font-family: "Source Sans Pro";
  font-weight: 300;

body {
  // get on the unprefixed train, Safari.
  display: -webkit-flex;
  -webkit-justify-content: center;
  -webkit-align-items: center;
  -webkit-flex-direction: column;
  // Yay chrome and FF.
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

h1, p {
  max-width: 30rem;
  font-weight: 300;

p {
  margin-bottom: 0.5rem;
  line-height: 1.5rem;

code {
  font-family: "Source Code Pro";
  background: #f5f5f5;
  font-size: 0.9em;

svg {
  flex-grow: 0;
  flex-shrink: 0;
  height: 100px;
  width: 700px;
  margin-top: 1rem;

.axis {
  // default styling
  fill: none;
  path, line {
    shape-rendering: crispEdges;
    stroke: #eee;
    stroke-width: 1;
  // Labels
  text {
    opacity: 0;
  &.labeled text {
    opacity: 1;
  &.hours text {
    fill: #ccc;
    font-size: 1rem;
    font-family: "FontAwesome";
  &.days text {
    fill: #999;
    font-size: 0.9rem;
    text-transform: uppercase;
  // strokes for the two days axes
  &.days {
    path, line { stroke: #ccc; }
    // don't draw strokes for the labeled axis
    &.labeled {
      path, line { stroke: none; }
  .nighttime {
    fill: darken(#2980b9, 10%);
    fill-opacity: 0.25;
  .daytime {
    fill: #f1c40f;
    fill-opacity: 0.15;



                width = 700
height = 100
end = moment().startOf('day')
start = moment(end).subtract('days', 7)

# one scale to rule them all
timescale = d3.time.scale()
  .domain([start.toDate(), end.toDate()])
  .range([0, width-1])

# small tickmarks every three hours
# but only label 6a and 6p
hoursAxis = d3.svg.axis()
  .ticks(d3.time.hour, 3)
  .tickFormat((d) ->
    hours = d.getHours()
    if hours == 6
      # font awesome's sun icon
      return ''
    else if hours == 18
      # font awesome's moon icon
      return ''
      # don't label anything else
      return null)

# Larger tickmarks to denote midnights
# without labels
daysTickmarksAxis = d3.svg.axis()
  .ticks(, 1)
  .tickFormat('') # we want blank labels on the tickmarks

# the inverse of the previous
# e.g. without tickmarks, with labels
daysLabelsAxis = d3.svg.axis()
  .ticks(d3.time.hour, 12) # ticks at noon and midnight
  .tickSize(0)             # but without the actual tickmarks
  .tickFormat((d) ->
    # only draw labels at noon, between the date boundaries
    if d.getHours() == 12
      if d.getDate() == 1 or moment(d).isSame(start, 'day')
        # if the month changed
        # or it's the first label, show the month
        formatter = d3.time.format.utc('%a %d %b')
        # no month
        formatter = d3.time.format.utc('%a %d')
      return formatter(d)
      return null

# Time to draw things
svg ='body').append('svg')
  .attr('width', width)
  .attr('height', height)

# Draw all of the axes on top of one another
# Need to transform by 0.5,0.5 to ensure that
# the single-pixel axis strokes are drawn
# crisply in safari, despite specifying
# shape-rendering: crispEdges above.
hoursg = svg.append('g')
  .classed('axis', true)
  .classed('hours', true)
  .classed('labeled', true)
  .attr("transform", "translate(0.5,0.5)")

# Need the pixel dimensions between each tick
# e.g. three hours.
hoursTickSpacing = timescale(moment(start).add('hours', 3).toDate()) - 

# add day/night shading by adding elements to the dom
# for every tickmark in the hours axis.
	.insert('rect', ':first-child')
    .attr('class', (d, i) ->
          # each datum here is the date object for a given tick.
          hours = d.getHours()
          if hours < 6 or hours >= 18
            return 'nighttime'
            return 'daytime')
		.attr('x', 0)
		.attr('width', hoursTickSpacing) # pixels until the next tick
		.attr('height', 4);    
  .classed('axis', true)
  .classed('days', true)
  .attr("transform", "translate(0.5,0.5)")

  .classed('axis', true)
  .classed('days', true)
  .classed('labeled', true)
  .attr("transform", "translate(0.5,0.5)")