【发布时间】:2021-02-02 15:46:42
【问题描述】:
我需要为图表的条形添加边框,我知道有一个插件可以添加它,并且我已经看到了在同一平台上提出的其他问题,但我无法将它们添加到我的条形中,我需要帮助
window.addEventListener("load", (event) => {
// set the dimensions and margins of the graph
var margin = { top: 10, right: 30, bottom: 20, left: 50 },
width = 1500 - margin.left - margin.right,
height = 350 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3
.select("#my_chart")
.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 + ")");
// Parse the Data
// List of subgroups = header of the csv files = soil condition here
var subgroups = data.columns.slice(1);
console.log(data);
// List of groups = species here = value of the first column called group -> I show them on the X axis
var groups = d3
.map(data, function(d) {
return d.group;
})
.keys();
// Add X axis
var x = d3.scaleBand()
.domain(groups)
.range([0, width])
.padding([0.2]);
svg
.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x)
.tickSize(0))
.attr("font-size", "0.9rem")
.attr("font-weight", "700");
// Add Y axis
var y = d3.scaleLinear()
.domain([0, 800])
.range([height, 0]);
svg
.append("g")
.call(d3.axisLeft(y))
.attr("font-size", "0.9rem")
.attr("font-weight", "700");
// Another scale for subgroup position?
var xSubgroup = d3
.scaleBand()
.domain(subgroups)
.range([0, x.bandwidth()])
.padding([0.05])
;
// color palette = one color per subgroup
var color = d3
.scaleOrdinal()
.domain(subgroups)
.range(["#ffb741", "#e9e9e9", "#377eb8"]);
// gridlines in y axis function
function make_y_gridlines() {
return d3.axisLeft(y)
.ticks(10);
}
// add the Y gridlines
svg
.append("g")
.attr("class", "grid")
.call(make_y_gridlines()
.tickSize(-width)
.tickFormat(""));
// Show the bars
svg
.append("g")
.selectAll("g")
// Enter in data = loop group per group
.data(data)
.enter()
.append("g")
.attr("transform", function(d) {
return "translate(" + x(d.group) + ",0)";
})
.selectAll("rect")
.data(function(d) {
return subgroups.map(function(key) {
return { key: key, value: d[key] };
});
})
.enter()
.append("rect")
.attr("x", function(d) {
return xSubgroup(d.key);
})
.attr("y", function(d) {
return y(d.value);
})
.attr("width", xSubgroup.bandwidth())
.attr("height", function(d) {
return height - y(d.value);
})
.attr("fill", function(d) {
return color(d.key);
});
});
暂时我是这样的:
但我需要的是边框半径
我知道在这个平台上有很多关于这个主题的问题,但我不能在我的图表中应用它,我需要在我的图表中创建这些轮廓
【问题讨论】:
标签: javascript html d3.js bar-chart