Trying to display some simple bar charts using D3 but nothing is being displayed.
<style>
.chart div {
font: 10px sans-serif;
background-color: steelblue;
text-align: right;
padding: 3px;
margin: 1px;
color: white;
}
</style>
<script src="d3.v3.js"></script>
<script>
var chartData = [4, 8, 15, 16, 23, 42];
var body = d3.select('body');
var svg = body.append('svg')
.attr("width",800)
.attr("height",500);
var div = svg.append('div').attr('class', '.chart');
for (var i = 0; i < chartData.length; i++) {
div.append('div')
.attr("height", 20)
.attr("width", chartData[i]*10)
.html(chartData[i]);
}
</script>
When using chrome's inspect I see the newly created elements and the svg is highlighted but not the inner div's Thank you.
In a nutshell: you cannot append a HTML element to a SVG. The reason is simple:
<div>
,<p>
,<h1>
,<h2>
,<td>
,<li>
and so on are not valid SVG elements.This has been asked and answered many, many times. However, I'd like to answer this question because of a specific part of it (which is normally not asked):
Yes, the elements are there. But this doesn't mean it's going to work.
Let's show this in the following snippet, in which I'm appending
<div>
s to the SVG. look at theconsole.log
, it shows the SVG structure:You can see that the divs are in the DOM. However, there is nothing on the screen...
The fact that you see the element in the DOM means nothing, because you can append anything!
For instance, let's append an element named
CharlesDarwin
:You can see
<charlesDarwin>
in the DOM. However, it's obvious that nothing will show up in the SVG.PS: Regarding your chart:
This is the working code: