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.

            
              <html>
	<head>
		<script src="https://d3js.org/d3.v4.min.js"></script>
	</head>
	<body>
		<div id="graph"></div>
	</body>
</html>


            
          
!
            
              #graph{
	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')
	.insert('svg')
	.attr('width', _canvas.width)
	.attr('height', _canvas.height)
	.append('g')
	.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;
})]).nice();


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


// X軸
_svg.append('g')
	.attr('class', 'axis x')
	.attr('transform', 'translate(0,' + _svgInit.height + ')')
	.call(d3.axisBottom(_x));

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

// 棒グラフ描画
_svg.selectAll('.chart')
	.data(_data)
	.enter().append('rect')
	.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(){
	// アニメーション表示
	_svg.selectAll('.bar')
		.transition()
		.delay(200)
		.duration(500)
		.attr("y", function(d) { return _y(d.data); })
		.attr("height", function(d) { return _svgInit.height - _y(d.data); })
}

// アニメーション実行
animation();

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

Console