Skip to content

Commit 8742a0b

Browse files
authored
Merge pull request #466 from brownplt/horizon
fix for image-histogram
2 parents d0ad725 + 88ed161 commit 8742a0b

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/web/js/trove/chart-lib.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,6 @@
6161
return true;
6262
}
6363
}
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-
}
9064

9165
google.charts.load('current', {'packages' : ['corechart']});
9266

@@ -1123,8 +1097,34 @@
11231097
// The only way to hijack rect events is to walk the DOM here
11241098
// If Google changes the DOM, these lines will likely break
11251099
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+
});
11281128
})
11291129
}
11301130
};

0 commit comments

Comments
 (0)