Rectangle with a bottom arc cut in d3 js

烂漫一生 提交于 2019-12-13 14:44:25

问题


I was able to get only the rectangle. Not able to figure out how to produce the cut in the bottom?

define(['jquery', 'knockout', 'd3', 'data/server', 'css!app/css/vista'],function($, ko, d3, server){
	return {		
		activate: function(){
			
		},
		compositionComplete: function(){
			var self = this;						
			self.loadyourRank();
		},
		loadyourRank: function(){
			var data = [3];
			var width = 325, height = 430;
							
			var svgContainer = d3.select("#yourrank")
				.append("svg")
				.attr("width", width)
				.attr("height", height);
			
			svgContainer.selectAll("rect")
				.data(data)
				.enter()
				.append("rect")
				.attr("x", 30)
				.attr("y", 50)
				.attr("width", 255)
				.attr("height", 340)
				.attr("fill", "#F2135D")
				.attr("stroke", "gray");	
			
		}
    };
});
<div class="card">
	<div class="row">
		<div id="yourrank" class="col-xs-4">
			<h4>Your Rank</h4>
		</div>
		<div id="bestrank" class="col-xs-8">
			<h4>Your Best Ranked Specialities</h4>
		</div>		
	</div>		
</div>

How to get the above shape or element in svg using d3.js ?? somebody help


回答1:


Use SVG Path.

d attribute calculation

  • Move to x,y
  • Line to (x+width),y
  • Line to (x+width),(y+height)
  • Quadratic Bezier curve from current point to (x+width)/2, required_curve_height to x,(y+height)
  • Close path (End point to Start point)

Refer here for more details.

The SVG <path> element is used to draw advanced shapes combined from lines, arcs, curves etc. with or without fill. The <path> element is probably the most advanced and versatile SVG shape of them all.

var data = [3];
var width = 325,
  height = 430;

var svgContainer = d3.select("#yourrank")
  .append("svg")
  .attr("width", width)
  .attr("height", height);

svgContainer.selectAll("path")
  .data(data)
  .enter()
  .append("path") 
  .attr("d", "M 30,50 L 285,50 L 285,390 Q 157.5,200  30,390 Z")
  .attr("fill", "#F2135D")
  .attr("stroke", "gray");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="yourrank"></div>


来源:https://stackoverflow.com/questions/33909931/rectangle-with-a-bottom-arc-cut-in-d3-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!