cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <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('https://s3-us-west-2.amazonaws.com/s.cdpn.io/85568/fontawesome-webfont.woff');
    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()
  .nice(d3.time.day)
  .domain([start.toDate(), end.toDate()])
  .range([0, width-1])

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

# Larger tickmarks to denote midnights
# without labels
daysTickmarksAxis = d3.svg.axis()
  .scale(timescale)
  .orient('bottom')
  .ticks(d3.time.day, 1)
  .tickFormat('') # we want blank labels on the tickmarks
  .tickSize(30)
  .tickPadding(6)

# the inverse of the previous
# e.g. without tickmarks, with labels
daysLabelsAxis = d3.svg.axis()
  .scale(timescale)
  .orient('bottom')
  .ticks(d3.time.hour, 12) # ticks at noon and midnight
  .tickSize(0)             # but without the actual tickmarks
  .tickPadding(36)
  .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')
      else
        # no month
        formatter = d3.time.format.utc('%a %d')
      return formatter(d)
    else
      return null
    )


# Time to draw things
svg = d3.select('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)")
  .call(hoursAxis)

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

# add day/night shading by adding elements to the dom
# for every tickmark in the hours axis.
hoursg.selectAll('g.tick')
	.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'
          else
            return 'daytime')
		.attr('x', 0)
		.attr('width', hoursTickSpacing) # pixels until the next tick
		.attr('height', 4);    
  
  
svg.append('g')
  .classed('axis', true)
  .classed('days', true)
  .attr("transform", "translate(0.5,0.5)")
  .call(daysTickmarksAxis)

  
svg.append('g')
  .classed('axis', true)
  .classed('days', true)
  .classed('labeled', true)
  .attr("transform", "translate(0.5,0.5)")
  .call(daysLabelsAxis)

            
          
!
999px
Loading ..................

Console