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.

+ 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>
	<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(d.data); })
	.attr("height", function(d) { return _svgInit.height - _y(d.data); })
	.attr('width', _svgInit.barWidth);

            
          
!
999px
🕑 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 ..................

Console