Public
Edited
May 8, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
flowerPlot = d3.select(svgCell)
Insert cell
import {toNum} from "@emfielduva/dvlib"
Insert cell
import {radiansFromDeg} from "@emfielduva/dvlib"
Insert cell
import {polarXY} from "@emfielduva/dvlib"
Insert cell
data = FileAttachment("Sampled_BidenvTrump@2.csv").csv()
Insert cell
data.forEach(d => {
d["pct_diff_x"] = polarXY(d["sample_size"], 90 - (d["pct_diff"]*5.294))["x"];
d["pct_diff_y"] = polarXY(d["sample_size"], 90 - (d["pct_diff"]*5.294))["y"];
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
datalines = flowerPlot.selectAll(".dataline")
.data(data)
.join("path")
.attr("d", d=> "M " + viz_width_mid + " " + baseline + " l " + d["pct_diff_x"]*line_scale + " -" +d["pct_diff_y"]*line_scale)
.attr("class", d => {
if (d["candidate_name"] == "Joe Biden" && d["partisan"] != "") {return "dataline biden"}
else if (d["candidate_name"] == "Donald Trump" && d["partisan"] != "") {return "trump dataline"}
else {return "dataline"}}
)
Insert cell
zoomToBoundingBox = (evt,d) => {
evt.stopPropagation();
let viewBox = "300 600 200 50";
let svg = d3.select(svgCell);
svg.transition().duration(1000).attr("viewBox", viewBox);
}
Insert cell
dimAll = () => {
datalines.attr("class", "dataline")
}
Insert cell
filter_all = (field, val) => {
if (val == "all") {return datalines}
else if (val != "all") {return datalines.filter(d => d[field] == val)}
}
Insert cell
filter = (field,val) => {
// create a function to increase opacity only for a single element, via its data.
dimAll(); // first we call dimAll to dim all of them.
filter_all(field, val).attr("class", d => {
if (d["candidate_name"] == "Joe Biden" && d["partisan"] != "") {return "dataline biden"}
else if (d["candidate_name"] == "Donald Trump" && d["partisan"] != "") {return "trump dataline"}
else {return "dataline"}})
}
Insert cell
bigDim = () => {
datalines.attr("class", "noline")
}
Insert cell
voter_filter = (val) => {
bigDim();
datalines.filter(d => d["population"] == val).attr("class", d => {
if (d["candidate_name"] == "Joe Biden" && d["partisan"] != "") {return "dataline biden"}
else if (d["candidate_name"] == "Donald Trump" && d["partisan"] != "") {return "trump dataline"}
else {return "dataline"}})
}
Insert cell
filter_all_states = (val) => {
if (val == "All polls") {return datalines}
else if (val != "All polls") {return datalines.filter(d => d["state"] == val)}
}
Insert cell
state_filter = (val) => {
bigDim();
filter_all_states(val).attr("class", d => {
if (d["candidate_name"] == "Joe Biden" && d["partisan"] != "") {return "dataline biden"}
else if (d["candidate_name"] == "Donald Trump" && d["partisan"] != "") {return "trump dataline"}
else {return "dataline"}})
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more