请根据上面的数据用d3.js画出堆叠柱状图并给出代码
时间: 2024-05-13 14:18:44 浏览: 15
以下是用d3.js画堆叠柱状图的代码示例:
```javascript
// 设置数据
var data = [
{category: 'A', value1: 10, value2: 20, value3: 30},
{category: 'B', value1: 15, value2: 25, value3: 35},
{category: 'C', value1: 20, value2: 30, value3: 40},
{category: 'D', value1: 25, value2: 35, value3: 45},
];
// 设置画布大小和边距
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// 创建 SVG 元素
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 + ")");
// 设置 x 和 y 轴的比例尺
var x = d3.scaleBand()
.range([0, width])
.padding(0.1)
.domain(data.map(function(d) { return d.category; }));
var y = d3.scaleLinear()
.range([height, 0])
.domain([0, d3.max(data, function(d) { return d.value1 + d.value2 + d.value3; })]);
// 定义颜色比例尺
var color = d3.scaleOrdinal()
.range(["#98abc5", "#8a89a6", "#7b6888"]);
// 定义堆叠函数
var stack = d3.stack()
.keys(["value1", "value2", "value3"])
.order(d3.stackOrderNone)
.offset(d3.stackOffsetNone);
// 堆叠数据
var series = stack(data);
// 添加 x 轴
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// 添加 y 轴
svg.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.append("text")
.attr("x", 2)
.attr("y", y(y.ticks().pop()) + 0.5)
.attr("dy", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text("Value");
// 添加堆叠柱状图
svg.selectAll("g.layer")
.data(series)
.enter().append("g")
.attr("class", "layer")
.style("fill", function(d, i) { return color(i); })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("x", function(d) { return x(d.data.category); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.attr("width", x.bandwidth());
// 添加图例
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
```
这段代码会生成一个堆叠柱状图,其中每个类别有三个值,分别用不同的颜色表示。你可以根据需要修改数据和样式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)