Why can not I draw a circle with d3.js in a lightning component?
appl.app
:
<aura:application >
<c:libComp/>
</aura:application>
comp.cmp
:
<aura:component>
<ltng:require scripts="{!$Resource.d3Lib}"
afterScriptsLoaded="{!c.drawACircle}" />
</aura:component>
compController.js
:
({
drawACircle: function () {
//Make an SVG Container
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);
//Draw the Circle
var circle = svgContainer.append("circle")
.attr("cx", 30)
.attr("cy", 30)
.attr("r", 20)
}
});
According to this I am doing everything correctly.
But here is the error message:
Uncaught TypeError: Cannot read property 'document' of undefined
throws at https://oobarbazanoo-dev-ed.lightning.force.com/resource/1527188162000/d3Lib:15:4187
In the d3Lib
I put all that is written over here.
Best Answer
You can use lightning: container to accomplish this. Lightning:container was designed to use 3rd party libraries which are not locker service friendly in lightning components.
HTML file:
use.js
Manifest file for CSP:
and finnally the downloaded D3js file,refrenced in HTMl.
Create a zip of all those 4 files and upload as a static resouces named as "d3Demo".
And then in you lightning component/application refer the lightning:container as
Thasts it done. D3js in lightning components, no need to worry about locker services.
Source:https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/container_overview.htm