Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.4+ fails to acquire context in Salesforce Lightning #3887

Closed
kadut opened this issue Feb 9, 2017 · 3 comments
Closed

2.4+ fails to acquire context in Salesforce Lightning #3887

kadut opened this issue Feb 9, 2017 · 3 comments

Comments

@kadut
Copy link

kadut commented Feb 9, 2017

Inclusion of Chart.js as a static resource for Salesforce Lightning has been broken since 2.4. See for example http://salesforce.stackexchange.com/questions/155591/chartjs-v2-4-0-failed-to-create-chart-cant-acquire-context-from-the-given-item.

Fails with
"Can't acquire context from the given item" - actual line number depends on version

Chrome 56, all extensions disabled to avoid fingerprinting issues.

With a simple component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes">
<ltng:require scripts="/resource/Chart" afterScriptsLoaded="{!c.getChart}"/>

</aura:component>
[note that adding an html id to the canvas does not help]
[
note that there is line a few above here that seems to show up as blank when i look at what i wrote, but should be showing the canvas tag. reproduced below with opening and closing <> left out to make it show up:
canvas aura:id="chart" height="301" width="301"></canvas
]

And controller doing:
var ctx = component.find("chart").getElement().getContext("2d");
var barChart = new Chart(ctx, {type: 'bar', data: data, options: options});

Issue seems to have appeared with #3474 as for some reason the checks fail with Salesforce.

There are 2 lines at issue in function acquireContext(item, config), with reference to 2.4:

if (item instanceof HTMLCanvasElement)
debugger:
false
fix:
if (item instanceof HTMLCanvasElement).nodeName || item.nodeName === "CANVAS")

if (context instanceof CanvasRenderingContext2D)
debugger:
false
fix:
remove this check.

With both these fixes, 2.4 renders my graph above perfectly.
I would have made a PR except that I wasn't sure how you wanted to handle (2) since you have tests using a mock that would fail. I'd use a ducktype check for some method of CanvasRenderingContext2D, but wasn't sure if that was copacetic with your overall scheme.

@etimberg
Copy link
Member

@kadut what is Salesforce Lightning actually rendering into? It looks like it replaces all of the DOM with it's own elements that don't actually meet the spec

@kadut
Copy link
Author

kadut commented Feb 18, 2017

I am by no means a Salesforce expert, but I believe the wrapping assumption is correct due to their security model, though they claim that the result should be indistinguishable from raw DOM. Not meeting the spec seems a Salesforce deficiency, so arguably you might not want to support it and rather have me but in a request to fix to them, but this package seems pretty popular in the Salesforce world.

@etimberg
Copy link
Member

etimberg commented Apr 1, 2017

Closing since I don't think this is really a Chart.js error as we're only using standard JS. IMO Salesforce doesn't meet the spec and I think it's up to them to fix.

The message is thrown at https://github.com/chartjs/Chart.js/blob/master/src/core/core.controller.js#L116 and I'm guessing the context is not defined due likely due to https://github.com/chartjs/Chart.js/blob/master/src/platforms/platform.dom.js#L199

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants