D3.js组合条形图和折线图
D3.js combining bar and line chart
我想通过组合条形图和折线图来创建一个多系列图表。当我使用rangeBands()
设置输出范围时,该线从图表的第一个条形图的开始处开始绘制,并在最后一个条形图开始处结束。我应该更改什么,使线条从第一个刻度开始,到最后一个刻度结束?
var data = [{ date: '1-May-12', close: 58.13, open: 7.41 }, { date: '2-May-12', close: 53.98, open: 45.55 }, { date: '3-May-12', close: 67.00, open: 11.78}];
var margin = { top: 30, right: 40, bottom: 30, left: 50 },
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var x = d3.scale.ordinal().rangeBands([0, width], .09); // <-- to change the width of the columns, change the .09 at the end to whatever
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom")
.tickFormat(d3.time.format("%d-%b-%y"));
var yAxisLeft = d3.svg.axis().scale(y)
.orient("left");
var valueline = d3.svg.line()
.x(function (d) { return x(d.date); })
.y(function (d) { return y(d.open); });
var svg = d3.select("body")
.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 + ")");
// Get the data
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
});
// Scale the range of the data
x.domain(data.map(function (d) { return d.date; }));
y.domain([0, d3.max(data, function (d) { return d.close; })]);
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.style("fill", "steelblue")
.call(yAxisLeft);
// Draw the bars
svg.selectAll("bar")
.data(data)
.enter()
.append("rect")
.style("fill", "#99ffcc")
.attr("x", function (d) { return x(d.date); })
.attr("width", x.rangeBand())
.attr("y", function (d) { return y(d.close); })
.attr("height", function (d) { return height - y(d.close); });
// Add the valueline path
svg.append("path")
.attr("d", valueline(data));
body {
font: 12px Arial;
}
path {
stroke: red;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
将行函数x访问器调整一半量程:
var valueline = d3.svg.line()
.x(function (d) { return x(d.date) + x.rangeBand()/2; })
.y(function (d) { return y(d.open); });
完整代码:
var data = [{ date: '1-May-12', close: 58.13, open: 7.41 }, { date: '2-May-12', close: 53.98, open: 45.55 }, { date: '3-May-12', close: 67.00, open: 11.78}];
var margin = { top: 30, right: 40, bottom: 30, left: 50 },
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var x = d3.scale.ordinal().rangeBands([0, width], .09); // <-- to change the width of the columns, change the .09 at the end to whatever
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom")
.tickFormat(d3.time.format("%d-%b-%y"));
var yAxisLeft = d3.svg.axis().scale(y)
.orient("left");
var valueline = d3.svg.line()
.x(function (d) { return x(d.date) + x.rangeBand()/2; })
.y(function (d) { return y(d.open); });
var svg = d3.select("body")
.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 + ")");
// Get the data
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
});
// Scale the range of the data
x.domain(data.map(function (d) { return d.date; }));
y.domain([0, d3.max(data, function (d) { return d.close; })]);
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.style("fill", "steelblue")
.call(yAxisLeft);
// Draw the bars
svg.selectAll("bar")
.data(data)
.enter()
.append("rect")
.style("fill", "#99ffcc")
.attr("x", function (d) { return x(d.date); })
.attr("width", x.rangeBand())
.attr("y", function (d) { return y(d.close); })
.attr("height", function (d) { return height - y(d.close); });
// Add the valueline path
svg.append("path")
.attr("d", valueline(data));
body {
font: 12px Arial;
}
path {
stroke: red;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
相关文章:
- 用chart.js绘制条形图
- d3.js用按钮更新条形图工具提示
- 添加新数据时D3.JS条形图列偏移量
- Chart.js条形图:网格颜色和隐藏标签
- 在条形图中呈现数据AngularJS,如何制作一个好的多条形图
- Chart.js 2.1.2条形图动画问题
- 如何在Extjs5中更改条形图的标签
- Javascript FLOT组合条形图;以x轴为字符串的折线图
- 如何隐藏由Chart.js V2.0绘制的组合折线图和条形图中的点
- D3.js组合条形图和折线图
- 条形图和折线图在 DC.js 中不交互
- 为什么 D3 折线图和条形图显示在同一时间
- 在 flot.js 中绘制折线图和条形图以获取 JSON 数据
- 单击折线图中的单个点但不单击条形图中单个条形时显示的引导模式
- DC.js条形图条形图大小
- 使用nvd3.js绘制具有堆叠条形图和折线图的图表
- 在高图条形图上打印一个值
- 在Javascript中组合条形图和折线图
- 组合具有不同粒度的chart.js条形图和折线图
- 百度条形图和折线图问题