JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<div id='item-container'></div>
CSS
.component-caption {
font: 14px sans-serif;
font-weight: bold;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis line.minor {
stroke: #777;
stroke-dasharray: 2,2;
opacity: 0.5;
}
.line {
fill: none;
stroke-width: 1.5px;
}
.bar {
opacity: 0.7;
}
path.percent {
stroke: orange;
fill: none;
}
.sales {
fill: steelblue;
}
.forecast {
fill: tomato;
}
JavaScript
var data = [
{ name:'Branch A', sales: 5400, forecast: 7000 },
{ name:'Branch B', sales: 2800, forecast: 4500 },
{ name:'Branch C', sales: 3600, forecast: 3300 },
{ name:'Branch D', sales: 1700, forecast: 4700 },
{ name:'Branch E', sales: 2200, forecast: 5500 }
];
var margin = { top: 50, right: 100, bottom: 40, left: 40 }
, width = 800 - margin.left - margin.right
, height = 300 - margin.top - margin.bottom
, svg = d3.select('#item-container').append('div').append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// caption
svg.append('text')
.attr('class', 'component-caption')
.attr('text-anchor', 'middle')
.attr('transform', 'translate(10, -' + (margin.top - 20) + ')')
.text('売り上げと予想');
// axis
// x
// x軸は文字列なのでordinalにするのとrangeRoundPointsでちょうど良い場所を取得する
var x = d3.scale.ordinal()
.domain(data.map(function (d) { return d.name; }))
.rangeRoundPoints([0, width], 0.5);
var xAxis = d3.svg.axis().scale(x)
.orient('bottom');
svg.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0, ' + height + ')')
.call(xAxis);
// x軸のTickの文字を斜めにする
svg.selectAll(".x text")
.attr("transform", function (d) {
return "translate(" + this.getBBox().height * -2 + "," + this.getBBox().height + ")rotate(-45)";
});
// y
// 一つ目のy軸。売り上げと予想から最大値を取得する
var y = d3.scale.linear().nice()
.domain([0, d3.max(data, function (d) { return Math.max(d.sales, d.forecast); })])
.range([height, 0]);
var yAxis = d3.svg.axis().scale(y)
.orient('left')
.ticks(5);
var gy = svg.append('g')
.attr('class', 'y axis')
...