I'm trying to add a mask to AreaChart and have found out that created mask is applied (and displayed) only when it's defined within <defs>
section of the svg document generated by Google Charts API. I created an EventListener for the 'ready'-event, which appends <mask>
to <defs>
but it had no success.
The most strange thing is that I can see the appended <mask>
when I inspect <svg>
with Firefox Webmaster Tools and when I edit <defs>
in browser 'as HTML', change something (eg: add additional space between any tags or attributes) and save — the mask is applied to the path of my chart, but I don't understand why it is not applied when the API draws the chart.
Here is some code:
google.visualization.events.addListener(chart, 'ready', function () {
var mask = chart_placeholer.querySelector('defs > mask#mask-stripe');
if(!mask) {
mask = document.createElement('mask');
mask.setAttribute('id', 'mask-stripe');
// Note: there is no difference whether I create `<rect>` using createElement
// and append, or create it with innerHTML — result is always the same.
mask.innerHTML = '<rect x="0" y="0" width="100%" height="100%" fill="url(#pattern-stripe)"/>';
chart_placeholer.getElementsByTagName('defs')[0].appendChild(mask);
}
}
I also have code which changes the attributes fill
and mask
of the <path>
, and it works fine, so there is no need to put that code here. The problem is that that mask
is applied to path only after I find the definition of the mask with HTML Inspector, make some irrelevant changes and save it.