Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jkutianski
Last active December 28, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkutianski/7475950 to your computer and use it in GitHub Desktop.
Save jkutianski/7475950 to your computer and use it in GitHub Desktop.
A new attempt to add templating to d3.js

D3.js templating test based on my previous solution of John Berryman's attempt.

ICahnHaz.js uses <script></script> and I preffer the idea of Eric Bidelman. The <template> element is currently supported in Chrome, Firefox and Opera (15+)for now.

Because the use of innerHTML run-time string parsing of user-supplied data can easily lead to XSS vulnerabilities and isn't supported by SVGElement, I use now XMLSerializer() and DOMParser() to inject the templates into the DOM. The callTemplate function uses querySelector and isn't limitted to Mustache.

Fork me @ GITHUB

TODOs:

(function (d3Pointers) {
var callTemplate = function(callback,sel,d) {
sel = (sel) ?
'template' +
(
(sel.match(/^[\#|\.|\:|\[]/g)) ?
sel :
' ' + sel
) :
'';
var serializer = new XMLSerializer(),
parser = new DOMParser(),
template = d3.select(sel)[0][0],
thisSelection = (d) ? this.datum(d) : this,
preTemplate = postTemplate = '',
templateContent = (
(serializer && template.toString() == '[object HTMLTemplateElement]') ?
serializer.serializeToString(template.content) :
template.innerHTML
).replace(/\<script.*?\>.*?\<\/script\>/gi,'');
switch(template.getAttribute('type')) {
case 'image/svg+xml':
preTemplate = '<svg xmlns=\'http://www.w3.org/2000/svg\'>';
postTemplate = '</svg>';
break;
case 'text/html':
preTemplate = '<html xmlns=\'http://www.w3.org/1999/xhtml\'>';
postTemplate = '</html>';
break;
case 'text/plain':
preTemplate = '<text>';
postTemplate = '</text>';
break;
case 'text/javascript':
templateContent = '';
break;
default:
break;
}
parser.async = false;
thisSelection.each(function (d) {
var renderedContent = callback(templateContent, d),
parsedContent = parser
.parseFromString(preTemplate + renderedContent + postTemplate,'text/xml')
.documentElement
.firstChild;
while (parsedContent) {
this.appendChild(
this.ownerDocument.importNode(parsedContent, true)
);
parsedContent = parsedContent.nextSibling;
}
});
};
while((pointer = d3Pointers.pop())) {
pointer.prototype.callTemplate = callTemplate;
}
})(
[d3.selection, d3.selection.enter]
);
<!DOCTYPE html>
<html>
<head>
<title>D3js Templating Test</title>
<script src="http://d3js.org/d3.v2.min.js" type="text/javascript"></script>
<script src="d3.selection.template.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.min.js" type="text/javascript"></script>
</head>
<body>
<template id="head" type="image/svg+xml">
<path
d="M 28.471261,-5.0558794 C 32.671841,22.243131 19.834631,49.162831 0.60952108,49.162831 -18.615599,49.162831 -27.684919,24.667631 -27.053699,-3.0332694 -26.458119,-29.170189 -25.961069,-47.588419 0.21246108,-48.936829 19.417701,-49.926249 24.103691,-33.440159 28.471261,-5.0558794 z"
id="face"
transform="scale({{head.width}} 1)"
style="color:#000000;fill:#ffccaa;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.5;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate">
</path>
<path
d="m 15,0 c 0,3.7959 -4.91374,13.7461 -12.8383199,13.7461 -7.92458,0 -17.6233801,-10.3065 -15.8591001,-13.7461 1.0069201,-1.9631 7.9345201,0 15.8591001,0 7.9245799,0 12.8383199,-3.7959 12.8383199,0 z"
id="mouth"
transform="translate(0, {{mouth.drop}}) translate(0,17) scale({{mouth.width}} {{mouth.height}})"
style="color:#000000;fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate">
</path>
<path
d="m -18.009869,-17.276949 c 3.23815,-9.62232 14.5716901,-8.70591 14.5716901,-8.70591"
id="rightbrow"
transform="translate(0, -{{brow.lift}})"
style="color:#000000;fill:none;stroke:#000000;stroke-width:1.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate">
</path>
<path
d="m 20.443211,-18.868939 c -3.23815,-9.62232 -14.5716899,-8.70591 -14.5716899,-8.70591"
id="leftbrow"
transform="translate(0, -{{brow.lift}})"
style="color:#000000;fill:none;stroke:#000000;stroke-width:1.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate">
</path>
<path
d="m 2.3,0 c 0.90841,1.59522 -1.53169,3.13263 -4.07974,3.51899 -2.5480401,0.38635 -5.4168801,-0.16412 -5.1475501,-2.11987 0.25826,-1.87529 1.62394,-2.35301 4.1719801,-2.73937 2.54805,-0.38636 4.17038,-0.21373 5.05531,1.34025 z"
id="righteye"
transform="translate(-5,-18) scale({{eye.width}} {{eye.height}})"
style="color:#000000;fill:#000000;stroke:#000000;stroke-width:1.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate">
</path>
<path
d="m 5.5,0 c 0.90841,1.59522 -1.53169,3.13263 -4.07974,3.51899 -2.5480399,0.38635 -5.4168799,-0.16412 -5.1475499,-2.11987 0.25826,-1.87529 1.62394,-2.35301 4.17198,-2.73937 2.5480499,-0.38636 4.1703899,-0.21373 5.0553099,1.34025 z"
id="lefteye"
transform="translate(10,-18) scale({{eye.width}} {{eye.height}})"
style="color:#000000;fill:#000000;stroke:#000000;stroke-width:1.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate">
</path>
<path
d="M -8.1253309,-1.8487371 C -3.740886,13.80696 10.948853,19.556588 10.177223,-2.5578614"
id="nose"
transform="translate(0, {{nose.drop}}) scale({{nose.width}} {{nose.height}})"
style="color:#000000;fill:#ffccaa;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate">
</path>
</template>
</body>
<script type="text/javascript">
window.onload = function () {
var count = 0,
w = 960,
h = 520,
body = d3.select('body'),
vis = body.append('svg')
.attr('width', w)
.attr('height', h),
nextData = function() {
count += 1;
return {
head: {
width: .7*Math.random()+.8 //.8 - 1.5
},
mouth: {
width: .8*Math.random()+.5, //.5 - 1.3
height: Math.random()+.5, //.5 - 1.5
drop: 15*Math.random()-5 //-5 - 10
},
nose: {
width: Math.random()+.5, //.5 - 1.5
height: Math.random()+.5, // .5 - 1.5
drop: 15*Math.random()-5 // -5 - 10
},
eye: {
width: .8*Math.random()+.5, //.5 - 1.3
height: 1.1*Math.random()+.4//.4 - 1.5
},
brow: {
lift: 10*Math.random() //0 - 10
},
x: w*(count%5+1)/5 - 85 ,
y: h*.95*Math.ceil(count/5)/4.9 - 51
};
},
data = d3.range(25).map(function() {
return nextData();
});
vis.selectAll('.head')
.data(data)
.enter()
.append('g')
.attr('class', 'head')
.attr('transform',function(d) {
return 'translate('+ d.x +' '+ d.y +')'
})
.callTemplate(Mustache.render, "#head");
};
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment