score:12
this.renderer.createElement('svg')
creates HTML element instead of SVGElement because Angular doesn't know you wanted svg:svg.
Try using this.renderer.createElement('svg', 'svg')
, where second parameter is the Namespace. Every svg element must be created in the same way, e.g:
let rect = this.renderer.createElement('rect', 'svg')
this.renderer.appendChild(svg, rect)
score:0
You have correctly added an svg element with a height and width to the view. But an svg element does not have a background-color
attribute. You can think of it as the canvas on top of which you layer other elements that have shape and colour.
You need to add another element to this view to see anything. Remove the background-color
attribute from the svg, then, as you have added the svg element to the view, you can use d3 to add, say, a rect to the view:
d3.select(this.svg)
.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', 600)
.attr('height', 400)
.attr('fill', 'blue');
alternatively, you could just do the following:
@Component({
selector: 'bar-chart',
template: `
<div class='bar-chart' #barChart></div>
`
})
export class BarChartComponent implements OnInit {
@ViewChild('barChart')
public chart: ElementRef;
public svg: any;
private width: number = 600;
private height: number = 400;
ngOnInit() {
this.svg = d3.select(this.chart.nativeElement)
.append('svg')
.attr('width', this.width)
.attr('height', this.height);
this.svg
.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', this.width)
.attr('height', this.height)
.attr('fill', 'blue');
}
}
Source: stackoverflow.com
Related Query
- Renderer2 does not render SVG element while using d3 js - Angular 4
- IE11 does not accept SVG height and width when using D3
- Using d3.js svg clipPath not clipping in Angular
- Using Angular ng-class with d3.js on svg element
- D3 Does not render or paint DOM element
- "Property does not exist on type {}" error when using anonymous function with D3 SVG Symbol
- d3 svg zoom does not work when neighbouring element exists
- d3 svg rect does not render in chrome
- Microsoft Edge does not render SVG style elements on ajax call to a gzipped SVG file
- Why is my D3 SVG not getting the correct width/height from Angular element
- Text not wrapping in d3 SVG element even when using Bostock's wrap function
- Pattern not rendering when using group element (g) in d3, but rendering when made as an individual SVG
- svg element does not show up
- D3.js does not render SVG in ASP MVC app
- svg element not appearing in the body using d3.js
- document.getElementById() does not work with puppeteer finding and id on a <path> element on SVG
- Importing local json file using d3.json does not work
- D3.js: Position tooltips using element position, not mouse position?
- How do I get the width of an svg element using d3js?
- Append SVG canvas to element other than body using D3
- D3 - Positioning tooltip on SVG element not working
- Angular 2 typescript d3 type issue: Property 'x' does not exist on type '[number, number]'
- Svg clip-path within rectangle does not work
- d3.on("mouseover") event does not work with nested SVG elements
- HTML element inside SVG not displayed
- C3.js SVG visualization to Canvas with canvg - Line charts filled with black rectangles , "ERROR: Element 'parsererror' not yet implemented"
- Edge does not handle scaling and text-anchor:middle correctly in svg
- How to get reference to SVG (child) elements nested in another SVG element (parent) using D3?
- D3 - Large GeoJSON File does not show draw map properly using projections
- SVG added to DOM using javascript is not visible.
More Query from same tag
- Get ID from dataset when clicking on a series using Dimple JS
- D3 - attribute d: Expected number Error in creating curved chart
- Random fill colors in Chart.js
- Js for loop to store values
- d3: why is variable set inside d3.json not accesible outside?
- In a D3 tree, how to close all nodes at a selected depth (ex. only grandchildren) programmatically (without clicking on a node)
- Reset or Clear Cubism Graph
- How do I properly sort the data for my d3 bubble map so that smaller bubbles show up on top of larger bubbles?
- Enable scroll on the axis of D3 chart
- Why is the first Link item being skipped?
- Collapse/expand child nodes of tree in d3.js?
- D3 - Finding an SVG Element with the same data
- Comparing different size arrays - D3 JS
- How to use call function?
- d3 Force: Making sense of data binding
- Grouped scatter plot with multiple datasets D3
- d3.js projection (albersUsa) mapping incorrectly
- storing dc.js filters in URI and restoring them
- Pixi.js vs Konva.js vs D3.js
- D3 dots do not match curved area chart
- get all nodes that is connected through their links
- d3 bar chart grow from height 0 to bars at loading
- d3 line chart not showing data - based of tutorial
- d3-timeline Error: <rect> attribute x: Expected length, "NaN"
- Check if URL exists within a function in D3.js
- how to show only primary connection in d3plus rings
- Using Handlebar with D3.js
- NVD3 - Adding extra functionality to legend click without overriding existing
- Converting object array to hierarchical data structure
- D3.JS how to load 3 csv files and change data on line chart with button click event?