繁体   English   中英

条形图 d3 条形下方的圆圈

[英]bar chart d3 circles under the bars

我需要在条形图的每一列下放置圆圈,如下所示:

在此处输入图片说明

我已经设法使每一轮的条形结束,但我不知道如何将每个圆圈放在每个条形下

这将是我的代码

 // set the dimensions and margins of the graph var margin = { top: 10, right: 30, bottom: 20, left: 50 }, width = 500 - margin.left - margin.right, height = 250 - 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 var data = [{ group: "0", Nitrogen: "10" }, { group: "1", Nitrogen: "30" }, { group: "2", Nitrogen: "50" }, ]; data.columns = ["group", "Nitrogen"]; // 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", "400") .attr("color", "lightgray"); // Add Y axis var y = d3 .scaleLinear(25) .domain([0, 75]) .range([height, 0]) ; svg .append("g") .call(d3.axisLeft(y)) .attr("font-size", "0.9rem") .attr("font-weight", "400") .attr("color", "lightgray") .attr("style", "transform: translate(0px, -12px);"); // 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(["#93B6F8"]); // 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("class", "bar") .attr( "style", "padding-bottom: 8px; transform: translate(0px, -6px);" ) .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); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script> <div id="my_chart"></div>

我是这样展示的:

在此处输入图片说明

我也想如果不是太麻烦,有人向我解释如何使条具有最大宽度

考虑以下。 我从这个例子中获取了代码,将它保留在相同的结构中,但稍微降低了条形高度并在底部添加了圆圈:

 var dataString = `salesperson,sales Bob,33 Robin,12 Anne,41 Mark,16 Joe,59 Eve,38 Karen,21 Kirsty,25 Chris,30 Lisa,47 Tom,5 Stacy,20 Charles,13 Mary,29`; // set the dimensions and margins of the graph var margin = { top: 20, right: 20, bottom: 30, left: 40 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; // set the ranges var x = d3.scaleBand() .range([0, width]) .padding(0.5); var y = d3.scaleLinear() .range([height, 0]); // append the svg object to the body of the page // append a 'group' element to 'svg' // moves the 'group' element to the top left margin 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 + ")"); var data = d3.csvParse(dataString); // format the data data.forEach(function(d) { d.sales = +d.sales; }); // Scale the range of the data in the domains x.domain(data.map(function(d) { return d.salesperson; })); y.domain([0, d3.max(data, function(d) { return d.sales; })]); // append the rectangles for the bar chart var barGroup = svg.selectAll(".bar") .data(data) .enter() // Not rect, but g, we create one group per rect+circle combination .append("g") .attr("class", "bar"); barGroup .append("rect") .attr("x", function(d) { return x(d.salesperson); }) .attr("width", x.bandwidth()) .attr("y", function(d) { return y(d.sales); }) .attr("rx", 5) // Some rounded corners .attr("height", function(d) { // Subtract a little bit of height, // because we need to draw the circles underneath // return height - y(d.sales); return height - x.bandwidth() - y(d.sales); }); var circlePadding = 4; //pixels barGroup .append("circle") .attr("cx", function(d) { return x(d.salesperson) + x.bandwidth() / 2; }) .attr("cy", function(d) { return height - x.bandwidth() / 2; }) .attr("r", x.bandwidth() / 2 - circlePadding); // add the x Axis svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // add the y Axis svg.append("g") .call(d3.axisLeft(y));
 .bar { fill: steelblue; }
 <script src="https://d3js.org/d3.v4.min.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM