{
const plotWidth = 600;
const plotHeight = 400;
const plotMargin = 25;
const outerWidth = plotWidth + 2 * plotMargin;
const outerHeight = plotHeight + 2 * plotMargin;
const svg = d3.create("svg")
.attr("width", outerWidth)
.attr("height", outerHeight)
.attr("transform", `translate(${plotMargin}, ${plotMargin})`);
const speakerAvgViews = {};
const speakerTotalViews = {};
const speakerNumInvites = {};
tedTalkData.forEach(d => {
d.views = +d.views;
if (!(d.main_speaker in speakerTotalViews)) {
speakerTotalViews[d.main_speaker] = d.views;
speakerNumInvites[d.main_speaker] = 1;
}
else {
speakerTotalViews[d.main_speaker] += d.views;
speakerNumInvites[d.main_speaker] += 1;
}
});
Object.keys(speakerTotalViews).forEach(d => {
speakerAvgViews[d] = speakerTotalViews[d] / speakerNumInvites[d];
});
let visOneData = Object.keys(speakerTotalViews).map(speaker => ({
speaker,
total_views: speakerTotalViews[speaker],
average_views: speakerAvgViews[speaker],
num_invites: speakerNumInvites[speaker],
}));
const xScale = d3.scaleLinear()
.domain([0, d3.max(visOneData, d => d.total_views)])
.range([0, plotWidth]);
const yScale = d3.scaleLinear()
.domain([0, d3.max(visOneData, d => d.average_views)])
.range([0, plotHeight]);
const pointScale = d3.scaleLinear()
.domain([0, d3.max(visOneData, d => d.num_invites)])
.range([1, 5]);
svg.selectAll("circle")
.data(visOneData)
.join(
(enter) => enter.append("circle"),
(update) => update,
(exit) => exit.remove()
)
.attr("class", "speaker")
.attr("r", d => pointScale(d.num_invites))
.attr("cx", d => xScale(d.total_views))
.attr("cy", d => yScale(d.average_views))
.attr("opacity", "0.7")
.attr("fill", "red");
return svg.node();
}