I’ve been using D3js recently to do some charting in my applications but I am really struggling to take something that works and renders in a standard ‘HTML with tags’ way and turn that into an Ember component.
Taking a simple example from blocks.org:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Testing Pie Chart</title>
var w = 300, //width
h = 300, //height
r = 100, //radius
color = d3.scale.category20c();
data = [{"label":"one", "value":20},
{"label":"two", "value":50},
{"label":"three", "value":30}];
var vis = d3.select("#chart")
.append("svg:svg")
.data([data])
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + r + "," + r + ")")
var arc = d3.svg.arc()
.outerRadius(r);
var pie = d3.layout.pie()
.value(function(d) { return d.value; });
var arcs = vis.selectAll("g.slice")
.data(pie)
.enter()
.append("svg:g")
.attr("class", "slice");
arcs.append("svg:path")
.attr("fill", function(d, i) { return color(i); } )
.attr("d", arc);
arcs.append("svg:text")
.attr("transform", function(d) {
d.innerRadius = 0;
d.outerRadius = r;
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function(d, i) { return data[i].label; });
</script>
</body>
</html>
Assume that D3 is imported correctly into the Ember component, I find it very challenging to refactor into something that Ember will accept. I end up in a mess of errors ranging from ‘not a function’ to ‘undefined’ or general unexpected token syntax problems. I suspect my best efforts also have things being triggered in incorrect load orders.
I’ve been surprised at the lack of resources that talk about using D3 in an ember context and wondered if anyone could provide some tips or guidance on what to look out for.