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.

 * I started with Scott Murray's tutorial, here: http://alignedleft.com/tutorials/d3/making-a-bar-chart
 * Then I introduced es6, pulled out some common functions, added scales and color scales.
 * To get good contrasting colors for the labels, used some methods from tinycolor.
 * I walk through the changes on my blog: http://billkidwell.com/post/2016-07-04-some-fun-with-d3-scales/

// The canvas represents the entire SVG
var canvas = { w: 700, h: 250 };
// Chart area is where the primary chart graphic appears
var chart = { w: 500, h: 100, x: 100, y: 100 };

var barPadding = 5;
var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
							11, 12, 15, 20, 25, 17, 16, 18, 23, 25 ];

var x = d3.scale.ordinal()
          .rangeBands([0, chart.w], .1);

var y = d3.scale.linear()
          .range([chart.h, 0]);

var blueScale = d3.scale.linear()
                    .domain([d3.min(dataset), d3.max(dataset)])
                    .range(["lightblue", "darkblue"]);

var inverseBlue = d3.scale.linear()
                    .domain([d3.max(dataset), d3.min(dataset)])
                    .range(["lightblue", "darkblue"]);

function contrastingColor(d) {
  var barColor = d3.rgb(blueScale(d));
  var color = d3.rgb(inverseBlue(d)); // start with the color from the inverse scale
  if (isDark(barColor)) return color.brighter(2);
  else return color.darker(2);

x.domain(dataset.map( (d) => d ));
y.domain([0, d3.max(dataset)]);


var labelFont = {
  family: 'sans-serif',
  size: '11px'

function font(selection, fontInfo) {
  selection.attr('font-family', fontInfo.family)
           .attr('font-size', fontInfo.size)
  return selection;

function fill(selection, fillColor) {
  selection.attr("fill", fillColor);
  return selection;

// Thanks to tinycolor
function getBrightness(color) {
  var rgb = d3.rgb(color)
  return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000;

// Thanks to tinycolor
function isDark(color) {
  return getBrightness(color) < 128;

// Thanks to tinycolor
function isLight(color) {
  return !isDark(color);
//Create SVG element
var svg = d3.select("body")
						.attr("width", canvas.w)
						.attr("height", canvas.h);

var group = svg.append("g")
                .attr("transform", "translate(" + chart.x + "," + chart.y + ")");

   .attr("x", (d) => x(d))
   .attr("y", (d) => y(d))
   .attr("width", x.rangeBand())
   .attr("height", (d) => chart.h - y(d) )
   .call(fill, (d) => blueScale(d));

     .text((d) => d )
     .attr("x", (d, i) => x(d) + 0.5*x.rangeBand() ) // center of range
     .attr("text-anchor", "middle") // bottom middle is anchor
     .attr("y", (d) => y(d) + 15 )
     .call(font, labelFont)
     .call(fill, (d)=> contrastingColor(d));

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