|
61 | 61 | return true;
|
62 | 62 | }
|
63 | 63 | }
|
64 |
| - |
65 |
| - var replaceRectsWithImages = function(chart, rects, dataTable) { |
66 |
| - const svgRoot = chart.container.querySelector('svg'); |
67 |
| - // remove any labels that have previously been drawn |
68 |
| - $('.__img_labels').each((idx, n) => $(n).remove()); |
69 |
| - |
70 |
| - // Render each rect above the old ones, using the image as a pattern |
71 |
| - dataTable.forEach(function (row, i) { |
72 |
| - const rect = rects[i]; |
73 |
| - // make an image element for the img, from the SVG namespace |
74 |
| - const imgDOM = row[2].val.toDomNode(); |
75 |
| - row[2].val.render(imgDOM.getContext('2d'), 0, 0); |
76 |
| - let imageElt = document.createElementNS("http://www.w3.org/2000/svg", 'image'); |
77 |
| - imageElt.classList.add('__img_labels'); // tag for later garbage collection |
78 |
| - imageElt.setAttributeNS(null, 'href', imgDOM.toDataURL()); |
79 |
| - // position it using the position of the corresponding rect |
80 |
| - imageElt.setAttribute('preserveAspectRatio', 'none'); |
81 |
| - imageElt.setAttribute('x', rects[i].getAttribute('x')); |
82 |
| - imageElt.setAttribute('y', rects[i].getAttribute('y')); |
83 |
| - imageElt.setAttribute('width', rects[i].getAttribute('width')); |
84 |
| - imageElt.setAttribute('height', rects[i].getAttribute('height')); |
85 |
| - Object.assign(imageElt, rects[i]); // we should probably not steal *everything*... |
86 |
| - svgRoot.appendChild(imageElt); |
87 |
| - }); |
88 |
| - |
89 |
| - } |
90 | 64 |
|
91 | 65 | google.charts.load('current', {'packages' : ['corechart']});
|
92 | 66 |
|
|
1123 | 1097 | // The only way to hijack rect events is to walk the DOM here
|
1124 | 1098 | // If Google changes the DOM, these lines will likely break
|
1125 | 1099 | const svgRoot = chart.container.querySelector('svg');
|
1126 |
| - const rects = svgRoot.children[1].children[1].children[1].children; |
1127 |
| - replaceRectsWithImages(chart, rects, table); |
| 1100 | + const rectRoot = svgRoot.children[1].children[1].children[1]; |
| 1101 | + const rects = rectRoot.children; |
| 1102 | + |
| 1103 | + // remove any labels that have previously been drawn |
| 1104 | + $('.__img_labels').each((idx, n) => $(n).remove()); |
| 1105 | + |
| 1106 | + // sort the table in value-order, so the images are in the same |
| 1107 | + // order as the data used to draw the rects |
| 1108 | + table.sort((r1,r2) => (toFixnum(r1[1]) < toFixnum(r2[1]))? -1 : 0) |
| 1109 | + |
| 1110 | + // walk the table and swap in the images for the rects |
| 1111 | + table.forEach(function (row, i) { |
| 1112 | + const rect = rects[i]; |
| 1113 | + // make an image element for the img, from the SVG namespace |
| 1114 | + const imgDOM = row[2].val.toDomNode(); |
| 1115 | + row[2].val.render(imgDOM.getContext('2d'), 0, 0); |
| 1116 | + let imageElt = document.createElementNS("http://www.w3.org/2000/svg", 'image'); |
| 1117 | + imageElt.classList.add('__img_labels'); // tag for later garbage collection |
| 1118 | + imageElt.setAttributeNS(null, 'href', imgDOM.toDataURL()); |
| 1119 | + // position it using the position of the corresponding rect |
| 1120 | + imageElt.setAttribute('preserveAspectRatio', 'none'); |
| 1121 | + imageElt.setAttribute('x', rects[i].getAttribute('x')); |
| 1122 | + imageElt.setAttribute('y', rects[i].getAttribute('y')); |
| 1123 | + imageElt.setAttribute('width', rects[i].getAttribute('width')); |
| 1124 | + imageElt.setAttribute('height', rects[i].getAttribute('height')); |
| 1125 | + Object.assign(imageElt, rects[i]); // we should probably not steal *everything*... |
| 1126 | + rectRoot.appendChild(imageElt); |
| 1127 | + }); |
1128 | 1128 | })
|
1129 | 1129 | }
|
1130 | 1130 | };
|
|
0 commit comments