Skip to content

Commit 531973c

Browse files
author
Emmanuel Schanzer
committed
dot plots: fix bad indenting, and move garbage collection and layout access outside of the image-scatter condition
1 parent 5de828e commit 531973c

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,9 @@ ${labelRow}`;
16521652
// legendEnabled to decided which index to look up.
16531653
// This is brittle and needs to be revisited
16541654
const svgRoot = chart.container.querySelector('svg');
1655+
const layout = chart.getChartLayoutInterface();
1656+
// remove any labels that have previously been drawn
1657+
$('.__img_labels').each((idx, n) => $(n).remove());
16551658

16561659
let markers;
16571660
if(legendEnabled) {
@@ -1661,29 +1664,25 @@ ${labelRow}`;
16611664
}
16621665
if (hasImage) {
16631666

1664-
const layout = chart.getChartLayoutInterface();
1665-
// remove any labels that have previously been drawn
1666-
$('.__img_labels').each((idx, n) => $(n).remove());
1667-
1668-
// for each point, (1) find the x,y location, (2) render the SVGImage,
1669-
// (3) center it on the datapoint, (4) steal all the events
1670-
// and (5) add it to the chart
1671-
combined.forEach((p, i) => {
1672-
get(p, 'ps').filter(p => p[3]).forEach((p, i) => {
1673-
const xPos = layout.getXLocation(data.getValue(i, 0));
1674-
const yPos = layout.getYLocation(data.getValue(i, 1));
1675-
const imgDOM = p[3].val.toDomNode();
1676-
p[3].val.render(imgDOM.getContext('2d'), 0, 0);
1677-
// make an image element from the SVG namespace
1678-
let imageElt = document.createElementNS("http://www.w3.org/2000/svg", 'image');
1679-
imageElt.classList.add('__img_labels'); // tag for later garbage collection
1680-
imageElt.setAttributeNS(null, 'href', imgDOM.toDataURL());
1681-
imageElt.setAttribute('x', xPos - imgDOM.width/2); // center the image
1682-
imageElt.setAttribute('y', yPos - imgDOM.height/2); // center the image
1683-
Object.assign(imageElt, markers[i]); // we should probably not steal *everything*...
1684-
svgRoot.appendChild(imageElt);
1667+
// for each point, (1) find the x,y location, (2) render the SVGImage,
1668+
// (3) center it on the datapoint, (4) steal all the events
1669+
// and (5) add it to the chart
1670+
combined.forEach((p, i) => {
1671+
get(p, 'ps').filter(p => p[3]).forEach((p, i) => {
1672+
const xPos = layout.getXLocation(data.getValue(i, 0));
1673+
const yPos = layout.getYLocation(data.getValue(i, 1));
1674+
const imgDOM = p[3].val.toDomNode();
1675+
p[3].val.render(imgDOM.getContext('2d'), 0, 0);
1676+
// make an image element from the SVG namespace
1677+
let imageElt = document.createElementNS("http://www.w3.org/2000/svg", 'image');
1678+
imageElt.classList.add('__img_labels'); // tag for later garbage collection
1679+
imageElt.setAttributeNS(null, 'href', imgDOM.toDataURL());
1680+
imageElt.setAttribute('x', xPos - imgDOM.width/2); // center the image
1681+
imageElt.setAttribute('y', yPos - imgDOM.height/2); // center the image
1682+
Object.assign(imageElt, markers[i]); // we should probably not steal *everything*...
1683+
svgRoot.appendChild(imageElt);
1684+
});
16851685
});
1686-
});
16871686
}
16881687

16891688
if (dotChartP) {
@@ -1705,7 +1704,7 @@ ${labelRow}`;
17051704
}
17061705

17071706
// compute circleY, and add a new SVG to the graph
1708-
circles.forEach((circle) => {
1707+
circles.forEach( (circle, i) => {
17091708
const circleX = Number(circle.getAttribute('cx'));
17101709

17111710
// Shift the circle up by r+1, so it sits on the x-axis
@@ -1721,7 +1720,7 @@ ${labelRow}`;
17211720
const randomVHeight = (1 - Math.random()**2) * usableHeight;
17221721
circleY = randomVHeight + chartCeiling - circleR;
17231722
}
1724-
1723+
// save the location of the new dot along with all the others
17251724
prevDotArray.push([circleX, circleY]);
17261725

17271726
const circleElt = circle.cloneNode(false);

0 commit comments

Comments
 (0)