admin管理员组文章数量:1392105
I need to add borders to the bars of the graph, I know that there is a plugin to add it and I already saw the other questions that are raised in this same platform, but I cannot add them to my bars, I need help
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);
});
});
for the moment I have it like this:
but what I need is with border-radius
I know that on this platform there are many questions regarding this topic, but I cannot apply it in my graph and I need to create those contours in my graph
I need to add borders to the bars of the graph, I know that there is a plugin to add it and I already saw the other questions that are raised in this same platform, but I cannot add them to my bars, I need help
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);
});
});
for the moment I have it like this:
but what I need is with border-radius
I know that on this platform there are many questions regarding this topic, but I cannot apply it in my graph and I need to create those contours in my graph
Share Improve this question edited Oct 19, 2020 at 20:38 Ruben Helsloot 13.2k6 gold badges32 silver badges55 bronze badges asked Oct 19, 2020 at 20:18 DGomezDGomez 3996 silver badges16 bronze badges1 Answer
Reset to default 6<rect>
elements have two attributes rx
and ry
that can be used to give them a border radius. See also the docs. You don't need to set both, if you set one, the other will assume the same value.
var margin = {
top: 20,
right: 20,
bottom: 70,
left: 40
},
width = 600 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.time.format("%Y-%m").parse;
var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3.time.format("%Y-%m"));
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10);
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 + ")");
const data = d3.csvParseRows(`2013-01,53
2013-02,165
2013-03,269
2013-04,344
2013-05,376
2013-06,410
2013-07,421
2013-08,405
2013-09,376
2013-10,359
2013-11,392
2013-12,433
2014-01,455
2014-02,478`, function(row) {
return {
date: parseDate(row[0]),
value: +row[1],
};
});
x.domain(data.map(function(d) {
return d.date;
}));
y.domain([0, d3.max(data, function(d) {
return d.value;
})]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Value ($)");
svg.selectAll("bar")
.data(data)
.enter().append("rect")
.style("fill", "steelblue")
.attr('rx', 5)
.attr("x", function(d) {
return x(d.date);
})
.attr("width", x.rangeBand())
.attr("y", function(d) {
return y(d.value);
})
.attr("height", function(d) {
return height - y(d.value);
});
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare./ajax/libs/d3/3.5.17/d3.min.js"></script>
<script src="https://d3js/d3-dsv.v1.min.js"></script>
本文标签: javascriptAdd borderradius bar chart d3Stack Overflow
版权声明:本文标题:javascript - Add border-radius bar chart d3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744764844a2623975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论