score:18
Accepted answer
the solution was to comment out/remove the width and height attribute values and instead add the following two lines:
//- .attr("width", width + margin.left + margin.right)
//- .attr("height", height + margin.top + margin.bottom)
.attr("preserveaspectratio", "xminymin meet")
.attr("viewbox", "0 0 960 500")
score:2
check the svg is drawn with parent height and width.
ref: https://bl.ocks.org/curran/3a68b0c81991e2e94b19
var parentdiv = document.getelementbyid("parentdiv");
var svg = d3.select(parentdiv).append("svg");
function redraw(show) {
// extract the width and height that was computed by css.
var width = parentdiv.clientwidth;
var height = parentdiv.clientheight;
// use the extracted size to set the size of an svg element.
svg
.attr("width", width)
.attr("height", height);
if (show === false) {
svg.style('visibility', 'hidden');
} else {
svg.style('visibility', 'visible');
}
svg.attr("style", "outline: thin solid red;")
.append("circle")
.attr("cx", 30)
.attr("cy", 30)
.attr("r", 20);
}
// draw for the first time to initialize.
redraw(false);
// redraw after sometime
settimeout(redraw, 1000);
#parentdiv {
height: calc(100vh - 100px); /** output container is small for display */
width: calc(100vw - 100px);
display: block;
border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.10/d3.min.js"></script>
<div id="parentdiv"></div>
Source: stackoverflow.com
Related Query
- Making SVG container 100% width and height of parent container in D3 v4 (instead of by pixels)
- d3.js brush() inherits incorrect height and width From svg parent using viewBox
- Auto width and height for SVG image
- IE11 does not accept SVG height and width when using D3
- How to maintain the SVG marker width and height ?
- How can I get the natural width and height of an svg image element?
- viewBox attribute with svg height and width attributes set
- How to get the width and height of an image? ( getBBox() size of SVG return 0 )
- Responsive graphs: Get width and height of axis, so graph can take up remaining size of a container
- Distribute nodes evenly around central node using a relative SVG width and height
- Dynamically set the width and height of SVG based on data
- D3 SVG with finite height and width
- Change height and width of the path tag in SVG Image
- how to use getElementById with getBBox to determine the svg width and height
- Why is this SVG Path showing incorrect width and height on Firefox?
- How to set the width and height of svg to be equal to its g?
- set width and height of an SVG element - d3.js
- Clojurescript integrating with datamaps sets svg width and height 0
- D3.js: How to get the computed width and height for an arbitrary element?
- d3.js Map (<svg>) Auto Fit into Parent Container and Resize with Window
- D3 how to change the width and length of SVG
- D3 Zoom with Scrollbars used as panning, Scrollbar width and height adjusts to zoom scale
- D3 treemap: reserve min width and height for the smallest node
- How to increase width and height of plotly area graph
- Making svg container appear one below another on array looping
- D3 Bar Chart with Variable X and Y(variable width and height for bars)
- Adding image to svg container using d3 not working on FF and Safari
- What is the default width and height of a group element in html (if it has any)?
- Make D3 Stacked Bar Chart fill parent SVG container
- how can I set a height and width for this map in d3.js
More Query from same tag
- Failed to remove labels from force directed graph
- Creating interactive floor plan using d3.js (or similar)
- D3.js svg does not draw Map
- How to change Y-Axis dc.js
- dc.js barChart bars overlapping
- Bootstrap and D3 problems
- D3.js animation
- Brush.extent is returning wrong yMax and yMin values
- Custom x-axis labels in d3 bar charts?
- Formatting Interpolated String or Number in d3
- Determining coordinates in lat/lng of a point in pixels on a map after zoom has been applied
- D3 lines and scope
- how i can put more lines in Line Plus Bar Chart using nvd3.js?
- In d3, how do I include text and a background box as I roll over my line chart?
- having problems selecting svg child elements using d3
- D3 How to refresh after image href of pattern is updated?
- d3 fitSize gives NaN values
- d3.js voronoi event. Mouseover seems to disappear when cursor is exactly above point
- add data and redraw D3 chord layout
- Hierarchy Level labels for d3.partition icicle chart
- d3.js bundle layout, moving nodes inklusiv line
- Network node highlighting with Crossfilter
- Get Clipped Line Visible Area Length d3
- Adding text to collapsible d3 tree links
- scaleBand() shifts the values left
- d3js v4 x-axis-label is there but not visible
- d3.js - Plotting points on zoomable, clickable map
- Can D3.js visualizations be served using Hugo?
- d3 map color to something other than value because colors are being duplicated when values are equal
- Overlay d3 paths onto Google Maps?