I'm trying to use ChartJS in a Lightning Component on a private customer community (SP17 org). When I try to generate the chart, I get the following error logged in the browser console:
"Failed to create chart: can't acquire context from the given item"
I have the following code:
// Any of the following formats may be used
var ctx = document.getElementById("myChart");
//var ctx = document.getElementById("myChart").getContext("2d");
//var ctx = $("#myChart");
//var ctx = "myChart";
var myPieChart = new Chart(ctx1,{
type: 'pie',
data: pieData
});
According to ChartJS docs, any of the 'ctx' assignments should work. However I consistently get the "Failed to create chart" error. My guess is that LockerService is blocking ChartJS from finding the correct DOM Canvas element in which to draw the chart.
I've also tried
var chart1 = component.find(elementId).getElement();
var ctx1 = chart1.getContext("2d");
which also does not work. Can anyone tell me how to let ChartJS find the right DOM element?
afterScriptsLoaded
callback of<ltng:require>
? I have a working example with Chart v2.1.4 where the markup is<canvas aura:id="chart" id="chart" width="100" height="100">
and in theafterScriptsLoaded
callback the context is retrieved viacomponent.find("chart").getElement().getContext("2d")
. It's also a line chart instead of pie.