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


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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


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.


		<script src=""></script>
		<div id="graph"></div>



	width: 800px;
	height: 400px;
	border: 1px solid #ccc;


                // データを設定
var _data = [
  {'name': 'A','data': 50},
  {'name': 'B','data': 100},
  {'name': 'C','data': 500},
  {'name': 'D','data': 300},
  {'name': 'E','data': 400}

// 初期設定
var _barTarget = d3.selectAll('#graph');

var _canvas = {
	width: _barTarget.node().getBoundingClientRect().width,
	height: _barTarget.node().getBoundingClientRect().height

// 枠内の余白用マージンを定義
var _margin = {top: 20, right: 20, bottom: 30, left: 40};

// svgの描画初期設定
var _svgInit = {
	width: _canvas.width - _margin.left - _margin.right, //グラフの横幅
	height: _canvas.height - - _margin.bottom, //グラフの高さ
	fontSize: 8, // 文字サイズ
	barWidth: 50 // 棒グラフの横幅

// svgの定義
var _svg ='#graph')
	.attr('width', _canvas.width)
	.attr('height', _canvas.height)
	.attr('class', 'chart')
	.attr("transform", "translate(" + _margin.left + "," + + ")");

// x軸の範囲を指定
var _x = d3.scaleBand()
	.rangeRound([0, _svgInit.width]);

var _y = d3.scaleLinear()
	.rangeRound([_svgInit.height, 0]);

// 各軸のデータ最小値・最大値を指定
_x.domain( (d) {
_y.domain([0, d3.max(_data, function (d) {

// カラーを定義
var _colorScale = d3.schemeCategory10;

// X軸
	.attr('class', 'axis x')
	.attr('transform', 'translate(0,' + _svgInit.height + ')')

// Y軸
	.attr("class", "y axis")
		d3.axisLeft(_y).ticks(10) //目盛りの数を指定

// 棒グラフ描画
	.attr('class', 'bar')
    .attr('fill', function(d,i) {
		return _colorScale[i];
	.attr("x", function(d) { return _x( + _x.bandwidth() / 2 - _svgInit.barWidth / 2; })
	.attr("y", function(d) { return _y(; })
	.attr("height", function(d) { return _svgInit.height - _y(; })
	.attr('width', _svgInit.barWidth);