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 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

Packages

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.

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 id="activity"></div>

              
            
!

CSS

              
                #activity, #activity svg {
  margin-left: auto;
  margin-right: auto;
}

#activity {
  width: 450px;
  margin: 5rem auto 0 auto;
}

body {
  background-color: black;
}
              
            
!

JS

              
                /* Statistics */
const stats = [
 {
    name: 'Moving',
    value: 122,
    goal: 350,
    perc: 0.35,
    unit: 'kcal',
    color: 'hotpink'
  }, {
    name: 'Exercising',
    value: 40,
    goal: 40,
    perc: 1.00,
    unit: 'min',
    color: 'limegreen'
  }, {
    name: 'Standing',
    value: 9,
    goal: 12,
    perc: 0.75,
    unit: 'h',
    color: 'turquoise'
  }
];

/* Define icons */
const icons = {
  moving: '↦',
  exercising: '↠',
  standing: '↟'
};

/* Chart configuration. */
const width = 450;
const height = 450;
const margin = 40;
const circleStroke = 50;
const circleSpace = 2;

/* Define Math.PI * 2 because I don't like typing it out. */
const fullCircle = Math.PI * 2;

/* Create the chart. */
const chart = d3.select('#activity').append('svg')
  .attr('width', width)
  .attr('height', height)
  .attr('role', 'img') // SR support
  .attr('aria-labelledby', 'activityTitle activityDesc'); // SR support


/* Add title. */
chart.append('title')
  .text('Activity')
  .attr('id', 'activityTitle');

/* Generate the description. */
const generateDescription = () => {
  return stats.map((stat) => {
    return `${stat.name}: ${stat.perc * 100}%.`;
  }).join(' ');
}

/* Add the description. */
chart.append('desc')
  .text(generateDescription)
  .attr('id', 'activityDesc');

/* Draw a ring with a given radius and angle. */
const drawRing = (parent, arc, color, opacity) => {
  return parent.append('path')
    .attr('d', d3.arc()
      .innerRadius(arc.inner)
      .outerRadius(arc.outer)
      .startAngle(arc.start)
      .endAngle(arc.end)
     )
    .attr('fill', color)
    .attr('opacity', opacity);
}

/* Set the radius. */
const radius = (width - margin) / 2;


/* Draw 3 rings */
stats.forEach((stat, index) => {
  /* Set the properties for the arc */
  const arc = {
    inner: radius - circleStroke * (index + 1) - circleSpace * index,
    outer: radius - circleStroke * index - circleSpace * index,
    start: 0,
    end: fullCircle
  };
 
  /* Add a group */
  chart.append('g')
    .attr('class', stat.name)
    .attr('transform', `translate(${width / 2}, ${height / 2})`);
  
  /* Draw the dark band */
  drawRing(
    d3.select(`.${stat.name}`),
    {...arc, start: fullCircle * stat.perc },
    stat.color,
    0.25
  );
  
  /* Draw the active band */
  drawRing(
    d3.select(`.${stat.name}`),
    {...arc, end: fullCircle * stat.perc},
    stat.color,
    1
  );
  
  /* Add the labels. */
  d3.select(`.${stat.name}`)
    .append('text')
    .text(icons[stat.name.toLowerCase()])
    .attr('fill', '#000')
    .attr('transform', `translate(${circleSpace}, -${(arc.outer + arc.inner) / 2 - circleSpace * (index + 2)})`)
    .attr('font-size', '1.5rem');
});
              
            
!
999px

Console