splitVotesGrowGolfBalls = {
const container = d3.select(DOM.svg(1000, 300));
const limit = 10;
const imageLink = 'https://upload.wikimedia.org/wikipedia/commons/4/47/Golf_ball.svg';
const imageHeight = 20
const styleText = `text {
font: 1.2em sans-serif;
text-anchor: middle;
fill: blue;
}`
container.append("style").text(styleText);
const pinkGroup = container
.append("g")
.attr("transform", () => "translate(" + 40 + "," + 20 + ")")
.attr("id", "pinkGroup")
.attr('fill', '#ff96ca')
const greenGroup = container
.append("g")
.attr("transform", () => "translate(" + 100 + "," + 20 + ")")
.attr("id", "greenGroup")
.attr("fill","lime")
function init(svg, value) {
svg.attr("value", value);
svg.append("circle")
.attr("r", '20')
svg.append("text")
.text(svg.attr("value"))
.attr("dy", "0.35em")
const arrayOfInts = d3.range(1,value+1);
svg.selectAll("image")
.data(arrayOfInts)
.enter().append("image")
.attr('xlink:href', imageLink)
.attr("width", imageHeight)
.attr("height", imageHeight)
.attr("x", -10 )
.attr("y", d => d*imageHeight)
.attr("id", d => svg.attr("id") + "image" + d);
}
init(pinkGroup, 3)
init(greenGroup, limit-3);
function updateScores(svg1, svg2) {
let score1 = +svg1.attr("value");
let score2 = +svg2.attr("value");
if (score1 >= limit) return;
score1 += 1;
score2 -= 1;
svg1
.append("image").attr('xlink:href', imageLink)
.attr("width", imageHeight).attr("height", imageHeight)
.attr("x", -10 )
.attr("y", score1*imageHeight)
.attr("id", svg1.attr("id") + "image" + score1);
const imageToRemove = "#" + svg2.attr("id") + "image" + svg2.attr("value")
d3.selectAll(imageToRemove).remove();
svg1.select("text").text(score1);
svg1.attr("value", score1);
svg2.select("text").text(score2);
svg2.attr("value", score2);
}
pinkGroup.on("click", function(d) {
updateScores(pinkGroup, greenGroup);
});
greenGroup.on("click", function(d) {
updateScores(greenGroup, pinkGroup);
});
return container.node();
}