Published
Edited
Jul 28, 2022
Insert cell
Insert cell
_new_chart = {
const zoom = d3.zoom()
.scaleExtent([0.5, 32])
.on("zoom", zoomed);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

// const gGrid = svg.append("g");

const gDot = svg.append("g")
.attr("fill", "none")
.attr("stroke-linecap", "round");

const alldots = gDot.selectAll('.ele')
.data(data)
.enter()
.append('g')

.attr('class', 'ele');

const circles = alldots
.append("path")
// .attr("r",3)
.attr("d", d => `M${x(d['x'])},${y(d['y'])}h0`)
.attr("stroke", d => d['color_name']);
// .attr('stroke', d=> {
// let val = parseFloat(d.color_name)
// console.log(color_scale(val))
// return color_scale(val)
// })

// const labels = alldots
// .append('text')
// .attr('x', d=> x(d.x))
// .attr('y', d=> y(d.y))
// .attr('dx', '-3em')
// .attr('fill','black')
// .text(d=>d['text'])
// .style('font-size',3)

// .attr('class','label_text');
const gx = svg.append("g");

const gy = svg.append("g");

svg.call(zoom).call(zoom.transform, d3.zoomIdentity);

function zoomed({transform}) {
const zx = transform.rescaleX(x).interpolate(d3.interpolateRound);
const zy = transform.rescaleY(y).interpolate(d3.interpolateRound);
gDot.attr("transform", transform).attr("stroke-width", 9/ transform.k);

var radius = transform.k
var side = 2 * radius * Math.cos(Math.PI / 4),
dx = radius - side / 2;
// labels.attr('font-size', 0.1/transform.k)
gx.call(xAxis, zx);
gy.call(yAxis, zy);
// gGrid.call(grid, zx, zy);
}

return Object.assign(svg.node(), {
reset() {
svg.transition()
.duration(750)
.call(zoom.transform, d3.zoomIdentity);
}
});
}
Insert cell
_new_chart2 = {
const zoom = d3.zoom()
.scaleExtent([0.5, 32])
.on("zoom", zoomed);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

// const gGrid = svg.append("g");

const gDot = svg.append("g")
.attr("fill", "none")
.attr("stroke-linecap", "round");

const alldots = gDot.selectAll('.ele')
.data(data2)
.enter()
.append('g')
.attr('class', 'ele');

const circles = alldots
.append("path")
// .attr("r",3)
.attr("d", d => `M${x(d['x'])},${y(d['y'])}h0`)
.attr('stroke', d=> {
let val = parseFloat(d.color_name)
console.log(color_scale(val))
return color_scale(val)
})

const labels = alldots
.append('text')
.attr('x', d=> x(d.x))
.attr('y', d=> y(d.y))
.attr('dx', '-3em')
.attr('fill','black')
.text(d=>d['text'])
.style('font-size',3)

.attr('class','label_text');
const gx = svg.append("g");

const gy = svg.append("g");

svg.call(zoom).call(zoom.transform, d3.zoomIdentity);

function zoomed({transform}) {
const zx = transform.rescaleX(x).interpolate(d3.interpolateRound);
const zy = transform.rescaleY(y).interpolate(d3.interpolateRound);
gDot.attr("transform", transform).attr("stroke-width", 9/ transform.k);

var radius = transform.k
var side = 2 * radius * Math.cos(Math.PI / 4),
dx = radius - side / 2;
labels.attr('font-size', 0.1/transform.k)
gx.call(xAxis, zx);
gy.call(yAxis, zy);
// gGrid.call(grid, zx, zy);
}

return Object.assign(svg.node(), {
reset() {
svg.transition()
.duration(750)
.call(zoom.transform, d3.zoomIdentity);
}
});
}
Insert cell
Insert cell
grid = (g, x, y) => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g
.selectAll(".x")
.data(x.ticks(12))
.join(
enter => enter.append("line").attr("class", "x").attr("y2", height),
update => update,
exit => exit.remove()
)
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d)))
.call(g => g
.selectAll(".y")
.data(y.ticks(12 * k))
.join(
enter => enter.append("line").attr("class", "y").attr("x2", width),
update => update,
exit => exit.remove()
)
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d)));
Insert cell
k = height / width
Insert cell
Insert cell
color_scale = d3.scaleLinear()
.domain([0, max_/20, max_/2, max_])
// .range(['white', 'yellow', 'orange', 'red'])
.range(["#deebf7","#6baed6", "#2171b5", "#084594"])
Insert cell
max_ = d3.extent(data2, d=> d.color_name)[1]
Insert cell
min_ = d3.extent(data2, d => d.color_name)[0]
Insert cell
// data = FileAttachment("vispub_compare_scatter_user_mode.csv").csv({typed: true})
data = FileAttachment("vispub_compare_scatter_init_mode (3).csv").csv({typed: true})
Insert cell
data2 = FileAttachment("vispub_compare_scatter_user_mode.csv").csv({typed: true})
Insert cell
height = 1200
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data, d => d.x)).nice()
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.y)).nice()
.range([ margin.top, height - margin.bottom])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
Insert cell
margin = ({top: 25, right: 20, bottom: 35, left: 40})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more