css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

		<script src="https://d3js.org/d3.v4.min.js"></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.top - _margin.bottom, //グラフの高さ
	fontSize: 8, // 文字サイズ
	barWidth: 50 // 棒グラフの横幅

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

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

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

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

// カラーを定義
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(d.name) + _x.bandwidth() / 2 - _svgInit.barWidth / 2; })
	.attr("y", function(d) { return _y(0); })
	.attr("height", function(d) { return 0; })
	.attr('width', _svgInit.barWidth);

function animation(){
	// アニメーション表示
		.attr("y", function(d) { return _y(d.data); })
		.attr("height", function(d) { return _svgInit.height - _y(d.data); })

// アニメーション実行

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